Académique Documents
Professionnel Documents
Culture Documents
Home
Ads by Google AVR Project UART Denon AVR Atmel AVR
Home
Atmel AVR Tutorials
Introduction
Getting Started
Making Programmer and Development Board
Making Hello World Project
Digital Input/Output in AVRs
Interfacing Seven Segment Displays
Controlling DC Motors
Working with LCD Modules
Using Internal Peripherals
Analog To Digital Convertor
Introduction To AVR Timers
Timers in Compare Mode
Multiplexed Seven Segment Displays
Interfacing Temperature Sensor LM35
RS232 Communication Basics
RS232 Level Convertor
Using AVR USART
Serial I/O
More ...
Products
Low Cost AVR Dev Board
Microchip PIC Dev Board
xBoard MINI v2.0 (ATmega8)
xBoard v2.0 (ATmega16)
USB AVR Programmer
USB PIC Programmer
USB 8051 Programmer
More ...
Online Store
Forum
Links
Contact Us
Enquiry Form
Payment and Shipping
.Net Serial Library Windows & Window CE RS232 Zmodem, Xmodem, Ymodem www.wcscne t.com
Export Goods from China FedEx™ Launches New Flight betw een India and China. Open an A/c Now ! Fe dEx .Busine ss-Shipping.com
Extend your battery life in your application using PIC MCUs w ith nanoWatt XLP Technology www.m icrochip.com /XLP
Dec-29th-2008
Till now we have seen the basics of RS232 communication, the function of level converter and the internal USART of AVR micro. After
understanding the USART of AVR we have also written a easy to use function to initialize the USART. That was the first step to use
RS232. Now we will see how we can actually send/receive data via rs232. As this tutorial is intended for those who are never used
USART we will keep the things simple so as to just concentrate on the "USART" part. Of course after you are comfortable with usart
you can make it more usable my using interrupt driven mechanism rather than "polling" the usart.
This two functions will demonstrate the use of USART in the most basic and simplest way. After that you can easily write functions that
can write strings to USART.
extremeelectronics.co.in/…/using-the-… 1/25
2/24/2011 Using the USART of AVR Microcontroll…
automatically received by the USART of AVR and put in a buffer and bit in a register (UCSRA) is also set to indicate that data is
available in buffer. Its now your duty to read this data from the register and process it, otherwise if new data comes in the previous one
will be lost. So the funda is that wait until the RXC bit (bit no 7) in UCSRA is SET and then read the UDR register of the USART.
char USARTReadChar()
{
//Wait untill a data is available
return UDR;
}
Please not that the data held in the buffer is not the current data which the USART is busy sending. USART reads data from the buffer to its shift register which it
starts sending and thus the buffer is free for your data. Every time the USART gets it data from buffer and thus making it empty it notifies this to the CPU by telling
"USART Data Register Ran Empty" (UDRE) . It does so by setting a bit (UDRE bit no 5) in UCSRA register.
So in our function we first wait until this bit is set (by the USART ), once this is set we are sure that buffer is empty and we can write new data to it.
UDR=data;
}
Note: Actually their are two separate buffers, one for transmitter and one for receiver. But they share common address. The trick is that all "writes"
goes to the Transmitter's buffer while any "read" you performs comes from the receiver's buffer.
That means if we read UDR we are reading from receivers buffer and when we are writing to UDR we are writing to transmitters buffer.
/*
*************************************************************
extremeelectronics.co.in/…/using-the-… 2/25
2/24/2011 Using the USART of AVR Microcontroll…
E-Mail: me@avinashgupta.com
Date : 29 Dec 2008
Hardware:
ATmega8 @ 16MHz
*/
#include <avr/io.h>
#include <inttypes.h>
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
*/
UCSRC=(1<<URSEL)|(3<<UCSZ0);
UCSRB=(1<<RXEN)|(1<<TXEN);
return UDR;
}
UDR=data;
}
void main()
{
//This DEMO program will demonstrate the use of simple
extremeelectronics.co.in/…/using-the-… 3/25
2/24/2011 Using the USART of AVR Microcontroll…
//Varriable Declaration
char data;
UBRR value = 51
*/
USARTInit(51); //UBRR = 51
//Loop forever
while(1)
{
//Read data
data=USARTReadChar();
*/
USARTWriteChar('[');
USARTWriteChar(data);
USARTWriteChar(']');
}
}
If you are lucky and own a really old PC then you may find a Physical COM port on your PC's back. It is a 9 pin D type male connector. In this case you
have to make a RS232 to TTL converter and connect the MCU to COM port via it.
Those who are not so lucky may buy a Virtual COM port. Again in this case too you need to built a RS232 to TTL converter and connect the
MCU to COM port via it.
extremeelectronics.co.in/…/using-the-… 4/25
2/24/2011 Using the USART of AVR Microcontroll…
CP2102 is single chip USB to UART Bridge by SiLabs. This chip can be used to connect your embedded applications to USB port and enable
them to transfer data with PC. It is the easiest path to build PC interfaced projects, like a PC controlled robot. We have a very good CP2102
module that can be used right out of the box. We have done all PCBs and fine SMD soldering for you.
extremeelectronics.co.in/…/using-the-… 5/25
2/24/2011 Using the USART of AVR Microcontroll…
extremeelectronics.co.in/…/using-the-… 6/25
2/24/2011 Using the USART of AVR Microcontroll…
extremeelectronics.co.in/…/using-the-… 7/25
2/24/2011 Using the USART of AVR Microcontroll…
A PC can have several COM ports, each may have some peripheral connected to it like a Modem. Serial Ports on PC are numbered like COM1, COM2 ... COMn
etc. You first need to figure out in which COM port you have connected the AVR. Only after you have a correct COM port number you can communicate with the
AVR using tools such as Hyperterminal. The steps below shows how to get COM port number in case of Virtual COM Ports.
Select "Properties"
extremeelectronics.co.in/…/using-the-… 8/25
2/24/2011 Using the USART of AVR Microcontroll…
System Properties.
In Hardware tab select "Device Manager" button. It will open up device manager.
extremeelectronics.co.in/…/using-the-… 9/25
2/24/2011 Using the USART of AVR Microcontroll…
Depending on whether you are using a "USB to Serial Converter" or "CP2102 USB/USART Bridge Module" you have to find the port with following name.
Note down the COM port number next to the port name. You need to open this Port in Hyperterminal.
extremeelectronics.co.in/…/using-the-… 10/25
2/24/2011 Using the USART of AVR Microcontroll…
Since this is the introductory article about serial communication, we won't be going in much detail on PC end COM port programming. For this reason we will be
using a ready made software for sending and receiving serial data. I will be showing how to use two different terminal program to exchange data with embedded
application.
Windows Hyperterminal
This is a default terminal program shipped with Windows OS. You can start it from
extremeelectronics.co.in/…/using-the-… 11/25
2/24/2011 Using the USART of AVR Microcontroll…
Hyperterminal is not available in Windows Vista or Windows 7 so you have to use other terminal programs like RealTerm.
On startup it will ask for a connection name. Here we will enter AVR
After that select a COM port you want to use. If you are using USB to serial adaptor please confirm which COM port number it is using. Other COM ports are
usually connected to some device say an Internal modem etc. While some others are Bluetooth COM ports. Don't use them. If you have a physical com port then
most probably it will be COM1. If you select wrong COM port during this step you won't be able to communicate with the AVR MCU and won't get expected
results.
extremeelectronics.co.in/…/using-the-… 12/25
2/24/2011 Using the USART of AVR Microcontroll…
HyperTerminal is ready for communication now! If everything went right HyperTerminal and AVR will talk happily and AVR will send the following message as we
have programmed it.
If the screen shows similar message then you have successfully created a link between PC and your AVR micro. It shows that PC can read the data sent by AVR.
To test if the AVR can also read Hyperterminal, press some keys on PC keyboard. Hyperterminal will send them over COM port to the AVR mcu where AVR will
process the data. In the simple test program this processing includes returning the same data but enclosed inside [ and ], so if you press 'k' then AVR will return [k].
If you are able to see this on PC screen then you are sure that AVR is receiving the data correctly.
That's it! It fully tests the Serial Communication Routine and your hardware setup.
If you are running Windows Vista or Windows 7 then the Hyperterminal Program may not be available. So in place of it you can use Realterm. It can be
downloaded from here.
http://realterm.sourceforge.net/
Start Realterm from its Desktop Icon. You will get a screen similar to this. Increase the Value of Row to 40 to see whole message.
extremeelectronics.co.in/…/using-the-… 13/25
2/24/2011 Using the USART of AVR Microcontroll…
Baud: 19200
Port: Port where you have connected the AVR
Data bits: 8
Parity: None
Stop bits: 1
Hardware Flow Control: None
After setting up Realterm connect the AVR board with COM port and switch it on. Rest process is same as given above for Hyperterminal.
See Also
Home Made AVR devboard - To experiment with AVR micros first make this.
Making a RS232 level converter.
RS232 / USART related
extremeelectronics.co.in/…/using-the-… 14/25
2/24/2011 Using the USART of AVR Microcontroll…
RS232 Basics
Level Conversion
AVRs Inbuilt USART description.
1
tweet
retw eet
3 StumbleUpon
Share
Submit
vote now
This entry was posted on Monday, December 29th, 2008 at 11:52 am and is filed under AVR Tutorials, Code Libraries. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
57 Responses to “Using the USART of AVR Microcontrollers : Reading and Writing Data”
1. 1
Avinash Says:
@Raghu
That will require the knowledge abt the underlying robot hardware like sensor position and number etc …
Hello Avinash
I can send data from my Pc “C#” to Usart ”Atmega32” with RS232 line, it work without problem.
Now I will send the information without RS232 line, I want send the information with Bluetooth. I need a Transmitter and receiver can you recommend me
some who is easy to use?
Why you don’t program in CodeVisionAVR it is much easy? And the program is gratis but you must also use AVR Studio for simulation. But write code is
much easy with CodeVisionAVR.
Percy Villegas.
hi use this code for intrnal oscilator atmega8 8Mhz baud rate 9600. what all must i change? UBRR=51 ? or what change in software AVR Studio
Hello Avinash,
you have used UCSRB=(1<<RXEN)|(1<<TXEN); for no interrupt , what should we use if there is a interrupt
extremeelectronics.co.in/…/using-the-… 15/25
2/24/2011 Using the USART of AVR Microcontroll…
March 9th, 2009 at 4:28 pm
5. 5
Ritesh Says:
Hello Avinash,
you have used UCSRB=(1<<RXEN)|(1<<TXEN); for no interrupt , what should we use if there is a interrupt, Can you plz help me
hello avinash,
there is a program in your tutorial which counts from 0-999 .i want to interface RS232 in the program (USART) so that I can see the increment from 0 to 999
in hyperterminal . I tried to combined your USART program with that but unscessful….
can you please help me out
thanking you
ritesh kumar
Hello,
Does anyone has this kind of program for a AVR STK128 board with an ATmega128.
greetz
hello avinash
nice job avinash, this is the best avr beginers forum i found on internet.
i have a problem with with an usart implementation.
i tried implementing an usart code for a quizbuzzer system (used 8mhz internal clk so i used USARTInit(25); ).The hyperterminal displayed junk values(like “..[
“) instead of the chars “1″ “2″ or “3″,when i used ur functions.
i even implemented the code given above, but the heyperterminal wasn’t accepting any input or displaying any output.
what could be the fault in this case?
is there a way i can rectify it?
i used a pc with vista installed in it and i used a hyperterminal 6.3 version downloaded from the internet(since i couldn’t find hyperterminal on vista)
thanks
teja
Hello AVANISH
Now you can see what I have do with atmega_32 in the factory Talaris “Teller Cash Dispenser” there we regulate a motor. All the code is do in
CodeVisionAVR. You can se the code and one video (10 minutes) about the project here.
http://villegastello.weebly.com/talaris_motorreglering.html
http://video.google.com/videoplaydocid=2332502783057173830
thanks
Percy Villegas
hi avinash
I burned your code on to my atmega16(4 Mhz external) and 4800 baud using avr burnomat with the settings as fuse>>clock options>>external clock>>
ceramic, 3 – 8Mhz. however the hyperterminal shows some garbage value whenever i type character….can you help
Hi Avinash!
extremeelectronics.co.in/…/using-the-… 16/25
2/24/2011 Using the USART of AVR Microcontroll…
Nice Tutorials!
I just tried to build the code in AVR Studio 4, and have a couple of errors due to URSEL. Below is the message screen:
Am I missing a library? I never used ‘C’ before, and barely no anything about assembler….but learning!
@fabelizer
Will try it. I did go to the tiny3213 data sheet and found there was no such designation. Didn’t have time to check the megas though. I do think that will solve
it. Thanks!
-fab
Hi! all…
I copied the source code into avr studio and build it with WinAvr..but the program doesn’t work…can somebody send the .hex file to
romandini.luca@libero.it?? it is so urgent..
P.S. I’m using an atmega8 4MHz external clock 9600 boud..
Hi!
Any plans to post something using interrupt-driven USART routines?
@Jeff
I have the routines ready but I have to create documentation so that I can post it here.
[...] introduction. You should also be familiar with RS232 communication. If you are new to it please see RS232 Serial Communication Tutorial. I also
recommend using wireless link only after you have successfully tried wired RS232 [...]
extremeelectronics.co.in/…/using-the-… 17/25
2/24/2011 Using the USART of AVR Microcontroll…
August 11th, 2009 at 12:02 pm
20. 20
Siddharth Dev Says:
Hello Avinash,
Again a great tutorial!! But “download a pdf version” tab is missing now..
Thank you for this tutorial it was really help full I used it in an atmega32p with small tweeks to enable dual USART communications at two different speeds.
Interrupt driven usart routines will be also very helpful!. Xon/Xoff flow control is something that it been driving me nuts!, do you know where can i get a sample
code? or where can i get a better idea of how to implement it?
i have tried to communicate my bot with pc through uart. i m using avr atmega8.but i m unable to transmit or receive the data. m using 16Mhz crystal.pls rply
hi,
Thank u very much for all the tuts.I tried the above said program but when ever I type a letter in hyperteminal I only get a bunch of 9 C’s
I dont understand.max232 cirkt is working.I looped it back and saw that working.
Please reply to me as soon as possible.Your solution is worth to me.
How do i test serial communication using windows vista (no hyperterminal with vista).
regards,
Ray
Hello Avinash
today i compile the code given by you in usart_demo.c but when i press f it give fþ6-
and with other like that
c >> f&-
g >> æf
now i change MCU freqency to 12MHz,still error is same but echo back in other symbol.
what freqency should i used for propare work of this module
Thanks
If you have Vista and need a terminal google PuttY… a helpfull tool..
Skåååll
Blim
extremeelectronics.co.in/…/using-the-… 18/25
2/24/2011 Using the USART of AVR Microcontroll…
November 29th, 2009 at 5:44 am
29. 29
Avinash Says:
@Rakesh
WHY DON’T U USE THE FREQ THAT THE PROGRAM IS DEVELOPED FOR ???????????
WHY ARE U DISTURBING OTHER WITH YOUR VERY SILLY MISTAKE ???
hey Avinash..
congratulations!! Your AVR series of tutorials are among the best that can be found on the internet. Its hard to believe that these tutorials are free of charge. I
am doing a project with Atmega32. It uses usart to communicate to a ericsson phone. But the problem is I want pc communication at the same time.As the
mobile would take up the usart port how can i implement serial communication for pc?? might be a naive question to ask but I am really confused..
use a second software usart. if u know how usar works (given in details in one of the tuts) then u can easily implement in software . use it for less data intensive
line.
hey avinash !!
Thanx for the quick reply!! you are awesome!! Ya I found out how to use a software UART.Thanks.Cheerz
plz help me
i am using avr atmegs 32 and i am communication with pic microcontroller when i send character values like “ATP” it recognize n respond
and for instance i have to send it data like “0100″ it dosend respond
i am using functions defined above in this tutorial
bt when i send 0100 through hyperterminal the pic controller recognize why does not it recognize integer values from my controller plz helo
Qasim,
You are really not sending an integer value, you are actually sending an ascii value. You must use a function such as atoi (ascii to integer) to convert to an
integer if that is what you want to use. Check an ascii code table to see what integers the characters are really sending, and be sure your code can respond to
them, or convert them.
-fab
avinash,
great explanation! I am currently trying to put this on an ATMega128 board, and it is complaining about alot of things. I fixed alot of them by finding that the
board this code was made for has 1 usart, while the one i am using has 2. however i think i got that working, now it is just complaining about the URSEL. any
ideas?
hi
the above code is working fine on Atmega32 and 16 but not on attiny2313.I have changed the statement
UCSRC=(1<<URSEL)|(3<<UCSZ0);
to
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
pls help…………
extremeelectronics.co.in/…/using-the-… 19/25
2/24/2011 Using the USART of AVR Microcontroll…
February 27th, 2010 at 3:40 pm
37. 37
AvrLabCom Says:
hi….
thanks for this amazing tutorial..
i used the same code to program the ic i am using ATmega8535 running at 8Mhz and with a baud rate of 9600bps.so UBRR=51.But when i type A on
hyperterminal it returns A and not [A].can u plzz tell me what could be the possible problem.
hi…..,
gr8 tutorials…
I ve been working on a project on atmega32…
I wanted to know if code vision avr is compatible with the atmega 32 development board…because the hyperterminal is not responding to the code…pls
help…
night…
why we can’t aplly this USART for a hardware?
(we use two ATnega 8535 with communication serial)
thanks
Hii Avinash,
I used your code to read compass value using I2C on AtMega32 and display this value on my computer’s hyperterminal using rs232. the only problem I have
is that the value is displayed as a character (ASCII), but I want to get this value as an integer or binary number, can you suggest me a way to do this.
thanks.
Answar !!
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
UBRR = ubrr_value;
@Noldor
Both does the same thing.
Hey Avinash! Your tutorials ROCK! Finished the LCD & ADC ones last night. This morning I planned on doing the RS232 one, but I discovered you needed
a max232 ic. All the electronics stores were closed. So I made a plan to make another level converter. I stripped a few vcrs and power supplies looking for a
few resistors, transistors and a diode. Im glad to report that it works PERFECTLY!!! )) Thanks again for all your help! G
Hi Avinash,
I am using ur program to echo the chars. in realterm,but I am getting an error Break status in realterm,I have checked the circuit.Any help will be appreciated.
Thanks
hey Avi,
Ur tutorial helps man,thanks.
Please post on rs485 communication.Like how to transmitt and recieve data..
dear avinash,
Thanks to your project “Using the USART of AVR Microcontrollers : Reading and Writing Data”, it very usefull for me. I have some question regarding that
project, I was build the circuit then burn the firmware to the chip, but I found the problem, baud rate only work in 1200 baud not in 19200 as your program. I
already replace x’tal with the new one 16 MHz, but still can’t work in 19200.
Second question, now I am doing to make project ADC USART AVR to Comm PC, and test with hyperterminal but symbol character show up, how to
convert ansi character to integer??? can you give solution for my problem?
extremeelectronics.co.in/…/using-the-… 21/25
2/24/2011 Using the USART of AVR Microcontroll…
thanks,
best regard
hi bro, i m a gr8 fan of ur tuts. I followed all ur tuts till now successfully,
I have a qn,
Can we use this method to control the robot via PC?
Thank you for the code. Simple and easy to understand, I just started to learn C-code and via your code I did some step forward.
Have you some code example about sending a command to toggle a pin on a port?
Thanks
Br
Tobbe
In your explanation you mention that the virtual com port also require RS232 to TTL converter. If the virtual com port is USB based converter isn’t necessary
as the maximum voltage of USB protocol is +5V.
@SJ
The USB to Serial Adaptors (Virtual Com Ports) has a TTL to RS232 convertor in them. Which makes their i/o RS232 from the TTL voltage of
USB. So again you need one for RS232 to TTL Convertor as you require on a real com port.
hola les hago una pregunta quiero enviar comandos AT a un atmega32 por comunicación serial..!!! como los puedo enviar tengo algo asi pero no me esta
funcionando:
printf(“AT+CMGF=1″);
putchar(13);
sir,
your tutorials have once again proved invaluable to me.however i have a small problem.if i need to control a motor using arrow keys from serial port,i ant do
so,because,arrow keys dont have ascii values.please,i request you to help me out.
Leave a Reply
Name (required)
extremeelectronics.co.in/…/using-the-… 22/25
2/24/2011 Using the USART of AVR Microcontroll…
Website
Submit Comment
Tags
internal peripherals microcontrollers avr programmer Multiplexing programming Timer0 timer1 remote control Seven Segment isr c xBoard usb pic
programmer ProGFX RFID PIC Development Board ks0108 graphic lcd pic programmer pic tutorial pwm Robotics atmega8 mcu
interrupt ir Timer ADC atmega16 mplab pic16f pic16f877 pic18f hi-tech c pic18f4520 atmega32 rs232 USART lcd
microchip pic18f2550 pic18f4550 pic18 AVR pic
Meta
Log in
RSS
Comments RSS
Wordpress
Valid XHTML
Recent Post
Comments
Video
Recent Post
AVR Project – ATmega8 based RPM Meter
Handling Text and Fonts in ProGFX
New Year 2011 Gifts!
My New Camera Tripod !
Introduction to PIC Interrupts and their Handling in C
MicroSD Module
Getting Started with Serial RFID Reader
Happy Diwali !
4×3 Matrix Keypad Interface – AVR Tutorial
VS-2 Servo, Ultrasonic, RFID, Solarcells, Keypads, MicroSD – All New Arrivals!
Comments
extremeelectronics.co.in/…/using-the-… 23/25
2/24/2011 Using the USART of AVR Microcontroll…
paul: Thank you very much man! you saved my sumo robot. Thanks!
Itus: Thank you a good deal! I truly enjoyed reading this.Looking via these posts and the info you?ve...
syed: @AKILA HAVE U COMPLETED THE PROJECT?
broheim: Nice tut
Fred: I installed the ubuntu 10.10 version. error while loading shared libraries:...
Victor Borah: Oh, I almost forgot, Thanks for the New Year Gift too !
Victor Borah: Hi Avinash, Thanks, I received my consignment (my second purchase from your...
Video
eXtreme Electronics
eXtremeElec
Back from New Delhi, EFY Expo!
y esterday · reply
Categories
AVR Development Board
AVR Projects
AVR Tutorials
Chitchat
Code Libraries
Code Snippets
electronics
Hardwares
Microchip PIC Tutorials
News
PIC Development Board
Programming in 'C'
RF
Robotics
Software
Tools
Navigation
Home
Forum
Shop
Links
Subscribe
extremeelectronics.co.in/…/using-the-… 24/25
2/24/2011 Using the USART of AVR Microcontroll…
Email address:
Subscribe
Delivered by FeedBurner
Comments
paul: Thank you very much man! you saved my sumo robot. Thanks!
Itus: Thank you a good deal! I truly enjoyed reading this.Looking via these posts and the info you?ve...
syed: @AKILA HAVE U COMPLETED THE PROJECT?
broheim: Nice tut
Fred: I installed the ubuntu 10.10 version. error while loading shared libraries:...
Victor Borah: Oh, I almost forgot, Thanks for the New Year Gift too !
Victor Borah: Hi Avinash, Thanks, I received my consignment (my second purchase from your...
extremeelectronics.co.in/…/using-the-… 25/25