How does Uart send or receive data?

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi 

I am confused on UART. I am trying to unerstand how we send or recive the character via Uart

 

 UART send start bit which is always one "0" bit, then send 8 data  bits, then stop bit which is "1", 

 

I have read that registers and flag are more important in uart communication.

 

Transmitter Register 

Receiver Register 

Transmitter Flag 

Receiver Flag 

 

 just assume I want to transfer the character 'A' from the microcontroller to PC

 

decimal : 65 (A in decimal)
hexa : 41 (A in hexa decimal)
binary : 10000001 (A in binary)

 

What happens when  both device communicate ?

 

I think UART send start bit which is a "0" bit, then check transmitter flag. It can be activate or deactivate. if flag is activate then it send  one bit and again it will check flag is  activate or deactivate so flag is active then uart send next bit continued to check flag  send 8 data bits , then stop bit which is "1",  When all bits are transferred, the flag becomes deactivate.

 

does it really happen in uart ?

 

 

This topic has a solution.
Last Edited: Fri. Jan 24, 2020 - 04:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The operation of the UART is described in the datasheet.

 

There are also Application Notes available which further describe it, and illustrate its use - see the 'Documents' tab on Product Page on the Microchip website

sky33 wrote:
What happens when  both device communicate ?

The UART is full-duplex - this means that it can both send and receive at the same time.

 

The hardware for sending is separate from the hardware for receiving - so they can operate in parallel.

Again, this is described in the datasheet.

 

This is pretty much the same in any microcontroller - not specific to AVR.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sky33 wrote:
does it really happen in uart ?
Yeah. Which is why (95% of the time) no one actually cares what happens on the wires themselves. The two communicating UARTs have a TX register at one end and an RX register at the other. The sending end puts something into its TX. A process of wire wiggling then ensues (the receiver knows when it starts/stops because of the start/stop bits around the data) and finally when the last part of the frame is received at the far end (the stop bit is seen) it sets a flag to say "something new" and the communication is over. 

 

Sure there is silicon there doing a job of "sniffing" 0/1 transitions on the inbound wire, using "start" to start the detection processs, then regular sampling of the line for the agreed number of data bits (then maybe parity) and finally a stop bit but that's all hidden details in the silicon that are there so you don't have to worry about things. All you know is that ever time the RXC flag is seen to become set it means "something new" just arrived. You pick up from (read) UDR and move on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have read the documents and I have few doubts 

 

assume we are sending data from microcontroller to PC. If the data is 1000 0001 for example: 

 

Tx = 1000 0001;

 

Does uart send one bit a time  ?

For example 

Tx = 1

Tx = 0

Tx = 0

Tx = 0

Tx = 0

Tx = 0

Tx = 0

Tx = 1

 

Does it send single byte at a time ?

For example :

Tx = 1000 0001;

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sky33 wrote:
Does uart send one bit a time  ?

 

Its a serial connection with a single wire.  Only one bit at a time goes down the line. 

 

sky33 wrote:
Does it send single byte at a time ?

 

ahh..

 

How about this.  The USART sends one BYTE at a time, one BIT at a time.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

As Andy said in #2 surely what you need is in the datasheet. (Almost?) every AVR that has a UART has a section like this in its datasheet (I simply picked the 328P as it's popular):

 

 

So what part of that is not clear? 

 

I'm guessing you use (as everyone does these days) 8N1 framing. So in the diagram the optional [5][6][7] bits will all be present in the data but there will not be [8] (it's not 9 bit transmission) and there will not be [P] (parity) as the usual use is without parity these days (that tended to be for analog modems where the transmission was "noisy").

 

So maybe start with a "better" test byte to send. I always like 0xD2 because the bit pattern is 11010010 (which is all 4 patterns of 2 bits: 11, 01, 00 and 10). Also it is not palindromic so you can tell whether it is "forwards" or "backwards". Forwards would indeed be 11010010 if you sent bit 7 then bit 6 then bit 5 and so on in "Western world", reading left to right order. If it were sent "backwards" (think Hebrew/Arabic) then it would be 01001011 (aka 0x4B).

 

As it happens the UART does send the bits "backwards" so bit 0 goes out first then bits 1, 2, 3, ....

 

So now with datasheet diagram and a sensible test pattern we might conclude that the signal on the wire would be:

