Change the world

Control Software

Using the PIC18F4620’s USART module allows your project to:

  • Communicate with a PC via the PC’s COM port
  • Communicate with another microcontroller
  • Interface with a specialized module or device that requires a RS-232 link

The USART module also allows you to do debugging and fault finding when you don’t have a LCD available.  In this case you would write the values to the USART which will send it to a PC’s COM port.  You can then use any RS-232 Terminal Application to see what messages are sent to the PC’s COM port, and also send messages from the PC.  Termite (http://www.compuphase.com/software_termite.htm) is such a free terminal program and was used in the example programs.

 

To allow communication between a microcontroller and a PC, the following circuit should be used:

 

Communication between microcontrollers is established as follows:

 

 

The following example programs are available to demonstrate the basic principles of using the UART module on a PIC18F4620:

ex1_uart_HelloWorld.c

Example program that uses the USART module to write "Hello World!" serially to a PC's COM port and then display a count from 0 to 255 repeatedly.  Run a program called Termite (http://www.compuphase.com/software_termite.htm) to use as a RS-232 terminal on the PC to see what messages are sent to the PC, and to send messages to the PIC.

 

ex2_uart_analog.c

Example that measures an analog voltage on PORTA RA0 (AN0) and transmits the result on the USART as a 10-bit value (0-1023).  Termite can be used on the PC to view the results.

 

ex3_uart_voltage.c

Example that measures an analog voltage on PORTA RA0 (AN0) and transmits the result on the USART as a voltage value (float 0.00 - 5.00).  Termite can be used on the PC to view the results.

 

ex4_uart_sw.c

Example that monitors any change on 8 switches.  When there is a change, the new value of the switches are written to the USART.

 

ex5_uart_leds_simple.c

Example that waits for a value from the USART to write to 8 LEDs.  Use Termite to enter a value from the PC.  The value must be entered a character in Termite, i.e. if you type 'A' and press ENTER, the value on the PIC will be 65 (from the ASCII table) and the LEDs will be 01000001.  Just make sure that in Termite you select Settings -> Transmitted Text -> Append nothing so that it doesn't send any other characters after the text that you type.

 

ex6_uart_leds.c

Example that waits for a command from the USART to control 8 LEDs.  The command must be terminated with a LF (ASCII 10) character to indicate the end of the command message, so in Termite you have select Settings -> Transmitted Text -> Apend LF. The message protocol from the PC is as follows:

 byte (HEX) byte (HEX) byte(HEX)

---------------------------------

| COMMAND  | SETTING  |    LF   |

---------------------------------

when command is 00 (SET_LEDS):

SETTING (00-FF) = LEDs value

when command is 01 (SET_FLASH_DELAY):

SETTING (00-FF) = flash delay of LEDs (0 - 255ms)

when command is 02 (START_FLASH):

SETTING is ignored

when command is 03 (STOP_FLASH):

SETTING is ignored

So if you type in Termite:

00FF <ENTER>, it will switch ON all the LEDs

0164 <ENTER>, it will set the delay to 100ms

02 <ENTER>, it will flash the LEDs

03 <ENTER>, it will stop flashing the LEDs

Note that all values must be entered in CAPITAL for the HexStToByte() functon to work, ie. 2a must be 2A


ex7_uart_leds_sw_analog.c

Example that uses the USART to communicate between the PIC and a PC to control 8 LEDs, read 8 switches, and read an analog input.  The PC will initiate the communication with a command and the PIC will then react to that command or will reply with a message.  The message protocol is as follows:

 byte (HEX) byte (HEX) byte(HEX)  byte(HEX)

--------------------------------------------

| COMMAND  |   DATA1  |   DATA0  |    LF   |

--------------------------------------------

COMMAND  DESCRIPTION

=======  ===========

00       Set the LEDs to the value of DATA1. DATA0 is not used

01       Set the LED flash rate to the value of DATA1.  DATA0 is not used.

02       Flash the LEDs.  DATA1 and DATA0 are not used.

03       Stop flashing the LEDs.  DATA1 and DATA0 are not used.

04       Request the value of the switches. DATA1 and DATA0 are not used.

05       Reply from the PIC with switches value.  DATA1 contains the value. DATA0 is not used.

06       Request the value of the analog input.  DATA1 and DATA0 are not used.

07       Reply from the PIC with analog value.  DATA1 contains the high byte of the 10-bit value and DATA0 the low byte.

 

E.g.

If all the switches are off, and the PC sends the command: 00 <ENTER>, then the reply from the PIC will be:  05FF

                 

E.g.

If the analog value is at maximum (1023 or 0x3FF), and the PC sends the command: 06 <ENTER>, then the reply from the PIC will be:  0703FF

Note that all values must be entered in CAPITAL.

 

ex8_uart_Pic_pic.c

Example that uses the USART to communicate between two PIC18F4620s.  Each PIC board has 8 LEDs, 8 switches, and an analog input.  When the switches change on PIC1, that value will be written to PIC2's LEDs, and vice versa.  PIC1 will also send a request on the USART for the value of PIC2's analog input every +- 1sec.  If this value is above 512, it will flash its LEDs, otherwise it won't.  The operation for PIC2 will be exactly the same with the analogs.  That means the control code in this case is exactly the same for both PICs. The message protocol is the same as was used in ex7_uart_leds_sw_analog.c, but the flash commands won't be used.  Only SET_LEDS, REQUEST_ANALOG and REPLY_ANALOG will be used.