Arduino pure programming part-6 uart loopback test


Connect the TX and RX of the arduino using a wire


#include <avr/io.h>
#include <avr/interrupt.h>

/*
UDR-->UART Data Register
UCSRA,B,C---> Uart control status register a,b,c
UBRRnL,UBRRnH --> UART Baud Rate Register

*/
ISR(USART_RX_vect)
{
if(UDR0 == 'C'){
PORTB |=1<<5;
}

}
void uart_init(unsigned int ubrr)
{
//ubrrn = fosc/16(Baud) -1.for 9600 = 104
//set baud rate
UBRR0H = (unsigned char)ubrr>>8;
UBRR0L = (unsigned char)ubrr;

//enable the receiver and transmitter and receiver enable interrupt

UCSR0B = (1<<RXEN0) |(1<<TXEN0) | (1<<RXCIE0);

UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); //8-bit


//enable global interrupt
sei();
//wait for udr to be empty
while(!(UCSR0A&&1<<UDRE0));

UDR0 = 'C';

//wait for transmission to complete

while(!(UCSR0A&&1<<TXC0));

while(1);




}


int main()
{
DDRB |= 1<<5;
PORTB &=~(1<<5);
uart_init(104);

return 0;



}


Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto