Atmega8 Uart Program

Programming atmega8 with its different features: ADC, Timers, PWM, UART, SPI etc. GitHub - NazimBL/ATmega8: programming atmega8 with its different features: ADC, Timers, PWM, UART, SPI etc. See full list on maxembedded.com.

An embedded project/product without a Universal Asynchrounous Receiver Transmiter (UART) interface is unimaginable. Even if the MCU is not talking to another serial device, you'll need it at-least during the development work to speak to your computer. UART also come in handy for firmware upgrade and enabling/disabling product features during it's lifetime. This probably one of the first interfaces you would want to include in your project.
So let's get started! As with the series, we will be using a Atmega32 for this tutorial, other AVRs should work the same way. Do do not forget to look at the datasheet once before trying out with other controllers.

I know you're getting your head around the acronym UART still, but do not worry it is simple. Don't know where the word Universal came from but, Asynchronous makes lot of sense.Figure below shows the typical connection of a serial device with the MCU. There are basically Transmit(Tx) and Receive(Rx) lines and a common ground.

Now imagine we want to send some data as shown below:

The duration of a '1' or '0' is unknown to the receiving device. Hence it asynchronous. This is solved by agreeing upon a common data rate by both the devices, known as baud Rate.

You'll find typical baud rates to be 4800, 9600, 19200, 115200 etc. Atmega32 allows you to select the baud rate you wish. You need to look at the baud rate of the interfacing device to know it's default value. Typically most devices are set to 9600. Do not take my word for it, do check the datasheet.

Let us go ahead and configure the Atmega32 Hardware UART to communicate with the computer and send/receive data strings.

Well, this looks cool isn't it? Just a one include uart.h and one UART_init(9600) function. Well it's dig a little deeper in the init function to see how it is set up.

UCSRA
USART Control and Status Register A
D7D6D5D4D3D2D1D0
RXCTXCUDREFEDORPEU2XMPCM


Atmega8 Uart Program Download

UCSRB
USART Control and Status Register B
D7D6D5D4D3D2D1D0
RXCIETXCIEUDREIERXENTXENUCSZ2RXB8TXB8

Bit 4 - RXEN: Receiver Enable

Atmega8 Uart Programming

Writing this bit to one enables the USART Receiver.

Bit 3 - TXEN: Transmitter Enable

Writing this bit to one enables the USART Transmitter.

UCSRC
USART Control and Status Register C
D7D6D5D4D3D2D1D0
URSELUMSELUPM1UPM0USBSUCSZ1UCSZ0UCPOL

Bit 2:1 - UCSZ1:0: Character Size

The UCSZ1:0 bits combined with the UCSZ2 bit in UCSRC sets the number of data bits ( Character Size) in a frame the Receiver and Transmitter use.

Bit 7 - URSEL: Register Select

The URSEL must be one when writing the UCSRC.

USART Baud Rate Register
UBRRH UBRRL
D15D14D13D12D11:D8D7:D0
URSEL---UBRR[11:8]UBRR[7:0]
Atmega8 Uart Program

Bit 11:0 - UBRR11:0: USART Baud Rate Register

Atmega8 Uart Program Online

This is a 12 bit register which contains the USART baud rate. The UBRRH contains the four most significant bits, and UBRRL contains the 8 least significant bits of the USART baud rate.


The UART_SetBaudRate() function calcuates the values to the set for appropriate registers as below.

The Explore Embedded AVR C library for UART includes several other functions which enable sending/receiving strings, numbers etc as shown below

For those of you, who would like to watch instead of read we have made a video with all the gyan.


Application

Download the complete project folder from the below link: https://github.com/ExploreEmbedded/ATmega32_ExploreUltraAvrDevKit/archive/master.zip


Have a opinion, suggestion , question or feedback about the article let it out here!

Atmega8 Uart Programming

Please enable JavaScript to view the comments powered by Disqus.

djc

Advanced Member level 1

Atmega8 Uart Code

Joined
Jan 27, 2013
Messages
402
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
India
Activity points
4,543

Atmega8 Uart Program Application

Hello all.
I am writing a code for MODBUS protocol using Atmega8 and Mikroc compiler. I have done UART programming. I am able to receive the data and send the data serially over the UART. I used the c code for CRC generation mentioned in the Mod-bus PDF. I tried that code using C compiler. I send manual data '0x82' and received '213F' which is correct as i calculated it manually too. Now my question is while implementing it with the micro controller, when we receive the data through UART which contains address of the slave,function code, data and CRC code, the received data is in ASCII format. how to process this data. Do we need to give this data as it is to the CRC generator function or do we need to process it first to take out each byte and then send it to the CRC function. And in general to work on the UART first we have to minus 48 from each received character and then process it, then convert it into string and then send to LCD or back to UART again. So if i want to see the generated CRC on LCD or UART again, what do i need to do. Please guide me on this. Below is the code.