UART : Making your own module and PC communication

Share

If you are done with UART between two micro-controllers, I am sure you would like to move on and try and communicate with a computer.

Communication with the serial port of a computer, however is not so straight-forward, since the logic system of the serial communication line differs from that of the micro-controller. The difference is basically in voltage levels. RS-232 is a method for porting the micro-controller logic to serial port logic. Read up on the background of RS-232 for understanding properly.

So, for this, you need a module which will connect the micro-controller and the PC and regulate data communication.A Max-232 chip is the one which converts the logic levels. Lots of circuits are available for it on the internet. You just need to run an image search for “max 232 circuit” or “rs 232” or similar. Here’s the link to the circuit which I used.
Here’s the image:

For making this module, use the following :
1. Right-angled DB9 female connector
2. A millimeter board made especially for the above connector
3. A Max-232 chip and mount
4. Miscellaneous connectors and wires.

All the above are easily available in the market. Try to use them for convenience,especially the first two.

Here’s how my finished module looks like :

Note the two holes on the board and the wire coming out. I purposely looped the wire once like this, because I have seen soldered connections break off due to pulling while use. The loop and the tight wires help to prevent that. Also, try to keep the wire a bit long, so it is convenient to connect it with your MCU.

Well,this is all you need. I will deal with the actual communication part in the next post (once I am done with it myself ).

Share

Data Communication : UART

Share

One useful feature of a micro-controller is that it can send and receive data to and from another micro-controller or computer. This is particularly useful when we are using plenty of micro-controllers and data-communication between them is needed. Another very useful application is that data can be viewed real time as it is being output by the MCU,while developing applications.

There are two ways for communication : SPI and USART. I haven’t done SPI as yet, so here’s a little about USART.

USART :

Introduction :

USART stands for Universal Synchronous Asynchronous Receiver Transmitter. It sends and receives data and is incorporated in the MCU itself. Data can be sent either synchronously and asynchronously. The difference between UART and USART is that in synchronous mode(USART only) the device require both data and clock. The clock is recovered from the data or an external one which is synchronous with data. In asynchronous mode(UART) the device requires only data. The data clock is internally generated and synchronised with start and stop bits embedded in the data received.Lets deal with UART first without worrying about clocks.

UART is not very difficult to understand.Here’s the basic layout :

Hardware and firmware :

On the hardware front, it has 3 pins : a transmit pin, a receive pin and a clock pin(but let’s not talk about that). There are not too many connections here. Just make sure the transmit pin of one chip is connected to the receive pin of the other for both chips.

It has 3 control registers for applying settings according to requirement, a buffer for storing received data or to hold data to be sent. There are seperate buffers for transmitting and receiving, but they can be accessed by the same variable.

Data is sent/received serially and is automatically moved out of the buffers by accessing the data register(during reception) or by writing to the buffer(during transmission).

A complete explanation of the control registers can be found on pages 164 to 167 of the ATMEGA16 datasheet. I am attaching a link to the ATMEGA 16 Datasheet. The pages are very comprehensive and easy to understand. An explanation shouldn’t be necessary. You can however email me or read the AVR-Freaks forums(the latter is preferred 😉 ) for queries.

Writing the code :

I wrote a program to test UART. In this program, I send a string from the first Atmega16 and the same is received by the other Atmega16. It tests the last received character for a value to check if the string was received intact. It is a rather inaccurate assumption on my part that the last character guarantees correct reception, but then, if you are transmitting real-time data, you wouldn’t be able to know or check what you are transmitting anyways.

Click here to download the codes.

Just burn these codes into the MCUs, connect the Rx and Tx pins as above, and you are good to go. Modify the codes if you want the output differently. Adding an LCD output function to this code would be a very good way to do that.

Remember :

1. You cannot transmit more than one character at a time. Please do not try. It will only lead to erroneous output(if any).
2. Please initialise the control registers correctly. A wrongly initialised/ uninitialised control register will lead to erroneous/no output.
3. All settings ( baud rate, clock speeds,parity bits, stop bits) should be the same for both micro-controllers. Micro-controllers with different clock speeds should also work fine, but I recommend changing them anyways. For details, refer to my previous post, Fuse Bits : Changing your MCU speed and JTAG.

Share