]> www.fi.muni.cz Git - tinyboard.git/blob - projects/rgb-led-string/serial.c
Merge branch 'master' of ssh://anxur.fi.muni.cz/~kas/html/git/tinyboard
[tinyboard.git] / projects / rgb-led-string / serial.c
1 #include <avr/io.h>
2 #include <util/delay.h>
3 #include <avr/interrupt.h>
4
5 #include "rgbstring.h"
6
7 void init_serial()
8 {
9         PORTB &= ~(_BV(PB2) | _BV(PB1));
10         DDRB |= _BV(PB2) | _BV(PB1);
11
12 #if 0
13         TCCR0A = _BV(WGM01) | _BV(WGM00);
14         TCCR0B = _BV(WGM02) | _BV(CS00);
15         OCR0A = 2;
16 #endif
17
18         zero_frame();
19 }
20
21 static void send_byte(unsigned char b)
22 {
23         unsigned char i, mask;
24
25 #if 0
26         USIDR = b;
27         USISR = _BV(USIOIF);
28         USICR = _BV(USIWM0) | _BV(USICS0);
29
30         while (!(USISR & _BV(USIOIF)))
31                 ;
32 #endif
33
34 #if 1
35         USIDR = b;
36         USISR = _BV(USIOIF);
37
38         while ( (USISR & _BV(USIOIF)) == 0 ) {
39                 USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK);
40                 USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC);
41         }
42 #endif
43
44 #if 0
45         for (i = 0; i < 8; i++) {
46                 USICR = _BV(USIWM0) | _BV(USITC);
47                 USICR = _BV(USIWM0) | _BV(USITC) | _BV(USICLK);
48         }
49 #endif
50
51 #if 0
52         for (i = 0; i < 8; i++) {
53                 PORTB &= ~_BV(PB2);             // clock low
54                 if (b & 0x80)                   // data bit on or off
55                         PORTB |= _BV(PB1);
56                 else
57                         PORTB &= ~_BV(PB1);
58                 b <<= 1;
59                 PORTB |= _BV(PB2);              // clock high
60         }
61 #endif
62 }
63
64 void end_frame()
65 {
66         PORTB &= ~_BV(PB2);                     // clock low
67         _delay_us(1000);
68 }
69
70 void send_rgb(unsigned char r, unsigned char g, unsigned char b)
71 {
72         send_byte(r);
73         send_byte(g);
74         send_byte(b);
75 }
76
77
78 void zero_frame()
79 {
80         unsigned char i;
81
82         for (i = 0; i < STRIP_SIZE; i++) {
83                 send_rgb(0, 0, 0);
84         }
85
86         end_frame();
87 }
88