One Bit at A Time, Slower and Less No of Cables Required
One Bit at A Time, Slower and Less No of Cables Required
required
CP2101 is used
in USB-UART
conversions
USART in AVR
AVR has built-in USART with following features:
• Full Duplex Operation (Independent Serial Receive and
Transmit Registers)
• Asynchronous or Synchronous Operation
• Master or Slave Clocked Synchronous Operation
• High Resolution Baud Rate Generator
• Supports Serial Frames with 5, 6, 7, 8, or 9 Data bits
and 1 or 2 Stop Bits
The USART of the AVR can be operated in three
modes:
• Asynchronous Normal Mode
• Asynchronous Double Speed Mode
• Synchronous Mode
Setting up the frame….
First, set the baud rate (should be same for both
Tx and Rx)
Second, set the number of data bits to be sent
Next, get the buffer ready
Lastly, enable the transmitter/receiver
Registers in AVR:
UDR0: USART Data Register (16-bit)
Bits 0 to 11 are for USART Baud Rate (UBRRH contains the four MSB’s and UBRRL contains
the eight LSB’s of USART baud rate)
Bits 12 to 15 are reserved for future use.
Program to transmit letter “G” at 9600 baud with 8
data bits, 1 stop bit. With freq=8MHz
#include <avr/io.h>
Void usart_init (void)
{
UCSRB=(1<<TXEN);
UCSRC=(1<<UCSZ!)|(1<<UCSZ0)|(1<<URSEL);
UBRRL=0x33;
}
Void usart_send (unsigned char ch)
{ //wait until UDR is empty, transmit “G”
While (! (UCSRA & (1<<UDRE)) )
UDR=ch;
}
Int main(void)
{ //intializze the USART do forever transmit “G” letter
Usart_init();
While(1)
Usart_send (‘G’);
Return 0;
}
To send the message “The CDAC” to serial Port continuosly
{
Unsigned char str[10]=“The CDAC”;
Unsigned char strLenght=10;
Unsigned char i=0;
While(1)
{
Usart_send(str[i++]);
If (i>=strLength)
i=0;
}
Return 0;
}
To receive bytes of data serially and put them on PORTA. Set the baud rate at
9600, 8-bit data and 1 stop bit
#include <avr/io.h>
Int main(void)
{
DDRA=0xFF;
UCSRB=(1<<RXEN);
UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
UBRRL=0x33;
While(1)
{
While(!(UCSRA & (1<<RXC)));
PORTA=UDR;
}
Return 0:
}