Sunday, June 26, 2016

ATMEGA328P UART0

CONNECTION CONNECT RX AND TX TO TX AND RX OF OTHER DEVICE

//This define the system frequency
#define F_CPU 16000000UL

//Including the required files
#include <avr/io.h>
#include <util/delay.h>



//Transmitting the uart writing the buffer UDR0
int uart_tx(unsigned char d){
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = d;
return 1;
}

//Function to print the the string
int print_uart(unsigned char d[]){
int i;
for( i = 0;d[i] != 0x0a;i++)
uart_tx(d[i]);
return i;
}

//Transmitting the single character
unsigned char uart_rx(){
while(! (UCSR0A & (1<< RXC0)));
return UDR0;

}

//Receiving the string on the uart
void string_rx(unsigned char *strdata){
unsigned char buff;
while(1){
buff = uart_rx();
*strdata = buff;
strdata++;
if(buff == 0x0a)
break;
}


}

int main(void)
{
//buffer to hold the data..
unsigned char buff[10];
UBRR0 = 16;
UCSR0B|= (1<<TXEN0)|(1<<RXEN0);
    while (1)
    {
string_rx(&buff[0]);
print_uart(buff);


    }
}

No comments:

Post a Comment