-
Notifications
You must be signed in to change notification settings - Fork 3
/
hello.c
63 lines (53 loc) · 1.18 KB
/
hello.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
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include <stdio.h>
void uart_putc(char c)
{
while((USISR & 1) == 0)
;
for(uint16_t i = 0 ; i < 100 ; i++)
asm("nop");
USIDR = c;
}
void uart_puts(const char * s)
{
while(*s)
uart_putc(*s++);
}
static void __attribute__((__noinline__)) pwm(uint8_t b, uint8_t led)
{
for(uint8_t i = 0 ; i < b ; i++)
PORTB = led;
for(uint8_t i = b ; i < 255 ; i++)
PORTB = 0;
}
static char __attribute__((__noinline__)) hexdigit(uint8_t x)
{
static const char PROGMEM hexdigit[16] = "0123456789abcdef";
return pgm_read_byte_near(hexdigit + (x & 0xF));
}
int main(void)
{
uint16_t cycle = 0xFADE;
*(volatile uint8_t*) 0x20 = hexdigit(cycle >> 4);
*(volatile uint8_t*) 0x20 = hexdigit(cycle >> 0);
while(1)
{
uint8_t bright = cycle >> 4;
if (bright & 0x80)
bright = ~bright;
pwm(bright, cycle >> 12);
cycle++;
if ((cycle & 0xFFF) != 0)
continue;
uart_putc(hexdigit(bright >> 4));
uart_putc(hexdigit(bright >> 0));
uart_putc(' ');
uart_putc(hexdigit(cycle >> 12));
uart_putc(hexdigit(cycle >> 8));
uart_putc(hexdigit(cycle >> 4));
uart_putc(hexdigit(cycle >> 0));
uart_puts(" Hello world\r\n");
}
}