Sie sind auf Seite 1von 25

2/24/2011 Using the USART of AVR Microcontroll…

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

Using the USART of AVR Microcontrollers : Reading and Writing Data

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.

So lets get started! In this section we will make two functions :-

USARTReadChar() : To read the data (char) from the USART buffer.


USARTWriteChar(): To write a given data (char) to 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.

Reading From The USART : USARTReadChar() Function.


This function will help you read data from the USART. For example if you use your PC to send data to your micro the data is

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.

(See the full description of USART registers)

char USARTReadChar()
{
//Wait untill a data is available

while(!(UCSRA & (1<<RXC)))


{
//Do nothing
}

//Now USART has got data from host


//and is available is buffer

return UDR;
}

Writing to USART : USARTWriteChar()


This function will help you write a given character data to the USART. Actually we write to the buffer of USART and the rest is done by USART, that means it
automatically sends the data over RS232 line. One thing we need to keep in mind is that before writing to USART buffer we must first check that the buffer is free or
not. It its not we simply wait until it is free. If its not free it means that USART is still busy sending some other data and once it finishes it will take the new data from
buffer and start sending it.

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.

(See the full description of USART registers)

void USARTWriteChar(char data)


{
//Wait until the transmitter is ready

while(!(UCSRA & (1<<UDRE)))


{
//Do nothing
}

//Now write the data to USART buffer

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.

UDR=some_data; //Goes to transmitter

some_data=UDR; //Data comes from receiver

(See the full description of USART registers)

Sample program to use AVR USART


The following program makes use of the two functions we developed. This program simply waits for data to become available and then echoes it back via transmitter
but with little modification. For example if you send "A" to it, it will send you back "[A]" that is input data but surrounded by square bracket. This program is enough
to test the USART yet easy to understand.

/*

A simple program to demonstrate the use of USART of AVR micro

*************************************************************

See: www.eXtremeElectronics.co.in for more info

Author : Avinash Gupta

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

Suitable level converter on RX/TX line


Connected to PC via RS232
PC Software : Hyper terminal @ 19200 baud
No Parity,1 Stop Bit,
Flow Control = NONE

*/

#include <avr/io.h>
#include <inttypes.h>

//This function is used to initialize the USART


//at a given UBRR value
void USARTInit(uint16_t ubrr_value)
{

//Set Baud rate

UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);

/*Set Frame Format

>> Asynchronous mode


>> No Parity
>> 1 StopBit

>> char size 8

*/

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

//Enable The receiver and transmitter

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

//This function is used to read the available data


//from USART. This function will wait untill data is
//available.
char USARTReadChar()
{
//Wait untill a data is available

while(!(UCSRA & (1<<RXC)))


{
//Do nothing
}

//Now USART has got data from host


//and is available is buffer

return UDR;
}

//This fuction writes the given "data" to


//the USART which then transmit it via TX line
void USARTWriteChar(char data)
{
//Wait untill the transmitter is ready

while(!(UCSRA & (1<<UDRE)))


{
//Do nothing
}

//Now write the data to USART buffer

UDR=data;
}

void main()
{
//This DEMO program will demonstrate the use of simple

//function for using the USART for data communication

extremeelectronics.co.in/…/using-the-… 3/25
2/24/2011 Using the USART of AVR Microcontroll…
//Varriable Declaration
char data;

/*First Initialize the USART with baud rate = 19200bps

for Baud rate = 19200bps

UBRR value = 51

*/

USARTInit(51); //UBRR = 51

//Loop forever

while(1)
{
//Read data
data=USARTReadChar();

/* Now send the same data but but surround it in


square bracket. For example if user sent 'a' our
system will echo back '[a]'.

*/

USARTWriteChar('[');
USARTWriteChar(data);
USARTWriteChar(']');

}
}

Download Sample Program

Running the USART Demo


You can run the above program in a ATmega8, ATmega16, ATmega32 cpu running at 16MHz without any modification. If you are using different clock frequency
you have to change the UBRR value that we are passing to USARTInit() function. See previous tutorial for calculating UBRR value. AVR running the USART demo
program can be interface to PC using following three ways.

Connect to a Physical COM Port.

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.

Connect to a Virtual COM Port.

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.

Virtual COM Port can be connect to USB Port.

extremeelectronics.co.in/…/using-the-… 4/25
2/24/2011 Using the USART of AVR Microcontroll…

RS232 to TTL Converter attached.

Connect Via a Chips like CP2102.

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.

CP2102 Based Module.

extremeelectronics.co.in/…/using-the-… 5/25
2/24/2011 Using the USART of AVR Microcontroll…

CP2102 Module Can be hooked to USB Directly!

Flywires used for interconnects.

extremeelectronics.co.in/…/using-the-… 6/25
2/24/2011 Using the USART of AVR Microcontroll…

Connection is very easy.

Female sides provide easy connection to headers.

xBoard MINI v2.0 with ATmega8 MCU

extremeelectronics.co.in/…/using-the-… 7/25
2/24/2011 Using the USART of AVR Microcontroll…

ATmega8 Connected to CP2102

Complete setup for ATmega to USB Connection

Finding the COM port number of Serial Port

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.

Right Click on "My Computer" icon in Windows Desktop.

My Computer Icon on Windows Desktop

Select "Properties"

extremeelectronics.co.in/…/using-the-… 8/25
2/24/2011 Using the USART of AVR Microcontroll…

System Context Menu

The System Properties will open up. Go to the "Hardware" Tab.

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…

Open Device Manager

In Device Manager Find the Node "Ports (COM & LPT)"

Expand the PORT node in Device Manager

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.

Prolific USB-to-Serial if you are using Bafo USB to Serial Converter.


Silicon Labs CP210x if you are using CP2102 chip.

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…

COM Port Number

COM Port Number

Communication using a Terminal Program on PC.

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

Start Menu->All Programs->Accessories->Communication->Hyperterminal.

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

Create New Connection

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.

Select COM Port

Now setup the COM port parameters as follows.

Bits per second: 19200


Data bits: 8
Parity: None
Stop bits: 1
Flow Control: None

extremeelectronics.co.in/…/using-the-… 12/25
2/24/2011 Using the USART of AVR Microcontroll…

Setting up the COM port

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.

Screenshot of Hyperterminal Showing the message received from AVR

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.

Setting Up Realterm and using it to communicate with AVR

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…

Screenshot of Realterm Showing the message received from AVR

Setup Realterm as follows. Go to the Port Tab and set it as follows.

Baud: 19200
Port: Port where you have connected the AVR
Data bits: 8
Parity: None
Stop bits: 1
Hardware Flow Control: None

Screenshot of Realterm Setup

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 …

January 9th, 2009 at 8:53 am


2. 2
percy villegas t Says:

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.

Thanks for all your help!!!!

Percy Villegas.

February 18th, 2009 at 4:18 pm


3. 3
newman Says:

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

March 5th, 2009 at 6:25 pm


4. 4
Ritesh Says:

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

March 9th, 2009 at 4:29 pm


6. 6
Ritesh Says:

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

March 16th, 2009 at 7:05 pm


7. 7
Nils Says:

Hello,

Does anyone has this kind of program for a AVR STK128 board with an ATmega128.

greetz

March 16th, 2009 at 7:15 pm


8. 8
teja Says:

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

March 31st, 2009 at 11:27 am


9. 9
percy Says:

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

Why you don’t program in CodeVisionAVR? It is easier you know.

thanks

Percy Villegas

April 6th, 2009 at 12:18 pm


10. 10
rajat Says:

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

May 22nd, 2009 at 3:46 pm


11. 11
fabelizer Says:

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:

Build started 31.5.2009 at 22:47:50


avr-gcc -mmcu=attiny2313 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT RS232.o -MF
dep/RS232.o.d -c ../RS232.c
../RS232.c: In function ‘USARTInit’:
../RS232.c:49: error: ‘URSEL’ undeclared (first use in this function)
../RS232.c:49: error: (Each undeclared identifier is reported only once
../RS232.c:49: error: for each function it appears in.)
../RS232.c: At top level:
../RS232.c:95: warning: return type of ‘main’ is not ‘int’
make: *** [RS232.o] Error 1
Build failed with 3 errors and 1 warnings…

Am I missing a library? I never used ‘C’ before, and barely no anything about assembler….but learning!

Thanks for all your great work!!!!


-fab

June 1st, 2009 at 8:19 am


12. 12
Avinash Says:

@fabelizer

try compiling for ATmega8

June 1st, 2009 at 9:58 am


13. 13
fabelizer Says:

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

June 1st, 2009 at 4:55 pm


14. 14
gulab Says:

why 8051 uses standard baud rate of 9600bps????? reply to my mail

June 13th, 2009 at 12:25 pm


15. 15
luca Says:

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..

July 2nd, 2009 at 10:46 pm


16. 16
luca Says:

Thanks for help!!!

July 2nd, 2009 at 10:46 pm


17. 17
Jeff Says:

Hi!
Any plans to post something using interrupt-driven USART routines?

July 13th, 2009 at 6:13 am


18. 18
Avinash Says:

@Jeff

I have the routines ready but I have to create documentation so that I can post it here.

July 13th, 2009 at 8:18 am


19. 19
RF Communication Between Microcontrollers - Part II | eXtreme Electronics Says:

[...] 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..

August 11th, 2009 at 5:45 pm


21. 21
Amaury Says:

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?

August 12th, 2009 at 8:06 am


22. 22
mitul tailor Says:

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

September 20th, 2009 at 12:33 pm


23. 23
pran Says:

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.

October 24th, 2009 at 6:07 pm


24. 24
jig Says:

hi, really excellent beginner’s tutorial.

Thanks for the job boss.

November 2nd, 2009 at 1:15 am


25. 25
Ray Says:

How do i test serial communication using windows vista (no hyperterminal with vista).

regards,

Ray

November 2nd, 2009 at 7:02 am


26. 26
rakesh Says:

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

can you tell me what is wrong here

November 29th, 2009 at 1:55 am


27. 27
rakesh Says:

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

November 29th, 2009 at 2:06 am


28. 28
Blimey Says:

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 ???????????

IT IS CLEARLY WRITTEN IN THE PROGRAM.

WHY ARE U DISTURBING OTHER WITH YOUR VERY SILLY MISTAKE ???

November 29th, 2009 at 8:39 am


30. 30
Shouvik Says:

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..

January 29th, 2010 at 1:40 am


31. 31
Avinash Says:

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.

January 29th, 2010 at 7:49 am


32. 32
Shouvik Says:

hey avinash !!
Thanx for the quick reply!! you are awesome!! Ya I found out how to use a software UART.Thanks.Cheerz

January 29th, 2010 at 8:09 am


33. 33
Qasim Says:

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

January 31st, 2010 at 12:49 am


34. 34
fabelizer Says:

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

January 31st, 2010 at 11:13 pm


35. 35
archerne Says:

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?

February 17th, 2010 at 11:09 am


36. 36
zahid Says:

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:

Hello, here is my C-ode for working with USART on ATTiny2313:

void USART_Init( unsigned int baudrate ) //??????? ????????????? USART


{

UBRRH = (unsigned char) (baudrate>>8);


UBRRL = (unsigned char) baudrate;
UCSRA = (1<<U2X); //???????? ????????
UCSRB = ( ( 1 << RXEN ) | ( 1 << TXEN ) ); //?????????? ?? ????? ? ? ????????? ????? USART
UCSRC = (1<<USBS) | (3<<UCSZ0);

unsigned char USART_Receive( void ) //??????? ?????? ??????


{

while ( !(UCSRA & (1<<RXC)) ); //???????? ?????? ???????

return UDR; //??????? ???????


}

void USART_Transmit( unsigned char data ) //??????? ???????? ??????


{
while ( !(UCSRA & (1<<UDRE)) ); //???????? ??????????? ?????? ??????

UDR = data; //?????? ???????? ??????


}

April 28th, 2010 at 4:38 pm


38. 38
aasma 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.

April 29th, 2010 at 6:01 pm


39. 39
tina Says:

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…

April 30th, 2010 at 3:21 pm


40. 40
teo Says:

night…
why we can’t aplly this USART for a hardware?
(we use two ATnega 8535 with communication serial)
thanks

June 3rd, 2010 at 8:17 pm


41. 41
Hari Says:

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.

June 25th, 2010 at 3:08 am


42. 42
Salvador Says:

Answar !!

You can do that with hyperterminal, they have that there.


Or you can do that in your program.

look here, i have work with atmega 32.


extremeelectronics.co.in/…/using-the-… 20/25
2/24/2011 Using the USART of AVR Microcontroll…
http://flempanboys.weebly.com/talaris_motorreglering.html
//percy

June 26th, 2010 at 1:28 pm


43. 43
Noldor Says:

Many thanks for these tutorials they are awesome!!

but i have clarify this code from your sample program:

In your sample program u wrote this

UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);

instead of this just this (from prev tutorial)

UBRR = ubrr_value;

UBRR is 16bit so UBRRH+UBRRL right?


so your sample program does this in UBRR (XXX is ubrr_value)
0b00000XXX00000XXX right?
so what is writen in UBRR if we write just this
UBRR = ubrr_value
is it the same, something else or did i get it completely wrong?

thx for the answer and keep up this good work

July 21st, 2010 at 1:29 pm


44. 44
Avinash Says:

@Noldor
Both does the same thing.

July 25th, 2010 at 8:22 am


45. 45
Interfacing RFID Reader with AVR MCUs - AVR Tutorial | eXtreme Electronics Says:

[...] Serial I/O [...]

September 20th, 2010 at 7:41 pm


46. 46
Gareth Says:

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

November 28th, 2010 at 1:02 am


47. 47
Jay Says:

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

December 9th, 2010 at 10:57 am


48. 48
uz Says:

hey Avi,
Ur tutorial helps man,thanks.
Please post on rs485 communication.Like how to transmitt and recieve data..

December 17th, 2010 at 12:17 pm


49. 49
hilmansyah Says:

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

December 18th, 2010 at 3:49 pm


50. 50
mKs Says:

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?

December 27th, 2010 at 4:58 pm


51. 51
mKs Says:

can we use it to control the robo via PC?

December 27th, 2010 at 5:02 pm


52. 52
Tobbe Says:

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

December 28th, 2010 at 8:52 pm


53. 53
kaba Says:

AVR and USART


Project with ATMEGA8 and PC software.
In the project is convert character to integer or single…
http://openthermmonitor.ic.cz/

January 7th, 2011 at 9:31 pm


54. 54
SJ Says:

Hi, thanks for your write up.

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.

January 8th, 2011 at 4:25 pm


55. 55
Avinash Says:

@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.

January 14th, 2011 at 10:24 am


56. 56
jose albuja Says:

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);

January 18th, 2011 at 12:45 am


57. 57
vishwanath Says:

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.

January 26th, 2011 at 12:31 am

Leave a Reply

Name (required)

Mail (will not be published) (required)

extremeelectronics.co.in/…/using-the-… 22/25
2/24/2011 Using the USART of AVR Microcontroll…

Website

Submit Comment

Notify me of followup comments via e-mail

Ads by Google New Products Ads by Google


AVR Schematic AVR Assembler
Electronics Tutorials
Tags AVR Basic
AVR C Programming Meta Harmankardon AVR
AVR Program AVR Controller
ATMEGA16 AVR Softw are
New Products

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

AVR Project – ATmega8 based RPM Meter | eXtreme


Electronics http://bit.ly/eYXW Tf
11 day s ago · reply

I bought this item on eBay - SanDisk MicroSDHC Card 16GB


Mobile microSD micro SD 5YW http://shop.ebay.in/1605...
31 day s ago · reply

Check this video out -- PIC18F4520 PW M Demo - LED


Brightness Control http://t.co/nHdUK49 via @youtube
36 day s ago · reply

Join the conversation

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…

Get New Articles Deliverd To Your Inbox!

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...

eXtreme Electronics © 2008-2010 | See Our Privacy Policy

extremeelectronics.co.in/…/using-the-… 25/25

Das könnte Ihnen auch gefallen