Strict Standards: Declaration of action_plugin_googleanalytics::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /home/ulcape/www/wiki.ulcape.org/lib/plugins/googleanalytics/action.php on line 6

Strict Standards: Declaration of action_plugin_stripslashes::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /home/ulcape/www/wiki.ulcape.org/lib/plugins/stripslashes/action.php on line 0
CAPE Wiki » tutorials:pic:pic18_usart_transmit_old
 

USART Transmit

The following is geared for the 18F4520. This tutorial will also work with the 18F452.

Part 2 involves getting the 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:

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 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.

USARTpinout.jpg

In the main function, set TRISC to all outputs.

1:  void main()
2:  {
3:
4:       TRISC = 0x00;
5:
6:  }

Now Port C has been opened. Next, its time to open the USART module.

Open USART

According to the C18 Libraries (PDF) file, in order to open the USART port, you must include the usart.h file.

1:  #include <usart.h>

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.

 1:  #include <p18cxxx.h>
 2:  #include <usart.h>
 3:
 4:  void main()
 5:  {
 6:
 7:       TRISC = 0x00;
 8:       OpenUSART( );
 9:
10:  }

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
 1:  #include <p18cxxx.h>
 2:  #include <usart.h>
 3:
 4:  void main()
 5:  {
 6:
 7:       TRISC = 0x00;
 8:       OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
 9:                 USART_EIGHT_BIT & USART_CONT_RX);
10:
11:  }

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.

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.

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
 1:  #include <p18cxxx.h>
 2:  #include <usart.h>
 3:
 4:  void main()
 5:  {
 6:
 7:       TRISC = 0x00;
 8:       OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
 9:                 USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 129);
10:
11:  }

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:

13:  putrsUSART( "Hello World!" );

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.

 1:  #include <p18cxxx.h>
 2:  #include <usart.h>
 3:  #include <delay.h>
 4:
 5:  void main()
 6:  {
 7:
 8:       TRISC = 0x00;
 9:       OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
10:                 USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 129);
11:
12:       while(1)
13:       {
14:            putrsUSART( "Hello World!" );
15:            Delay10KTCYx(200);
16:       }
17:
18:  }

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 MAX232 IC is allows USART data to be converted to what’s known as the 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:

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.

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 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 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.

HTconnection.jpg

Next, choose COM1 from the options listed below and press OK.

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 serial port. Some laptops do not come with serial ports. A typical 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

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.

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>ON TO PART 3 =>></big></center></b>

See Also

duty free alcohol airport duty free cigs uk buy duty free cuban cigars where to buy cosmetics duty free fragrances buy tobacco duty free