Files
Last update 4 years 6 months
by Tommigems
Filesfirmware | |
---|---|
.. | |
.vscode | |
fatfs | |
.gitignore | |
Makefile | |
i2c.c | |
i2c.h | |
main.c | |
usart.c | |
usart.h |
usart.c#include <avr/io.h> #include <stdlib.h> #include "usart.h" #define USART_BAUDRATE 115200 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1 int main() { USART_init(); for(;;) // continuous loop { unsigned char ReceivedByte = USART_receive(); USART_transmit(ReceivedByte); } } // Initialize USART void USART_init() { UCSR1B = (1 << RXEN1) | ( 1<< TXEN1); // Enable Receiver and Transmitter UCSR1C = (1 << UMSEL11) | (1 << UCSZ11) | (1 << UCSZ10); // use 8-bit character sizes UBRR1L = (uint8_t)(BAUD_PRESCALE); // Load lower 8-bits of the baud rate UBRR1H = (uint8_t)(BAUD_PRESCALE >> 8); // Load upper 8-bits } // function to receive data unsigned char USART_receive(void) { while(!(UCSR1A) & ( 1<< RXC1)) // wait while data is being received return UDR1; // return the value of the received data stored in UDR } // function to send data void USART_transmit(unsigned char data) { while ((UCSR1A & (1 << RXC1)) == 0) // Wait till data is received and is ready to be read from UDR UDR1 = data; // load the 8-bit data into the UDR }