-
Notifications
You must be signed in to change notification settings - Fork 0
/
blinkylights.c
231 lines (176 loc) · 3.55 KB
/
blinkylights.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <util/delay.h>
#include <stdint.h>
typedef enum {FALSE=0, TRUE} bool_t;
#define LED0_PIN PB0
#define BUTTON_PIN PB3
void init(void);
void solid(void);
void fade(void);
void blink(void);
void wait_for_watchdog(uint8_t period);
void sleep_mcu(void);
void pwm_on(void);
void pwm_off(void);
bool_t check_button(void);
// Sine table generated from http://www.daycounter.com/Calculators/Sine-Generator-Calculator2.phtml
const uint8_t PROGMEM sine_table[] = {
1,1,2,3,5,6,8,10,12,15,17,20,23,26,29,33,36,40,44,
48,53,57,61,66,71,76,81,86,91,96,101,106,112,117,
122,128,133,138,143,149,154,159,164,169,174,179,184,
189,194,198,202,207,211,215,219,222,226,229,232,235,
238,240,243,245,247,249,250,252,253,254,254,255,255,
255,255,254,254,253,252,250,249,247,245,243,240,238,
235,232,229,226,222,219,215,211,207,202,198,194,189,
184,179,174,169,164,159,154,149,143,138,133,128,122,
117,112,106,101,96,91,86,81,76,71,66,61,57,53,48,44,
40,36,33,29,26,23,20,17,15,12,10,8,6,5,3,2,1,1,0
};
volatile bool_t button_pressed = FALSE;
int main(void)
{
init();
while(1)
{
solid();
fade();
blink();
}
return 0;
}
void init(void)
{
uint8_t status = MCUSR;
MCUSR = 0;
// Determine if the reset source was a brownout
if(bit_is_set(status, BORF))
{
// Don't do any more
while(1)
{
}
}
cli();
DDRB = 0;
DDRB = _BV(LED0_PIN);
// Enable pull-up resistors on input ports
PORTB = _BV(BUTTON_PIN) |
_BV(PB1) |
_BV(PB2) |
_BV(PB5);
// Pin change interrupt
GIMSK |= _BV(PCIE);
PCMSK |= _BV(BUTTON_PIN);
GIFR |= _BV(PCIF);
sei();
}
void solid(void)
{
while(1)
{
PORTB |= _BV(LED0_PIN);
sleep_mcu();
if(check_button())
{
break;
}
}
}
void fade(void)
{
const register uint8_t *cstp = sine_table;
register uint8_t cstv;
OCR0A = 0;
pwm_on();
while(1)
{
if(check_button())
{
break;
}
cstv = pgm_read_byte_near(cstp);
if(cstv == 0)
{
cstp = sine_table;
continue;
}
OCR0A = cstv;
wait_for_watchdog(WDTO_30MS);
cstp++;
}
pwm_off();
}
void blink(void)
{
uint8_t level = 0xFF;
pwm_on();
while(1)
{
if(check_button())
{
break;
}
OCR0A = level;
wait_for_watchdog(WDTO_500MS);
level ^= 0xFF;
}
pwm_off();
}
void pwm_on(void)
{
TCCR0A |= ( _BV(WGM00) | _BV(WGM01) | _BV(COM0A1) );
TCCR0B |= ( _BV(CS01) | _BV(CS00) );
}
void pwm_off(void)
{
TCCR0A = 0;
TCCR0B = 0;
}
void wait_for_watchdog(uint8_t period)
{
cli();
wdt_reset();
WDTCR = (_BV(WDTIF) | _BV(WDTIE) | period);
sei();
sleep_mcu();
// Disable watchdog interrupt
WDTCR &= ~_BV(WDTIE);
}
void sleep_mcu(void)
{
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_mode();
// mcu woken up and control returned here
sleep_disable();
}
bool_t check_button(void)
{
if(button_pressed)
{
button_pressed = FALSE;
return TRUE;
}
return FALSE;
}
ISR(PCINT0_vect)
{
if(bit_is_clear(PINB, BUTTON_PIN))
{
_delay_ms(20);
if(bit_is_clear(PINB, BUTTON_PIN))
{
button_pressed = TRUE;
}
else
{
button_pressed = FALSE;
}
}
}
EMPTY_INTERRUPT(WDT_vect);