X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=tinyboard.git;a=blobdiff_plain;f=projects%2Frgb-led-light%2Fserial.c;fp=projects%2Frgb-led-light%2Fserial.c;h=1ab2e42dc891c38c1b354be02877323f2fea7774;hp=0000000000000000000000000000000000000000;hb=65fc48a197b52af65b549095a4171cfc8746373d;hpb=3c1c6465bb29893ccb26b131986d803a169f007f diff --git a/projects/rgb-led-light/serial.c b/projects/rgb-led-light/serial.c new file mode 100644 index 0000000..1ab2e42 --- /dev/null +++ b/projects/rgb-led-light/serial.c @@ -0,0 +1,88 @@ +#include +#include +#include + +#include "rgbstring.h" + +void init_serial() +{ + PORTB &= ~(_BV(PB1) | _BV(PB2)); + DDRB |= _BV(PB1) | _BV(PB2); + +#if 0 + TCCR0A = _BV(WGM01) | _BV(WGM00); + TCCR0B = _BV(WGM02) | _BV(CS00); + OCR0A = 2; +#endif + + zero_frame(); +} + +static void send_byte(unsigned char b) +{ + unsigned char i, mask; + +#if 0 + USIDR = b; + USISR = _BV(USIOIF); + USICR = _BV(USIWM0) | _BV(USICS0); + + while (!(USISR & _BV(USIOIF))) + ; +#endif + +#if 0 + USIDR = b; + USISR = _BV(USIOIF); + + while ( (USISR & _BV(USIOIF)) == 0 ) { + USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK); + USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC); + } +#endif + +#if 0 + for (i = 0; i < 8; i++) { + USICR = _BV(USIWM0) | _BV(USITC); + USICR = _BV(USIWM0) | _BV(USITC) | _BV(USICLK); + } +#endif + +#if 1 + for (i = 0; i < 8; i++) { + PORTB &= ~_BV(PB2); // clock low + if (b & 0x80) // data bit on or off + PORTB |= _BV(PB1); + else + PORTB &= ~_BV(PB1); + b <<= 1; + PORTB |= _BV(PB2); // clock high + } +#endif +} + +void end_frame() +{ + PORTB &= ~_BV(PB2); // clock low + _delay_us(1000); +} + +void send_rgb(unsigned char r, unsigned char g, unsigned char b) +{ + send_byte(r); + send_byte(g); + send_byte(b); +} + + +void zero_frame() +{ + unsigned char i; + + for (i = 0; i < STRIP_SIZE; i++) { + send_rgb(0, 0, 0); + } + + end_frame(); +} +