------+       +---+       +---+   +---+---+---+-------
  ^   |       |   |       |   |   |         ^
  ^   +---+---+   +---+---+   +---+         ^
  ^     ^                                    high stop bit
  ^     ^   0   1   0   0   1   0   1   1
  ^     ^
  ^     low start bit
  line idles "high"

 

But, again, why does any of this matter? It's all hidden implementation detail. As a UART user you just ensure that both ends agree on 8N1 framing and some baud rate then you put bytes like 0xD2 into UDR at one end and after a while the RXC flag raises at the other end so you can lift UDR out and pick up the 8 bits from the middle of the sandwich (delivering 0xD2) that actually travelled down the wires.

 

It's D2 -> D2, how it actually gets there doesn't usually matter to anyone (expect on the occasion that it's not D2 that arrives - then you may need to diagnose things - perhaps even look at the 0's and 1's (and their timing) on the wire.

 

Last Edited: Fri. Jan 24, 2020 - 03:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

jgmdesign wrote:
Its a serial connection with a single wire.  Only one bit at a time goes down the line.

Just to be clear, that's a single wire in each direction. So only one bit at a time goes down each line

 

 

jgmdesign wrote:
How about this.  The USART sends one BYTE at a time, one BIT at a time.

Indeed - like this:

 

  1. The program gives one Byte to the UART
  2. The UART does the start bit
  3. The UART shifts out the bits from the Byte - one at a time
  4. The UART does the parity (if required) and stop bit(s)
  5. The UART tells the program that it has finished sending the Byte

 

Again, this is all standard stuff - nothing specific to AVR

 

perhaps this tutorial will help:

 

https://learn.sparkfun.com/tutorials/serial-communication/all

 

See also

 

https://en.wikipedia.org/wiki/Universal_asynchronous_receiver-transmitter

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps take a look at this video:

 

https://www.youtube.com/watch?v=ZzRXKDkMBhA

 

The key bit is around 0:50 to 4:12

 

Once you've watched one, Youtube will suggest many more for you ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you clawson  You have explained very well 

 

We send or  receive character using uart  I do not understand when the flag is set and cleared

 

I think If I want to send character, the Tux flag will set automatically, the flag will be cleared when all the data is transferred.

 

Can you tell me conditions, When the flag is set and cleared in uart communications 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


clawson wrote:
what you need is in the datasheet

In addition to the text that clawson showed, it also has a diagram:

 

 

Here you can see:

  • The  Receiver & Transmitter are completely separate - which is how it can both transmit & receive at the same time;
  • The Data Register - UDRn - connects to the Data Bus, so that it takes a whole byte at a time to transmit, and delivers a complete received byte
  • The TxD and RxD pins connect to the end of the shift register - so they handle a single bit at a time.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


sky33 wrote:
I do not understand when the flag is set and cleared

Again, it's in the datasheet!

 

 

The UDRE flag tells you when the transmit register is Empty - ie, when all the bits have been sent

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


and further described in 19.6.3:

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Before this gets into another L O N G thread running in circles.  How about you(sky33) tell us what it is you are looking to do?  Then we can better guide you to your answers.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
How about you(sky33) tell us what it is you are looking to do?
My experience is that when people start "clutching at straws" like this it's because they've programmed some simple UART code. It won't work and they are now looking for something to explain why.

 

I would take a STRONG look at the clock/baud myself !!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just want to clear my doubt 

 

For example if  I want to send two bytes 

 

Byte 1 = 1   0   0   1   0   1   1   0   

Byte 2 = 0   1   0   1   0   1  0    1

 

 

0   1   0   0   1   0   1   1   0   1  

0   0   1   0   1   0   1  0    1   

 

 

Do we send start and stop bit for each byte as shown in example ?

Last Edited: Fri. Jan 24, 2020 - 05:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do we send start and stop bit for each byte as shown in example ?

Yes , as already posted (see posting #6 & #7)....why are you worried about that?

Have you been using the UART? try it!

WE is not we...the uart does the work...just give it the byte you want to send---it handles the rest.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why is there any doubt about that?

 

Did you watch the video?

 

Did you study the tutorial?

 

Did you read the datasheet description?

 

The transmission of a byte - every byte - consists of start bit, data bits, optional parity bit, stop bit(s).

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
I would take a STRONG look at the clock/baud myself !!

If only there were some quick-reference tips - or maybe a FAQ - about that ...

 

wink

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...