-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
129 lines (102 loc) · 2.78 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#define F_CPU 16000000UL
#define keypadDirectionRegisterR DDRB
#define keypadPortControlR PORTB
#define keypadPortValueR PINB
#define keypadDirectionRegisterC DDRC
#define keypadPortControlC PORTC
#define keypadPortValueC PINC
#define LEDDirectionRegister DDRD
#define LEDPort PORTD
#include <avr/io.h>
#include <util/delay.h>
void keypadScan();
int main(void){
// Initialize LED
LEDDirectionRegister = (1<<0);
// Keypad initialization
keypadDirectionRegisterR = (1<<0) | (1<<1) | (1<<2) | (1<<3);
keypadDirectionRegisterC = (0<<0) | (0<<1) | (0<<2) | (0<<3);
keypadPortControlR = (0<<0) | (0<<1) | (0<<2) | (0<<3);
keypadPortControlC = (1<<0) | (1<<1) | (1<<2) | (1<<3);
while(1){
keypadScan();
}
}
void keypadScan(){
// Store value for column
uint8_t keyPressCodeC = keypadPortValueC;
keypadDirectionRegisterC ^= (1<<0) | (1<<1) | (1<<2) | (1<<3);
keypadDirectionRegisterR ^= (1<<0) | (1<<1) | (1<<2) | (1<<3);
keypadPortControlC ^= (1<<0) | (1<<1) | (1<<2) | (1<<3);
keypadPortControlR ^= (1<<0) | (1<<1) | (1<<2) | (1<<3);
_delay_ms(5);
// Store value for row
int temp = keypadPortValueR;
uint8_t keyPressCodeR = temp << 4;
// Add value for column and row
uint8_t keyPressCode = keyPressCodeC | keyPressCodeR;
uint8_t blinkDuration = 0;
// Comparison and resultant action
// Column one
if(keyPressCode == 0b11101110){
blinkDuration = 1;
}
if(keyPressCode == 0b11011110){
blinkDuration = 2;
}
if(keyPressCode == 0b10111110){
blinkDuration = 3;
}
if(keyPressCode == 0b01111110){
blinkDuration = 4;
}
// Column two
if(keyPressCode == 0b11101101){
blinkDuration = 5;
}
if(keyPressCode == 0b11011101){
blinkDuration = 6;
}
if(keyPressCode == 0b10111101){
blinkDuration = 7;
}
if(keyPressCode == 0b01111101){
blinkDuration = 8;
}
// Column three
if(keyPressCode == 0b11101011){
blinkDuration = 9;
}
if(keyPressCode == 0b11011011){
blinkDuration = 10;
}
if(keyPressCode == 0b10111011){
blinkDuration = 11;
}
if(keyPressCode == 0b01111011){
blinkDuration = 12;
}
// Column four
if(keyPressCode == 0b11100111){
blinkDuration = 13;
}
if(keyPressCode == 0b11010111){
blinkDuration = 14;
}
if(keyPressCode == 0b10110111){
blinkDuration = 15;
}
if(keyPressCode == 0b01110111){
blinkDuration = 16;
}
// Toggles the led on and off
if (keyPressCode < 0xFF){
int i;
for (i = 0; i < blinkDuration; i++){
_delay_ms(50);
LEDPort ^= (1<<0);
_delay_ms(50);
LEDPort ^= (1<<0);
}
}
}