UART (Universal Asynchronous Receiver Transmitter)
It is a way of communication between the microcontroller and the computer system or another microcontroller. There are always two parts to any mode of communication-a Receiver and a Transmitter. Hence, our Atmega can receive data as well as send data to other microcontroller, computer or any other device.
UART: Theory of Operation
Figure 16 illustrates a basic UART data packet. While no data is being transmitted, logic 1 must be placed in the Tx line. A data packet is composed of 1 start bit, which is always a logic 0, followed by a programmable number of data bits (typically between 6 to 8), an optional parity bit, and a programmable number of stop bits (typically 1). The stop bit must always be logic 1.
Most UART uses 8bits for data, no parity and 1 stop bit. Thus, it takes 10 bits to transmit a byte of data.
Figure 17: Basic UART packet format: 1 start bit, 8 data bits, 1 parity bit, 1 stop bit.
BAUD Rate: This parameter specifies the desired baud rate (bits per second) of the UART. Most typical standard baud rates are: 300, 1200, 2400, 9600, 19200, etc. However, any baud rate can be used. This parameter affects both the receiver and the transmitter. The default is 2400 (bauds).
In the UART protocol, the transmitter and the receiver do not share a clock signal. That is, a clock signal does not emanate from one UART transmitter to the other UART receiver. Due to this reason the protocol is said to be asynchronous.
Since no common clock is shared, a known data transfer rate (baud rate) must be agreed upon prior to data transmission. That is, the receiving UART needs to know the transmitting UART’s baud rate (and conversely the transmitter needs to know the receiver’s baud rate, if any). In almost all cases the receiving and transmitting baud rates are the same. The transmitter shifts out the data starting with the LSB first.
Once the baud rate has been established (prior to initial communication), both the transmitter and the receiver’s internal clock is set to the same frequency (though not the same phase). The receiver "synchronizes" its internal clock to that of the transmitter’s at the beginning of every data packet received. This allows the receiver to sample the data bit at the bit-cell center.
The receiver detects the start bit by detecting the transition from logic 1 to logic 0 (note that while the data line is idle, the logic level is high). In the case of 16450 UART, once the start-bit is detected, the next data bit’s "center" can be assured to be 24 ticks minus 2 (worse case synchronizer uncertainty) later. From then on, every next data bit center is 16 clock ticks later. Figure 2 illustrates this point.
Once the start bit is detected,the subsequent data bits are assembled in a de-serializer. Error condition maybe generated if the parity/stop bits are incorrect or missing.
Serial Port of Computer
We will be using Serial Port for communication between the uC and the computer. A serial port has 9 pins as shown. If you have a laptop, then most probably there won’t be a serial port. Then you can use a USB to serial Converter.
If you have to transmit one byte of data, the serial port will transmit 8 bits as one bit at a time. The advantage is that a serial port needs only one wire to transmit the 8 bits.
Pin 3 is the Transmit (TX) pin, pin 2 is the Receive (RX) pin and pin 5 is Ground pin. Other pins are used for controlling data communication in case of a modem. For the purpose of data transmission, only the pins 3 and 5 are required.
The standard used for serial communication is RS-232 (Recommended Standard 232). The RS-232 standard defines the voltage levels that correspond to logical one and logical zero levels. Valid signals are plus or minus 3 to 15 volts. The range near zero volts is not a valid RS-232 level; logic one is defined as a negative voltage, the signal condition is called marking, and has the functional significance of OFF. Logic zero is positive; the signal condition is spacing, and has the function ON.
Now we know that this is not the voltage level at which our microcontroller works. Hence, we need a device which can convert this voltage level to that of CMOS, i.e., logic 1 = +5V and logic 0 = 0V. This task is carried out by an IC MAX 232, which is always used with four 10uF capacitors.
The circuit is shown:
The Rx and Tx shown in above figure (pins 11 and 12 of MAX232) are the Rx and Tx of Atmega 16 (PD0 and PD1 respectively).
Setting up UART in microcontroller
Once our electronic circuit is complete, we can start with coding. To enable UART mode in Atmega16, click on the USART tab in Code Wizard. Now depending upon your requirement, you can either check receiver, transmitter or both. You get a list of options to select from for the baud rate. Baud Rate is the unit of data transfer, defined as bits per second. We will select 9600 as it is fair enough for our purpose, and default setting in most of the applications (like Matlab, Docklight, etc.). Keep the communication parameters as default, i.e., 8 data, 1 stop and no parity and mode asynchronous. You also have option of enabling Rx and Tx Interrupt functions, but we won’t do at this point.
Once you generate and save the code, all the register values are set by CVAVR and you only need to know some of the C functions to transfer data. Some of them are:
putchar()
To send one character to the buffer, which will be received by the device (uC or computer)?
E.g.,
putchar(‘A’); //sends character ‘A’ to the buffer
putchar(c); // sends a variable character c
getchar()
To receive one character from the buffer, which might have been sent by the other uC or the computer. E.g., if we have already defined a variable char c, then
c = getchar(); // receives the character from buffer and save it in variable c
putsf()
To send a constant string. Eg,
putsf(“Robocon Team IIT Kanpur”);
Docklight
To communicate with the computer, you need a terminal where you can send data through keyboard and the received data can be displayed on the screen. There are many softwares which provide such terminal, but we will be using Docklight. Its evaluation version is free for download on internet, which is sufficient for our purpose.
To start with, check the Terminal Settings in Docklight. Go to Tools -> Project Settings. Select the Send/Receive communication channel, i.e., the name by which the serial port is known in your computer (like COM1…). In the COM port settings, select the same values as you had set while coding Atmega 16. So, we will select Baud Rate 9600, Data Bits 8, Stop Bits 1, Parity Bits none. You can select ‘none’ in Parity Error Character.
Click OK.
We are now ready to send/receive data, so, select Run -> Start Communication, or, press F5. If your uC is acting as a transmitter, then the characters it sends will appear in the Communication window of Docklight. E.g.,
putchar(‘K’);
delay_ms(500); // Sends character K after every 500ms
Hence what you get on the screen is a KKKKKKKKKKKKKKKKKKKKKKKKK…….. one K increasing every 500ms. To stop receiving characters, select Run -> Stop Communication, or press F6.
If the receiver option is also enabled in Atmega16, then whatever you type from keyboard will be received by it. You can either display these received characters on an LCD, control motors depending on what characters you send, etc. e.g.,
c = getchar() ; // receive character
lcd_putchar(c); // display it on LCD
if (c==’A’)
PORTA=255; // if that character is A, make all pins of PORTA high.
When you are done with sending data, select Run -> Stop Communication, or press F6.
No comments:
Post a Comment