-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblinker.c
More file actions
67 lines (58 loc) · 1.12 KB
/
blinker.c
File metadata and controls
67 lines (58 loc) · 1.12 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
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define USE_INTERRUPT
#define GCC
#ifdef GCC
#define MAIN_RETURN void
#else
#define MAIN_RETURN int
#undef ISR
#define ISR(vector) \
void vector (void) __attribute__ ((interrupt));\
void vector (void)
#endif
#define LED_PIN 5
#define ONE_SECOND 63974
#define BIT(n) (1 << n)
static void flip_led()
{
PORTB ^= BIT(LED_PIN);
}
#ifdef USE_INTERRUPT
ISR (TIMER1_OVF_vect)
{
flip_led();
TCNT1 = ONE_SECOND;
}
#endif
static __attribute__((unused)) void delay(uint8_t ms)
{
for (uint8_t i = 0; i < ms; i++)
{
for (volatile uint16_t j = 0; j < 0x34b; j++)
;
}
}
MAIN_RETURN main(void) __attribute__((noreturn));
MAIN_RETURN main(void)
{
DDRB = BIT(LED_PIN);
PORTB = 0x00;
#ifdef USE_INTERRUPT
TCNT1 = ONE_SECOND;
TCCR1A = 0x00;
TCCR1B = BIT(CS10) | BIT(CS12); // timer, prescale: 1024
TIMSK1 = BIT(TOIE1); // timer1 overflow interrupt
sei(); // global interrupt enable
while (true)
;
#else
while(true)
{
delay(100);
flip_led();
}
#endif
}