Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * UART test.c
- *
- * Created: 30-Jun-16 9:48:05 PM
- * Author : Lan
- */
- #define F_CPU 506880 // internal fuses set to 8mhz on internal RC oscillator
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #define BAUD 9600
- #define MYUBRR F_CPU/16/BAUD-1
- void USART_Init( unsigned int baud )
- {
- /* Set baud rate */
- UBRRH = (unsigned char)(baud>>8);
- UBRRL = (unsigned char)baud;
- /* Enable receiver and transmitter */
- UCSRB = (1<<RXEN)|(1<<TXEN);
- /* Set frame format: 8data, 2stop bit(USBS bit) */
- UCSRC = (1<<USBS)|(3<<UCSZ0);
- }
- void USART_Transmit( unsigned char data )
- {
- /* Wait for empty transmit buffer */
- while ( !( UCSRA & (1<<UDRE)) )
- ;
- /* Put data into buffer, sends the data */
- UDR = data;
- }
- unsigned char USART_Receive( void )
- {
- /* Wait for data to be received */
- while ( !(UCSRA & (1<<RXC)) )
- ;
- /* Get and return received data from buffer */
- return UDR;
- }
- int main(void)
- {
- USART_Init(MYUBRR);
- char A;
- /* Replace with your application code */
- while (1)
- {
- USART_Transmit(0xAA);
- A = USART_Receive();
- USART_Transmit(A);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement