Half-Duplex Serial

Half-duplex serial communication is what the the Propeller microcontroller sends to the Activity Board’s (original or WX version) serial-to-USB converter chip every time you use a print statement to send a message to the SimpleIDE Terminal.  The term "half-duplex" means that the system is either sending or receiving, not both.  In contrast, "full-duplex" means that the system can send and receive at the same time, which is covered in a different activity.

Half-duplex serial communication is also useful for communicating with a variety of devices.  The Parallax Serial LCD and RFID Reader are two examples.  This tutorial will show you how to use serial communication to send messages to the Parallax Serial LCD.  

 

Setup

Baud rate indicates the number of 1/0 values per second a serial signal transmits and/or receives.  The code example that comes with SimpleIDE sends values commands to the Parallax Serial LCD at a baud rate of 9600 bits per second (bps).  The Parallax serial LCD has 3 baud rate settings, 2400, 9600, and 19200 with a chart underneath that indicates the switch settings for each. 

  • Set the LCD’s baud rate switches to 9600 by setting switch 1 to OFF, and switch 2 to ON.

Circuit

The code example that comes with SimpleIDE happens to use P12 for communication.  Keep in mind that the Propeller chip can use any I/O pin for either half or full-duplex serial communication. 

  • Connect the serial LCD as shown in the schematic/wiring diagram.

If you haven't already installed the latest USB driver, SimpleIDE, or Learn folder, go to Propeller C - Set up SimpleIDE and Propeller C - Start Simple.

 

Test Code

The test code turns the display on, clears it, and then displays the message “Hello LCD!!!”

  • Click SimpleIDE’s Open Project button.
  • Open Hello LCD.side from ... Documents\SimpleIDE\Learn\Examples\Devices\Display. 
  • Click the Load RAM & Run button.
  • Verify that the LCD displays “Hello LCD!!!”

 

How it Works

The simpletools library has a #include in it for simpletext, which has built-in half-duplex serial functions.  To use these functions, your application has to declare serial *deviceNickname for every different serial device the application needs to communicate with.  In this example, there’s just one device that’s declared with serial *lcd.  In the main function, lcd = serial_open(12, 12, 0, 9600) sets the Propeller chip’s receive pin (12), its transmit pin (also 12), the communication mode (0), and the baud rate (9600 bits per second).  The serial_open function also allocates memory for a serial data structure that stores all these settings as well as the actual serial data.  Last but not least, serial_open returns the starting address of this memory, which gets stored in lcd.  From this point forward, code can use lcd with functions like dprint and writeChar to specify which serial connection to send/receive data.  For example, writeChar(lcd, ON) sends the value 22 to the lcd.  Likewise, print(lcd, “Hello LCD!!!”) sends the “Hello LCD!!!” string to the lcd.

/*
  Hello Serial LCD.c
*/

#include "simpletools.h"

serial *lcd;

const int ON  = 22;
const int CLR = 12;

int main()
{
  lcd = serial_open(12, 12, 0, 9600);
 
  writeChar(lcd, ON);
  writeChar(lcd, CLR);
  pause(5);
  dprint(lcd, "Hello LCD!!!");
}

If this code looks different from what came with your SimpleIDE, just copy from here and paste over the code provided in your SimpleIDE folder.

 


Did You Know?

Make it Global — The serial *lcd declaration is global since it is made before any functions.  This is useful because it allows any function in the application to pass the lcd device nickname in calls to simpletext functions like dprint and putChar.

More simpletext functions — Any simpletext function with a text_t *device parameter can be used with half-duplex serial peripherals like the Parallax Serial LCD.  For a complete list of simpletext functions, see …Documents\SimpleIDE\Learn\Simple Libraries\Text Devices\libsimpletext\Documentation simpletext Library.html.

More serial functions — The serial_open function is part of the serial library, which is included by the simpletext library, which is in turn included by the simpletools library.  For a full list of (half-duplex) serial functions, see … Documents\SimpleIDE\Learn\Simple Libraries\Text Devices\libsimpletext\Documentation serial Library.html.

LCD commands — The two constant declarations are values from the Parallax Serial LCD’s product documentation, which is available through the Downloads section on its product page.  This document has a list of values that can be sent to the LCD to make it perform a variety of tasks.  In addition to turning the display on (22) and clearing it (12), there are commands for cursor positioning, custom character definition and display, and even playing musical notes on the LCD’s built-in piezospeaker. 

Half-duplex input instead — If your application is set up so the Propeller chip only receives and does not send serial data, you can put a -1 in the serial_open function's transmit pin parameter, like this: serial someInputDevice = serial_open(7, -1, 0, 9600).


 

Try This

Sending a value to a serial device can be almost as easy as it is with the SimpleIDE terminal.  Here is an example that places the cursor on the fourth character over in the second line, with writeChar(lcd, LINE2 + 4); and then prints n = 123.

  • Save and rename a copy of the program to ...Documents/SimpleIDE/My Projects
  • Add the lines shown below
  • Run the program and verify the result.


 

Your Turn

Here is an excerpt from a program that sends similar messages to the serial LCD and terminal using the device functions.  The simpleterm_close function shuts down all the services that are provided by default, so print, putChar and other functions stop working after this call.  Next, this code manually creates a second half-duplex serial connection to the terminal.  The example here uses serial *term and term = serial_open(31, 30, 115200).  So now there are two device nicknames, lcd and term, and they can be used to tell functions like dprint, writeChar, writeStr and writeDec to communicate with either the terminal or the LCD.  For example, if you want to send a serial message to the LCD, you can use dprint(lcd, ….  If you instead want to send a message to the terminal, use dprint(term, ….

  • Incorporate the code excerpt above into your program.
  • Use a second set of dprint, writeChar and other functions to make the SimpleIDE Terminal display a copy of whatever you send to the LCD.