Electronics Engg Society Page 1 Contents Introduction PC – Serial Port RS-232 Protocol Theory of Operation Setting UART in micro-controller Hyper Terminal Electronics Engg Society Page 2 Introduction: There are many ways to interface your microcontroller to computer. The easiest way is to use UART i.e. Universal Asynchronous Receiver Transmitter. It is a way of communication between the microcontroller and the computer system or another microcontroller. Data exchange can be done using serial or parallel techniques. In parallel communication, the complete byte of data is sent at one time with each bit having a separated dedicated data line. This method is very fast but not cost effective. On the other hand serial communication is somewhat slower as every bit of data is sent serially one by one but is cost effective as only two data lines i.e. transmitter and receiver lines are required (of course a ground line is also required in addition to these two). Serial data communication can be further divided into two categories - synchronous and asynchronous. In synchronous communication transmitter and receiver are synchronised by a clock whereas in asynchronous system no clock pulse is shared between receiver and transmitter. As the name suggests UART is an example of asynchronous communication. Since no common clock is shared, a known data transfer rate (baud rate) must be agreed upon prior to data transmission i.e. the receiving UART needs to know the transmitting UART’s baud rate (and conversely the transmitter needs to know the receiver’s baud rate). Electronics Engg Society Page 3 Serial Communication in PC – The Serial Port: Figure 1.1: Serial Port of a computer We will be using Serial Port for communication between the microcontroller 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. Electronics Engg Society Page 4 RS-232 Protocol: The standard used for serial communication is RS-232 (Recommended Standard 232). The standard defines voltage levels that correspond to logical zero and logical one level. Voltages between plus and minus 3 to 15 volts are allowed voltages. 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. Voltage between plus or minus 3 volts near zero are not allowed. This is not the voltage level at which our microcontroller works. We have two different machines with two different ways to define 0 & 1 and we want to exchange information between them. Consider microcontroller as a French and Computer's Serial Port as an Indian person (obviously no common language in between!). If they want to exchange information they basically need a mediator who knows both the language. In other words, we need a device which can convert serial port’s 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: Electronics Engg Society Page 5 Figure 1.2: MAX 232 circuit Theory of Operation: Figure 1.3 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. Electronics Engg Society Page 6 Figure 1.3: Basic UART Frame Format 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 Electronics Engg Society Page 7 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 bitcell centre. A key concept in UART design is that UART’s internal clock runs at much faster rate than the baud rate. For example, the popular 16450 UART controller runs its internal clock at 16 times the baud rate. This allows the UART receiver to sample the incoming data with granularity of 1/16 the baud-rate period. This "oversampling" is critical since the receiver adds about 2 clock-ticks in the input data synchronizer uncertainty. The incoming data is not sampled directly by the receiver, but goes through a synchronizer which translates the clock domain from the transmitter’s to that of the receiver. Additionally, the greater the granularity, the receiver has greater immunity with the baud rate error. 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 "centre" can be assured to be 24 ticks minus 2 (worse case synchronizer uncertainty) later. From then on, every next data bit centre 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. Electronics Engg Society Page 8 Figure 1.3: Data sampling points by the UART Receiver Using UART in microcontroller: The USART has to be initialized before any communication can take place. The initialization process normally consists of setting the baud rate, setting frame format and enabling the Transmitter or the Receiver depending on the usage. Setting these in ATMEGA16 requires knowledge about USART registers (UCSR, UBRR and UDR) and their programming. Here our CV-AVR code wizard comes to our help. It generates codes for initialising USART in ATMEGA16 automatically! So you need not mess with the AVR Registers. All you have to do is to click some check-boxes and the code wizard will generate all the initialisation codes. Electronics Engg Society Page 9 Using CV-AVR Code Wizard: Create a new project in CV-AVR as usual using code wizard and click on USART tab. 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. Enabling them will Electronics Engg Society Page 10 generate an interrupt on reception and transmission completion. But as we don’t need an interrupt at this point we will not enable interrupts here. 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: 1. putchar: Sends one character to the buffer that can be received by receiving device.e.g. putchar(‘a’); putchar(‘F’); //sends character a to the buffer //sends character F to the buffer 2. 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 Similarly, there are functions like puts() and gets() that work on string in similar fashion. Hyper-Terminal: 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 Hyper-terminal. Its evaluation version is free for download on internet, which is sufficient for our purpose. You can download the evaluation version of hyper-terminal from http://www.hilgraeve.com/htpe/support/htpe7.exe Electronics Engg Society Page 11 To use hyper-terminal, just click on its icon from programs folder in your start menu. In the new connection dialog box that appears, enter a name for your connection and click OK. Figure 1.4 In the next dialog box enter the com port on which your usb to serial connector is working and click OK. To find out the COM port on which your serial connector is working, check the Device Manager in System Properties. Note: The COM port changes every time you plug-in the connector. Electronics Engg Society Page 12 Figure 1.5 In the dialog box that appears enter the values for baud rate, character size of uart data frame, parity bits, number of stop bits and flow control. These values should be same as that you have configured in your microcontroller. However, most of the systems use 9600 baud-rate, 8 data bits and no parity. If you are also using this configuration, click RESTORE DEFAULT button. Electronics Engg Society Page 13 Figure 1.6 Click on OK and you are ready to send and receive data over serial port. Type anything on the keyboard and it will be transmitted over uart. Anything received over UART will appear on the screen. Electronics Engg Society Page 14 Figure 1.7 If you have any suggestion that you would like to share with us feel free to contact at: [email protected] Akash Singh Electronics Engg. IT BHU Electronics Engg Society Page 15
© Copyright 2026 Paperzz