Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef F_CPU
- #define F_CPU 8000000UL // 8 MHz
- #endif
- #define C3G 0x80; // PORTB
- #define C3R 0x40;
- #define C3B 0x20;
- #define C2G 0x10;
- #define C2R 0x08;
- #define C2B 0x04;
- #define C1G 0x02;
- #define C1R 0x01;
- #define C1B 0x40; // PORTD
- #define C5R 0x20;
- #define C5G 0x10;
- #define C5B 0x08;
- #define C4R 0x04;
- #define C4G 0x01; // PORTA
- #define C4B 0x02;
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- volatile unsigned char counter;
- volatile unsigned char portStateB;
- volatile unsigned char portStateD;
- volatile unsigned char portStateA;
- volatile unsigned char led[31];
- volatile unsigned char fadecount;
- volatile unsigned char fade;
- volatile unsigned char rxSync;
- volatile unsigned char r;
- void USART_Init(void);
- void USARTWriteChar(char);
- int main(void) {
- rxSync = 0x00;
- USART_Init();
- counter=0x00;
- led[0]=0xFF; // prefix
- led[1]=0xFF;
- led[2]=0xFF;
- led[3]=0xFF;
- led[4]=0xFF;
- led[5]=0xFF;
- led[6]=0xFF;
- led[7]=0xFF;
- led[8]=0xFF;
- led[9]=0xFF;
- led[10]=0xFF;
- led[11]=0xFF;
- led[12]=0xFF;
- led[13]=0xFF;
- led[14]=0xFF;
- led[15]=0xFF;
- led[16]=0xFF;
- led[17]=0xFF;
- led[18]=0xFF;
- led[19]=0xFF;
- led[20]=0xFF;
- led[21]=0xFF;
- led[22]=0xFF;
- led[23]=0xFF;
- led[24]=0xFF;
- led[25]=0xFF;
- led[26]=0xFF;
- led[27]=0xFF;
- led[28]=0x00;
- led[29]=0x00;
- led[30]=0x00;
- portStateB = 0x00;
- portStateD = 0x00;
- portStateA = 0x00;
- DDRB = 0xFF;
- DDRD = 0x7C;
- DDRA = 0x03;
- sei();
- // TIMER INIT
- TCCR0A = (1 << CS02)|(CS01); // Prescaler FCPU / 1024
- TCNT0 = 0xFF; // Set timer to initial value 0;
- TCCR0B = (0<<CS01) | (1<<CS00); // prescaler
- OCR0A = 0xFF;
- TIMSK |= (1 << TOIE0); // Overflow interrup enabled
- PORTB = portStateB;
- PORTD = portStateD;
- PORTA = portStateA;
- while (1) {
- }
- return 0;
- }
- ISR (TIMER0_OVF_vect) {
- if (counter == 0xFF) {
- counter = 0x00;
- }
- else {
- counter++;
- }
- portStateB = 0x00;
- portStateD = 0x00;
- portStateA = 0x00;
- // Piiri 1
- //CHANNEL1
- if( counter < led[1] )
- portStateD += C1B;
- if( counter < led[2])
- portStateB += C1R;
- if( counter < led[3])
- portStateB += C1G;
- //CHANNEL2
- if( counter < led[4])
- portStateB += C2B;
- if( counter < led[5])
- portStateB += C2R;
- if( counter < led[6])
- portStateB += C2G;
- //CHANNEL3
- if( counter < led[7])
- portStateB += C3B;
- if( counter < led[8])
- portStateB += C3R;
- if( counter < led[9])
- portStateB += C3G;
- //CHANNEL4
- if( counter < led[10])
- portStateA += C4B;
- if( counter < led[11])
- portStateD += C4R;
- if( counter < led[12])
- portStateA += C4G;
- //CHANNEL5
- if( counter < led[13])
- portStateD += C5B;
- if( counter < led[14])
- portStateD += C5R;
- if( counter < led[15])
- portStateD += C5G;
- PORTB = portStateB;
- PORTD = portStateD;
- PORTA = portStateA;
- }
- void USART_Init(void) {
- // Set baud rate: clock / 16(UBBR + 1) -> 19231
- UBRRL = 25; //25=19200 12=38400
- UBRRH = 0;
- // Reciever, tramsmitter and complete interrupt enabled
- UCSRB = (1 << RXEN) | (1<<RXCIE) | (1<<TXEN);
- // 8bit
- UCSRC = (1<<UCSZ1) | (1<<UCSZ0);
- }
- ISR(USART_RX_vect) {
- r = UDR;
- //USARTWriteChar(r);
- if (r == 0xFF && rxSync == 0x00) {
- rxSync = 0x01;
- return;
- }
- else {
- led[rxSync] = r;
- rxSync++;
- }
- if (rxSync == 0x1C)
- rxSync = 0x00;
- return;
- }
- void USARTWriteChar(char data) {
- // Wait until transitter is ready
- while(!(UCSRA & (1<<UDRE)))
- {
- // Waiting
- }
- // Write to USART buffer
- UDR=data;
- }
Add Comment
Please, Sign In to add comment