-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
137 lines (118 loc) · 2.62 KB
/
Copy pathmain.c
File metadata and controls
137 lines (118 loc) · 2.62 KB
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
130
131
132
133
134
135
136
137
/*
* Assignment4.c
*
* Created: 2/14/2017 3:02:49 PM
* Author : Carter W, Drake S
*/
// Use sei() and cli() to set and clear interrupt status
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdbool.h>
int checkButtons();
void delay(int ms);
void delay_usec(unsigned int us);
#define OFF_STATE 0
#define CYCLE_STATE 1
#define FLASH_STATE 2
volatile bool button0Pressed = false;
volatile bool button0Released = false;
volatile bool button1Pressed = false;
volatile bool button1Released = false;
int state = OFF_STATE;
int main(void)
{
//Change input to PORTK0 A8; PORTK1 A9
DDRK &= ~0x03; //Setting pin 0 and 1 for input
PORTK |= 0x03; //Setting pin 0 and 1 to idle at high voltage
PORTF &= ~0x0F; //LEDs off
PCICR |= 0x04; //Enabling PCIE2 to accept interrupts
PCMSK2 |= 0x03; //Specifically enabling PCINT16 and 17s
int light_index = 0;
while (1)
{
sei();
//Flashing State
if(state == FLASH_STATE) {
//Lights are off
if(light_index == 0) {
PORTF |= 0x0F;
light_index = 1;
delay(200);
}
//Lights are on
else {
PORTF &= ~0x0F;
light_index = 0;
delay(1000);
}
}
//Cycling State
else if(state == CYCLE_STATE) {
PORTF &= ~0x0F;
light_index = (light_index + 1) % 4;
PORTF |= (1 << light_index);
delay(1000);
}
//Off State
else if(state == OFF_STATE) {
PORTF &= ~0x0F;
light_index = 0;
checkButtons();
}
}
}
//Break out needed
void delay(int ms) {
for(int i = 0; i < ms; i++) {
if(checkButtons())
break;
_delay_ms(1);
}
}
//Returns true if button pressed
int checkButtons() {
int buttonPress = 0;
//Button 0 has been pressed
if(button0Released) {
button0Released = false;
if(state == CYCLE_STATE)
state = OFF_STATE;
else
state = CYCLE_STATE;
buttonPress = 1;
}
//Button 1 has been pressed
if(button1Released) {
button1Released = false;
if(state == FLASH_STATE)
state = OFF_STATE;
else
state = FLASH_STATE;
buttonPress = 1;
}
return buttonPress;
}
ISR(PCINT2_vect)
{
cli();
// Cycle interrupt
if(0x01 & PINK) { //Checking that PORTK pin0 is high, meaning button has been released
delay_usec(110);
if(button0Pressed && (0x01 & PINK)) {
button0Released = true;
button0Pressed = false;
}
} else //pin is low, button has been pressed
button0Pressed = true;
// Flashing interrupt
if(0x02 & PINK) { //Checking that PORTK pin0 is high, meaning button has been released
delay_usec(110);
if(button1Pressed && (0x02 & PINK)) {
button1Released = true;
button1Pressed = false;
}
} else //pin is low, button has been pressed
button1Pressed = true;
sei();
}