rendered paste body#include <avr/io.h>
#include <avr/iom8.h>
#include <avr/interrupt.h>
//variables
#define portio PIND
#define portiow PORTD
#define rxled PD7
#define rele PD6
#define ubrr 51
#define MAX_BUFFSIZE 3
volatile short int ovf0cnt;
volatile short int btnsw_right = 0;
volatile unsigned char usart[MAX_BUFFSIZE];
volatile unsigned char usart1 = 0;
unsigned char rx_count = 0;
volatile unsigned char message = 0;
int main(void)
{
cli();
ovf0cnt = 0;
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(1<<TXCIE)|(0<<UDRIE)|(0<<UCSZ2);
UCSRC = (1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL);
UBRRH = (unsigned char) (ubrr>>8);
UBRRL = (unsigned char) ubrr;
TCCR0 = (1<<CS02 | 0<<CS01 | 0<<CS00);
DDRD = (1 << PD7 | 1 << PD6 | 0<<PD5 | 1<<PD4 | 0<<PD2);
PORTD |= (1 << PD5 | 1<<PD4);
sei();
while(1)
{
;
}
}
ISR(TIMER0_OVF_vect)
{
if(ovf0cnt) {
portiow ^= (1 << rxled);
if(--ovf0cnt == 0)
portiow ^= (1 << rele);
}
}
ISR(USART_RXC_vect)
{
if((rx_count < MAX_BUFFSIZE) && !message)
usart[rx_count++] = UDR;
if((rx_count == MAX_BUFFSIZE) && !message)
{
UDR = usart[1];
message = 1;
switch(usart[0])
{
case '1': usart1 = usart[1]; ovf0cnt = usart[2]; message = 1;
switch(usart1) {
case '1': portiow |= (1 << rele); break;
case '2': portiow &= ~(1 << rele); break;
case '3': ovf0cnt = 400; break;
default: UDR = 'C';
} break;
default : UDR = 'X';
}
message = 0;
}
}