Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this


  1. 1. E l e c t r o n i c s E n g g S o c i e t y Page 1Interfacing Micro-controllerwith PCElectronics EngineeringSocietyIT-BHU
  2. 2. E l e c t r o n i c s E n g g S o c i e t y Page 2ContentsIntroductionPC – Serial PortRS-232 ProtocolTheory of OperationSetting UART in micro-controllerHyper Terminal
  3. 3. E l e c t r o n i c s E n g g S o c i e t y Page 3Introduction:There are many ways to interface your microcontroller to computer. Theeasiest way is to useUART i.e. Universal Asynchronous ReceiverTransmitter.It is a way of communication between the microcontroller andthe computer system or another microcontroller.Data exchange can be done using serial or parallel techniques. In parallelcommunication, the complete byte of data is sent at one time with each bithaving a separated dedicated data line. This method is very fast but notcost effective. On the other hand serial communication is somewhat sloweras every bit of data is sent serially one by one but is cost effective as onlytwo data lines i.e. transmitter and receiver lines are required (of course aground line is also required in addition to these two). Serial datacommunication can be further divided into two categories - synchronousand asynchronous. In synchronous communication transmitter andreceiver are synchronised by a clock whereas in asynchronous system noclock pulse is shared between receiver and transmitter. As the namesuggests UART is an example of asynchronous communication. Since nocommon clock is shared, a known data transfer rate (baud rate) must beagreed upon prior to data transmission i.e. the receiving UART needs toknow the transmitting UART’s baud rate (and conversely the transmitterneeds to know the receiver’s baud rate).Interfacing ATMEGA-16 with PC
  4. 4. E l e c t r o n i c s E n g g S o c i e t y Page 4Serial Communication in PC – TheSerial Port:Figure 1.1: Serial Port of a computerWe will be using Serial Port for communication between themicrocontroller and the computer. A serial port has 9 pins as shown. If youhave a laptop, then most probably there won’t be a serial port. Then youcan use a USB to serial Converter.If you have to transmit one byte of data, the serial port will transmit 8 bitsas one bit at a time. The advantage is that a serial port needs only one wireto transmit the 8 bits.
  5. 5. E l e c t r o n i c s E n g g S o c i e t y Page 5RS-232 Protocol:The standard used for serial communication is RS-232 (RecommendedStandard 232). The standard defines voltage levels that correspond tological zero and logical one level. Voltages between plus and minus 3 to 15volts are allowed voltages.Logic one is defined as a negative voltage; thesignal condition is called marking, and has the functional significance ofOFF. Logic zero is positive; the signal condition is spacing, and has thefunction ON.Voltage between plus or minus 3 volts near zero are notallowed. This is not the voltage level at which our microcontroller works.We have two different machines with two different ways to define 0 & 1and we want to exchange information between them. Considermicrocontroller as a French and Computers Serial Port as an Indian person(obviously no common language in between!). If they want to exchangeinformation they basically need a mediator who knows both the language.In other words, we need a device which can convert serial port’s voltagelevel to that of CMOS( i.e. logic 1 = +5V and logic 0 = 0V). This task is carriedout by an IC MAX 232, which is always used with four 10uF capacitors. Thecircuit is shown:
  6. 6. E l e c t r o n i c s E n g g S o c i e t y Page 6Figure 1.2: MAX 232 circuitTheory of Operation:Figure 1.3illustrates a basic UART data packet. While no data is beingtransmitted,logic 1 must be placed in the Tx line. A data packet is composedof 1 start bit, which is always a logic 0, followed by a programmablenumber of data bits (typically between 6 to 8), an optional parity bit, and aprogrammable number of stop bits (typically 1). The stop bit must alwaysbe logic 1.Most UART uses 8bits for data, no parity and 1 stop bit. Thus, it takes 10bits to transmit a byte of data.
  7. 7. E l e c t r o n i c s E n g g S o c i e t y Page 7Figure 1.3: Basic UART Frame FormatBAUD Rate: This parameter specifies the desired baud rate (bits persecond) of the UART. Most typical standard baud rates are: 300, 1200,2400, 9600, 19200, etc. However, any baud rate can be used. Thisparameter 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 clocksignal. That is, a clock signal does not emanate from one UART transmitterto the other UART receiver. Due to this reason the protocol is said to beasynchronous.Since no common clock is shared, a known data transfer rate (baud rate)must be agreed upon prior to data transmission. That is, the receivingUART needs to know the transmitting UART’s baud rate (and converselythe transmitter needs to know the receiver’s baud rate, if any). In almost allcases the receiving and transmitting baud rates are the same. Thetransmitter 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 samefrequency (though not the same phase). The receiver "synchronizes" its
  8. 8. E l e c t r o n i c s E n g g S o c i e t y Page 8internal clock to that of the transmitter’s at the beginning of every datapacket received. This allows the receiver to sample the data bit at the bit-cell centre.A key concept in UART design is that UART’s internal clock runs at muchfaster rate than the baud rate. For example, the popular 16450 UARTcontroller runs its internal clock at 16 times the baud rate. This allows theUART receiver to sample the incoming data with granularity of 1/16 thebaud-rate period. This "oversampling" is critical since the receiver addsabout 2 clock-ticks in the input data synchronizer uncertainty. Theincoming data is not sampled directly by the receiver, but goes through asynchronizer which translates the clock domain from the transmitter’s tothat of the receiver. Additionally, the greater the granularity, the receiverhas greater immunity with the baud rate error.The receiver detects the start bit by detecting the transition from logic 1 tologic 0 (note that while the data line is idle, the logic level is high). In thecase 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 synchronizeruncertainty) later. From then on, every next data bit centre is 16 clock tickslater. Figure 2 illustrates this point.Once the start bit is detected, the subsequent data bits are assembled in ade-serializer. Error condition maybe generated if the parity/stop bits areincorrect or missing.
  9. 9. E l e c t r o n i c s E n g g S o c i e t y Page 9Figure 1.3: Data sampling points by the UART ReceiverUsing 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, settingframe format and enabling theTransmitter or the Receiver depending onthe usage.Setting these in ATMEGA16 requires knowledge about USARTregisters (UCSR, UBRR and UDR) and their programming. Here our CV-AVRcode wizard comes to our help. It generates codes for initialising USART inATMEGA16 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 willgenerate all the initialisation codes.
  10. 10. E l e c t r o n i c s E n g g S o c i e t y Page 10Using CV-AVR Code Wizard:Create a new project in CV-AVR as usual using code wizard and click onUSART tab.Now depending upon your requirement, you can either checkreceiver, transmitter or both.You get a list of options to select from for the baud rate. Baud Rate is theunit of data transfer, defined as bits per second. We will select 9600 as it isfair enough for our purpose, and default setting in most of the applications(like Matlab, Docklight, etc.). Keep the communication parameters asdefault, i.e., 8 data, 1 stop and no parity and mode asynchronous. You alsohave option of enabling Rx and Tx Interrupt functions. Enabling them will
  11. 11. E l e c t r o n i c s E n g g S o c i e t y Page 11generate an interrupt on reception and transmission completion. But aswe 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 byCVAVR 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 byreceiving device.e.g.putchar(‘a’); //sends character a to the bufferputchar(‘F’); //sends character F to the buffer2. getchar: To receive one character from the buffer, which might havebeen sent by the other uC or the computer. E.g., if we have already defined avariable char c, thenc = getchar();// receives the character from buffer and save it in variable cSimilarly, there are functions like puts() and gets() that work on string insimilar fashion.Hyper-Terminal:To communicate with the computer, you need a terminal where you cansend data through keyboard and the received data can be displayed on thescreen. There are many softwares which provide such terminal, but we willbe using Hyper-terminal. Its evaluation version is free for download oninternet, which is sufficient for our purpose.You can download the evaluation version of hyper-terminal fromhttp://www.hilgraeve.com/htpe/support/htpe7.exe
  12. 12. E l e c t r o n i c s E n g g S o c i e t y Page 12To use hyper-terminal, just click on its icon from programs folder inyour start menu. In the new connection dialog box that appears, enter aname for your connection and click OK.Figure 1.4In the next dialog box enter the com port on which your usb to serialconnector is working and click OK. To find out the COM port on which yourserial connector is working, check the Device Manager in SystemProperties.Note: The COM port changes every time you plug-in the connector.
  13. 13. E l e c t r o n i c s E n g g S o c i e t y Page 13Figure 1.5In the dialog box that appears enter the values for baud rate, character sizeof uart data frame, parity bits, number of stop bits and flow control. Thesevalues should be same as that you have configured in your microcontroller.However, most of the systems use 9600 baud-rate, 8 data bits and noparity. If you are also using this configuration, click RESTORE DEFAULTbutton.
  14. 14. E l e c t r o n i c s E n g g S o c i e t y Page 14Figure 1.6Click 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.
  15. 15. E l e c t r o n i c s E n g g S o c i e t y Page 15Figure 1.7If you have any suggestion that you would like to share with us feel free tocontact at:akash.singh.ece10@itbhu.ac.inAkash SinghElectronics Engg.IT BHU