Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h> //Bibloiteki
- #include <avr/io.h>
- //#include <util/delay.h>
- // Define baud rate
- #define F_CPU 8000000 // taktowanie
- #define USART_BAUDRATE 9600 // predkosc bandwith rate
- #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) // wzor ktory Ci przeslalem na wyliczenie baudprescale
- void USART_vInit(void)
- {
- // Set baud rate
- UBRRH = (uint8_t)(51>>8); // musisz to wyliczyc na podstaiwe taktowania twego badziewia
- UBRRL = (uint8_t)51; // to samo
- // Set frame format to 8 data bits, no parity, 1 stop bit
- UCSRC = (0<<USBS)|(3<<UCSZ0); // ustawiasz format na 8 bitowy, ostatni bit przekazuje sygnal
- // Enable receiver and transmitter
- UCSRB = (1<<RXEN)|(1<<TXEN); // wlaczasz odbiornik i nadjanij
- }
- uint8_t USART_vReceiveByte() // odbierz bit
- {
- // Wait until a byte has been received
- while((UCSRA&(1<<RXC)) == 0); // wykonuj dopoki bit nie dotrze
- // Return received data
- return UDR; // zwroc wartosc UDR
- }
- int main(void)
- {
- DDRB = 0xFF ; // jest to liczba 255 czyli ostatni najwiekszy mozliwy bit
- uint8_t u8Data; // zamiana u8data na maly int 8 bitowy
- // Initialize USART
- USART_vInit(); // incjalizacja usart
- // Repeat indefinitely
- for(;;) //forever
- {
- // Echo received characters
- u8Data = USART_vReceiveByte();
- switch (u8Data) { // decode what char just came in
- case 'a' :
- PORTB =0xff;
- break;
- case 'b' :
- PORTB =0x00;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment