Спойлер
#include <avr/io.h>#include <util/delay.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL
#define FOSC 8000000UL
#define BAUD 9600
#define MYUBRR ( FOSC + BAUD * 8UL ) / (16UL * BAUD) -1
#define LED_1 (1<<PC5)
#define LED_2 (1<<PC4)
#define LED_3 (1<<PC3)
#define LED_4 (1<<PC2)
#define LED1_ON PORTC &= ~LED_1
#define LED1_OFF PORTC |= LED_1
#define LED2_ON PORTC &= ~LED_2
#define LED2_OFF PORTC |= LED_2
#define LED3_ON PORTC &= ~LED_3
#define LED3_OFF PORTC |= LED_3
#define LED4_ON PORTC &= ~LED_4
#define LED4_OFF PORTC |= LED_4
static char wiadomosc;
void USART_Init( unsigned int ubrr)
{
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXC);
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
void USART_Transmit( unsigned char data )
{
while ( !( UCSRA & (1<<UDRE)) )
;
UDR = data;
}
unsigned char USART_Receive( void )
{
while ( !(UCSRA & (1<<RXC)) )
;
return UDR;
}
int main (void)
{
DDRC |= LED_1 | LED_2 | LED_3 | LED_4 ;
PORTC |= LED_1 | LED_2 | LED_3 | LED_4 ;
USART_Init(MYUBRR);
sei();
while(1) {
if(wiadomosc)
{
USART_Transmit(wiadomosc);
}
wiadomosc = 0;
}
}
ISR(USART_RXC_vect)
{
wiadomosc=UDR;
switch(wiadomosc)
{
case '1':
LED1_ON;
break;
case '2':
LED1_OFF;
break;
case '3':
LED2_ON;
break;
case '4':
LED2_OFF;
break;
case '5':
LED3_ON;
break;
case '6':
LED3_OFF;
break;
case '7':
LED4_ON;
break;
case '8':
LED4_OFF;
break;
}
}


