-
Notifications
You must be signed in to change notification settings - Fork 3
/
Keypad.c
94 lines (80 loc) · 1.74 KB
/
Keypad.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <avr/io.h>
#include <util/delay.h>
//*******************************
#define KEYPAD_PORT PORTB
#define KEYPAD_DDR DDRB
#define KEYPAD_PIN PINB
#define Segment_PORT PORTD
#define Segment_DDR DDRD
//*******************************
typedef unsigned char BYTE;
int key_scan(void) {
BYTE KEY;
BYTE temp_SW;
//read Port D
KEY = KEYPAD_PIN & 0x07;
//get only low 3 bits
//KEY &= 0x07;
temp_SW = 0;
if (KEY != 0x07) //if a key is pressed
{
// KEY = PIND;
// KEY &= 0x0f;
// if(KEY != 0x0f)
// {
switch (KEY) {
case 0x03: //00000011
temp_SW = 1;
break;
case 0x05: //00000101
temp_SW = 2;
break;
default:
temp_SW = 3;
}
// }
}
return temp_SW;
}
int main(void) {
BYTE bKey;
BYTE SEG[12] = { 0XC0, 0XF9, 0XA4, 0XB0, 0X99, 0X92, 0X82, 0XD8, 0X80, 0X90,
0X88, 0X8e };
Segment_DDR = 0xff; // 7 segments
Segment_PORT = 0xff;
// high 4bits: output
// low 4bits: input
KEYPAD_DDR = 0xF8; 0x11111000
while (1) {
KEYPAD_PORT = 0xF7; // only bit 6 low, 0b11110111
//PORTD = ~_BV(2); // only bit 6 low, 0b1011
_delay_ms(20);
bKey = key_scan();
if (bKey == 1)
Segment_PORT = 3;
else if (bKey == 2)
Segment_PORT = 6;
else if (bKey == 3)
Segment_PORT = 9;
KEYPAD_PORT = 0xE8; // only bit 5 low, 0b1101
//PORTD = ~_BV(1); // only bit 6 low, 0b1101
_delay_ms(20);
bKey = key_scan();
if (bKey == 1)
Segment_PORT = 2;
else if (bKey == 2)
Segment_PORT = 5;
else if (bKey == 3)
Segment_PORT = 8;
KEYPAD_PORT = 0xD8; // only bit 4 low, 0b1110
//PORTD = ~_BV(0); // only bit 6 low, 0b1110
_delay_ms(20);
bKey = key_scan();
if (bKey == 1)
Segment_PORT = 1;
else if (bKey == 2)
Segment_PORT = 4;
else if (bKey == 3)
Segment_PORT = 7;
}
}