checking uart of avr This tutorial is taken from the book
MY EXPERIENCE IN PROGRAMMING AVR MICROCONTROLLER
IN C USING WINAVR/AVRGCC.
you can download it free from the files
section(FILE-AVRBOOK.RAR) of yahoo group-booksbybibinhttp://groups.yahoo.com/group/booksbybibin/
comments about this book on
www.booksbybibin.blogspot.comUART of AVR
It is basically used for asynchronous
communication, a communication with 3 wires(Rx,Tx and ground). So first see the
uart of AVR. See the datasheet for the registers in the UART. So let's go for the first
program which tests your serial port.
Folder: serial1
/*this program is to test the microcontroller uart working or not by sending 0x99 to the
uart and reading it using Rx and sending it to port. Change the Port to PortA or PortB if
you had not disabled JTAG
*/
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include <avr/io.h>
/*----------------------------------------------------------------
-----------------DEFINITIONS------------------------------------
-----------------------------------------------------------------*/
#define Receive_DDR DDRC
#define Receive_PORT PORTC //don't forget to disable JTAG, otherwise change port to PORTB or PORTA
/*----------------------------------------------------------------
-----------------FUNCTIONS---------------------------------------
-----------------------------------------------------------------*/
void InitUART( unsigned char baudrate );
unsigned char ReceiveByte( void );
void TransmitByte( unsigned char data );
void Init_Ports(void);
/*----------------------------------------------------------------
-----------------MAIN FUNCTION------------------------------------
-----------------------------------------------------------------*/
void main()
{
unsigned char a;
//lcd initializationsa
Init_Ports();
InitUART( 25 ); /* Set the baudrate to 2400 bps using a 1MHz crystal */
for(;;) /* Forever */
{
TransmitByte(0x99); /* Echo the received character */
Receive_PORT=ReceiveByte();
}
}
/*----------------------------------------------------------------
------------FUNCTIONS TO Initialize UART--------------------------
-----------------------------------------------------------------*/
void InitUART( unsigned char baudrate )
{
UBRRL = baudrate; /* Set the baud rate */
UCSRB = (UCSRB | _BV(RXEN) | _BV(TXEN) ); /* Enable UART receiver and transmitter */
}
/*----------------------------------------------------------------
------------FUNCTIONS TO READ UART-------------------------------
-----------------------------------------------------------------*/
unsigned char ReceiveByte( void )
{
while ( !(UCSRA & (_BV(RXC))) ); /* Wait for incomming data */
return UDR;/* Return the data */
}
/*----------------------------------------------------------------
------------FUNCTIONS TO WRITE UART-------------------------------
-----------------------------------------------------------------*/
void TransmitByte( unsigned char data )
{
while ( !(UCSRA & (_BV(UDRE))) ); /* Wait for empty transmit buffer */
UDR = data; /* Start transmittion */
}
void Init_Ports(void)
{
Receive_DDR=0xFF; //setting that port for output
Receive_PORT=0XFF; //setting all bits high for starting
}
This program is meant for 1MHz internal oscillator working AVR's. Just short circuit
RX and TX (D0,D1) of AVR and see the value at PORTC. If the value at PORTC is 0x99
then your serial port is working fine.
Troubleshooting
1. See at 1MHz 2400 bps the error is .2%, but if you go for 9600 bps then error is
around 8.3%(refer datasheet baud rates).
2. You should disable the JTAG for acessing PORTC, see fuse bits.
3. Short circuit Rx and Tx.
Now you can confirm that your microcontroller uart is working fine.
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.