AVR ATMEGA8535 : USING USART [SERIAL COMM]


Here simple C code to use USART in AVR ATMEGA8535 using AVR-GCC compiler.

We can use Hyper terminal program in windows to test this program, the setting is:

1. Use COM to communicate

2. Baudrate = 9600

3. Stop bit = 1

4. Bits = 8

The step are: initialize – send data or receive data.

The program:

-start-

#include <stdio.h>

#include<avr/io.h>


int main(void) {

//USING 4MHZ CRYSTAL(Fsoc), SET BAUDRATE TO 9600BPS

//CALC UBRRL=(Fsoc)/(16 X BAUDRATE) – 1=(4000000)/(16 x 9600) – 1 = 25

UBRRL = 25;

//URSEL =1, to access UCSRC reg (if URSEL = 0, we access UBRRH register)

//UCSZ0=1 and UCSZ1=1, to select the number of bits of data that is transmited in each frame, Normally the unit of data in MCU is 8BIT

UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);

//RXEN to enable the receiver, TXEN to enable transmitter

UCSRB = (1<<RXEN)|(1<<TXEN);

DDRA = 0xFF;     //set PORTA as output

//infinite branch

while(1){

//Receive data

// RXC =1, receiver has complete reveived data.

while(!(UCSRA&(1<<RXC)));     //Wait until a byte has been received

//Showing data received at PORTA

PortData= UDR;

PORTA = PortData;

//UDRE =1, ready to send data

while(!(UCSRA&(1<<UDRE)) );       // Wait if a byte is being transmitted

// Transmit data

UDR = PortData;

};

}

-end-

links:

USART programming

http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers/

http://www.societyofrobots.com/microcontroller_uart_50_robot.shtml

Serial port and communication

http://extremeelectronics.co.in/avr-tutorials/rs232-communication-the-basics/

About these ads

One Response

  1. wonderful man,keep it up
    and thax for it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: