AVR: How to use serial port in ATMEGA88

Filed in AVR , Articles 0 comments

Article by: Kumail Ahmed, PAF-KIET

So far we have learned to read digital inputs, analog inputs and use interrupts in ATMEGA8. In this article we will be learning a very important and useful  feature if the microcontroller. After reading the article you will be able to send data over the serial port of the controller, to either PC or any RS232 device.

What is Serial Communication/ UART ?

The UART (pronounced you-art) is basically used for asynchronous serial communication between a micro-controller and PC. UART stands for Universal Asynchronous Receiver Transmitter. It defines a protocol which is common for all serial devices. Generally, the first bit is a start bit which is defined as a low; then comes 8 bits of data and lastly a stop bit which is high. Apart from this some UART also contain a parity bit which is used for error-checking.

This concludes the theoretical discussion about UART. Implementing this on an AVR micro-controller is an easy job! To start with, first we have to set the UART Control register, UCR. The most important bits in this register are RXCIE, RXEN and TXEN. RXCIE is the receive interrupt enable pin which allow us to use the UART receive interrupt. Whenever the AVR receives a byte from the PC, the program automatically jumps to a predefined routine known as the ISR (Interrupt Service Routine). RXEN is the Receive Enable bit which enables the micro-controller to accept data on its serial port. TXEN is the transmit enable bit.

The second important register is the UART baud rate register named UBRR. This register is to set the baud rate of serial communication. Baud rate for this particular micro-controller (or any other) is synonymous to bit rate. Hence, 4800 baud basically means 4800 bits/second. The formula to set the baud rate is :

Where BAUD is your required baud rate and Fosc is your micro-controller clock frequency. Most commonly micro-controllers are used at 9600 baud. If we have a oscillator of 6MHz then the value of UBRR would be 38 (in decimal) and 26 (in Hex).

Hardware for Serial Communication

The RS232 is a type of serial communication whose voltage levels range from -15v to +15v. RS232 doesn’t deal with the software protocols. Its just the hardware specifications which are followed in the serial port of PC. The main governing body is USART/UART. They deal with all the baudrate, parity, stop/start bits, timings, encoding and all other software specifications of serial communication.

Good news is that you really don’t have to worry about all this software stuff while setting up communication. All you need is to build a very common voltage level translator. But hey what the heck is that ? Hold on. As I said at the start that PC uses RS232 which works on plus minus fifteen volts. And our lovely AVR features UART (or USART)  which works on zero to five volts range.

So to translate 0-5 Volts communication into -15 to +15v communication, we need to have a level translator such as MAX232.

All you need is to circuit it between microcontroller and PC and a connecting cable, and voila you are done with hardware.

The circuit comprises of :

1 uF Polarized Caps – Qty 4
MAX232 IC – Qty 1
DB9 Female Connector – Qty 1

The Schematic is as follow:

Whenever we want to send some data to the PC we write this in the UDR register. UDR stands for UART Data Register. A small program follows:

USR is another register related to UART communication. It stands for UART status register. It basically tells the current status of the port. This weird looking line while((USR&(1<<TXC)); simply tells the program to stay on here until Transmission on the current byte is not complete. TXC stand for Transmission Complete [Bit].

This program sends a message ‘HELLO’ to the PC which we can easily view on the Hyper-terminal. Then is waits for user input and whatever key the user presses the particular ASCII code is send onto PORTB.

Although this program is very simple but it contains some important points:

  1. We use interrupts for receiving while polling for Transmission. This is because it’s completely useless for the AVR to wait for a user input. This might be an infinite wait and the whole programs halts.
  2. UDR is used in both Receive and Transmitter routines. In the receive routine we read from the UDR into a variable while in the transmission routine(myputchar();) we write data.
  3. Whenever we use the UART serial interface, the RX and TX pins overrides the simple GPIOs.
Posted by hamzaazeem   @   26 May 2010 0 comments
Tags : , , , , ,

Share This Post

RSS Digg Twitter StumbleUpon Delicious Technorati


blog comments powered by Disqus
Previous Post
Next Post