Differences

This shows you the differences between two versions of the page.

tutorials:pic:pic18_usart_transmit_old [2010/02/14 18:38] (current)
Line 1: Line 1:
 +====== USART Transmit ======
 +
 +//The following is geared for the 18F4520.  This tutorial will also work with the 18F452.//
 +
 +Part 2 involves getting the [[http://en.wikipedia.org/wiki/USART|USART]] or (Universal asynchronous receiver/transmitter) to work.  There are many applications for USART such as having printed messages on a PC terminal screen and having the ability to "type" custom commands to the PIC.
 +
 +In this section, we will devote our time to getting the USART working and printing %%Hello World!%% to the PC screen.
 +
 +===== Getting Started =====
 +
 +
 +You will need to have the following available:
 +  * The [[http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_C18_Libraries_51297f.pdf|C18 Libraries (PDF)]] file open
 +  * The [[http://ww1.microchip.com/downloads/en/DeviceDoc/39631a.pdf|PIC18F4520 datasheet (PDF)]] file open
 +  * A MAX232A IC and [[http://datasheets.maxim-ic.com/en/ds/MAX220-MAX249.pdf|datasheet (PDF)]]
 +
 +In order, you will
 +  * Open PORT C on the PIC
 +  * Open the USART module inside the PIC
 +  * Wire the MAX232 IC to the PIC and a DB-9 connector
 +  * Open and configure the Hyperterminal program in Windows
 +
 +==== Open PORTC ====
 +
 +According to the [[http://ww1.microchip.com/downloads/en/DeviceDoc/39631a.pdf|PIC18F4520 datasheet (PDF)]], RX and TX are located on pins 25 and 26.  **TX** stands for //transmit// and **RX** stands for //receive//. These 2 pins are used for USART serial communication.  Notice these pins are labeled RC6 and RC7.  The letter **C** stand for Port C on the PIC.  In order to use these pins as USART pins, Port C must be opened for use.
 +
 +{{:tutorials:pic:usartpinout.jpg|USARTpinout.jpg}}
 +
 +In the //main// function, set //TRISC// to all outputs.
 +
 +<code C 1>
 + void main()
 + {
 + 
 +      TRISC = 0x00;
 + 
 + }
 +</code>
 +
 +Now Port C has been opened.  Next, its time to open the USART module.
 +
 +==== Open USART ====
 +
 +
 +According to the [[http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_C18_Libraries_51297f.pdf|C18 Libraries (PDF)]] file, in order to open the USART port, you must include the //usart.h// file.
 +
 +<code C 1>
 + #include <usart.h>
 +</code>
 +
 +Find the section in the libraries file that mentions USART.  There, you will find a function called //OpenUSART//.  This function is called in order to configure the USART module in the PIC.
 +
 +<code C 1>
 + #include <p18cxxx.h>
 + #include <usart.h>
 + 
 + void main()
 + {
 + 
 +      TRISC = 0x00;
 +      OpenUSART( );
 + 
 + }
 +</code>
 +==== Inserting Parameters ====
 +
 +
 +The following parameters will be called inside the parentheses.  For our example, transmit interrupts will be turned off, receive interrupts will be turned off, asynchronous mode is chosen, eight bit is chosen, sync option is ignored, and continuous receive is chosen. //NOTE: These parameters can be changed later if needed.//
 +  * //USART_TX_INT_OFF//
 +  * //USART_RX_INT_OFF//
 +  * //USART_ASYNCH_MODE//
 +  * //USART_EIGHT_BIT//
 +  * //USART_CONT_RX//
 +
 +<code C 1>
 + #include <p18cxxx.h>
 + #include <usart.h>
 + 
 + void main()
 + {
 + 
 +      TRISC = 0x00;
 +      OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
 +                USART_EIGHT_BIT & USART_CONT_RX);
 + 
 + }
 +</code>
 +
 +==== LOW vs HIGH Baud ====
 +
 +At this stage, we have a problem.  Do we use the //USART_BRGH_LOW// paramenter or the //USART_BRGH_HIGH// parameter?  Also, what //SPBRG// number do we use?  How is this determined?
 +
 +The baud rate (speed of transmission) relies on several factors.  In order to select a HIGH baud or a LOW baud, the baud rate must be decided upon.  For our example, we will choose 1.2 kbps (or 1200 baud).  The oscillating speed of the PIC must be known as well.  For our example, we will assume a 10 MHz crystal is used to run the PIC.
 +
 +According to the 18F4520 datasheet, several tables are listed showing several baud rates based on several oscillator speeds.  Knowing **asynchronous mode** was chosen, a **10 MHz** crystal is being used, and **1.2 kbps** is our desired baud rate, we now have to decide on a HIGH or LOW baud rate parameter and decide on a SPBRG number.  Once these decisions are made, //OpenUSART// is complete.
 +
 +The following table shows **BRGH = 0** (LOW baud).  For **1.2 kpbs** and **10 MHz**, the SPBRG number is 129 and the % error is 0.16.
 +
 +{{:tutorials:pic:spbrg1.jpg|spbrg1.jpg}}
 +
 +The following table shows **BRGH = 1** (HIGH baud).  For **1.2 kpbs** and **10 MHz**, there is no SPBRG number and no % error listed. Therefore, HIGH baud is not an option.
 +
 +{{:tutorials:pic:spbrg2.jpg|spbrg2.jpg}}
 +
 +Clearly, the LOW baud selection is a better option since there is no 1.2 kbps option available in the HIGH baud mode.  Now, its time to complete the //OpenUSART// code.  Add the following parameters:
 +  * //USART_BRGH_LOW//
 +  * //129//
 +
 +<code C 1>
 + #include <p18cxxx.h>
 + #include <usart.h>
 + 
 + void main()
 + {
 + 
 +      TRISC = 0x00;
 +      OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
 +                USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 129);
 + 
 + }
 +</code>
 +
 +Finally, the //OpenUSART// function has been called.
 +
 +==== Transmit Characters ====
 +
 +
 +There are multiple ways to transmit characters (printable characters and non-printable characters).  For our example, we will transmit a string of characters using the USART.
 +
 +According to the libraries file, there exists a function called //putrsUSART//.  This funtion allows the programmer to type a sentence and have the sentence transmitted.  For example:
 +
 +<code C 13>
 + putrsUSART( "Hello World!" );
 +</code>
 +
 +This line will transmit //Hello World!// using USART.  Below, in the code, we will transmit this message over and over again using a //while()// loop.  Since this loop will print the message really fast, we will insert a delay immediately after.  Be sure to include //delay.h// at the top of the file.
 +
 +<code C 1>
 + #include <p18cxxx.h>
 + #include <usart.h>
 + #include <delay.h>
 + 
 + void main()
 + {
 + 
 +      TRISC = 0x00;
 +      OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
 +                USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 129);
 + 
 +      while(1)
 +      {
 +           putrsUSART( "Hello World!" );
 +           Delay10KTCYx(200);
 +      }
 + 
 + }
 +</code>
 +
 +This program will open PORTC, open USART, print "Hello World", delay for some time and repeat.  Use MPLAB to build the code, erase the PIC, and program the PIC.  When done, place the PIC back onto the breadboard.  Next, we will work with the MAX232 chip.
 +
 +==== Interface the MAX232A ====
 +
 +
 +The [[http://datasheets.maxim-ic.com/en/ds/MAX220-MAX249.pdf|MAX232]] IC is allows USART data to be converted to what's known as the [[http://en.wikipedia.org/wiki/RS232|RS232]] standard.  This standard is what serial ports on PCs use to communicate with other devices.
 +
 +According to the MAX232 datasheet, a simple wiring diagram is shown.  You will need the following:
 +  * five 0.1 uF [[http://en.wikipedia.org/wiki/Capacitor|capacitors]]
 +  * a MAX232 chip
 +  * a crossover [[http://en.wikipedia.org/wiki/Serial_cable|serial cable]]
 +  * a [[http://en.wikipedia.org/wiki/D-subminiature|DB-9 connector]] (male)
 +
 +{{:tutorials:pic:max232pinout.jpg|MAX232pinout.jpg}}
 +
 +Insert the MAX232 chip onto the bread board and insert the 5 capacitors:
 +  * a capacitor between pin 1 and pin 3
 +  * a capacitor between pin 4 and pin 5
 +  * a capacitor between pin 6 and ground
 +  * a capacitor between pin 2 and power
 +  * a capacitor between power and ground
 +
 +Wire the power into pin 16 and ground into pin 15.  You have completed the setup of the MAX232.
 +
 +  * Connect pin 25 (**TX**) of the PIC to pin 11 on the MAX232
 +  * Connect pin 26 (**RX**) of the PIC to pin 12 on the MAX232
 +
 +Insert a DB-9 or DE-9 connector (male) into the bread board.  If you look carefully inside the connector, you will see the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9 labeled on the plastic.  
 +
 +{{:tutorials:pic:db-9a.jpg|DB-9a.jpg}}
 +
 +Wire the DB-9 connector to the MAX232 chip as listed below:
 +  * Connect pin 3 of the DB-9 connector to pin 13 of the MAX232
 +  * Connect pin 2 of the DB-9 connector to pin 14 of the MAX232
 +  * Connect pin 5 of the DB-9 connector to ground
 +
 +You have completed the setup of the DB-9 connector.  Connect one end of the crossover serial cable to the DB-9 connector and connect the other end of the crossover serial cable to the [[http://en.wikipedia.org/wiki/Serial_port|PC serial port]].  Next, we configure Hyperterminal.
 +
 +==== Configure Hyperterminal ====
 +
 +
 +Hyperterminal is a simple terminal emmulator that allows serial data to be displayed on a screen.  This program also allows text to be typed and transmitted over a serial cable to the PIC.
 +
 +  * //NOTE: This program is usually found under Programs > Accessories > Communications.  For Windows Vista and Windows 7 users, a free open-source terminal program is available online called **[[http://en.wikipedia.org/wiki/PuTTY|PuTTY]]**.//
 +
 +Open up Hyperterminal in Windows. A pop-up box will be displayed asking for a name.  For our example, use any name and press OK.
 +
 +{{:tutorials:pic:htconnection.jpg|HTconnection.jpg}}
 +
 +Next, choose COM1 from the options listed below and press OK.
 +
 +{{:tutorials:pic:com1a.jpg|com1a.jpg}}
 +
 +  * //NOTE: If COM1 is not an option, you will have to try another COM port until you discover which one works.  If no COM port is listed at all, check the back of the computer to see if you have a [[http://en.wikipedia.org/wiki/Serial_port|serial port]].  Some laptops do not come with serial ports.  A typical [[http://www.google.com/search?hl=en&rls=GGLD%2CGGLD%3A2004-07%2CGGLD%3Aen&q=USB-to-serial+adapter|USB-to-serial adapter]] may be needed in order to continue.//
 +
 +Next, we need to choose the correct port settings.  For our example, we chose our baud rate as 1.2 kbps or 1200 baud.  See the following selections:
 +  * Bits per second = 1200
 +  * Data bits = 8
 +  * Parity = None
 +  * Stop bits = 1
 +  * Flow control = None
 +
 +{{:tutorials:pic:port.jpg|port.jpg}}
 +
 +When the selections have been made, press OK.  Hyperterminal is now ready to receive USART data.  Next, we finish up and power the PIC.
 +
 +==== Finished ====
 +
 +
 +Now that everything is setup, apply power to the breadboard, which turns on the PIC and the MAX232 chip. Hyperterminal should display //Hello World!// on the screen.
 +
 +{{:tutorials:pic:hthelloworld.jpg|HThelloworld.jpg}}
 +
 +  * //NOTE: If you don't see anything on the screen, there are several things to trouble shoot.  Be sure both the PIC and MAX232 have power.  Also, try swapping the wires on pin 2 and pin 3 of the DB-9 connector.  Some serial cables are not considered crossover cables.//
 +
 +  * //For advanced troubleshooting, use an oscilloscope and test pin 25 on the PIC and make sure data is being sent.  Next, test pin 14 of the MAX232 and make sure data is being sent.  If no data is being sent, the PIC was not programmed correctly or there may be a software issue.  If data is being sent, check the serial cable and the COM port in Hyperterminal.//
 +
 +  * //For advanced troubleshooting, use a multimeter in "continuity mode" and test each connection between each device on the breadboard searching for an open circuit.//
 +
 +<b><center><big>[[Tutorial-PIC Programming Basics Part 3|ON TO PART 3 =>>]]</big></center></b>
 +
 +
 +===See Also===
 +* [[C18 Tutorials]]
duty free alcohol airport duty free cigs uk buy duty free cuban cigars where to buy cosmetics duty free fragrances buy tobacco duty free