• No results found

TALKING TO A PIC MICROCONTROLLER WITH A PC VIA A WINDOWS TERMINAL PROGRAM

In document c What Happens ebook (Page 121-126)

Having the ability to talk to a PIC microcontroller-controlled black box using a personal com­

puter (PC) running a terminal program under Windows (tm) opens up an area of interesting possibilities. The PC can:

• Send data or a command.

• Receive status information.

• Receive data - display data on-screen, print data, save data as a file, massage data with a spreadsheet program, graph data, do math manipulation, etc.

Many PC's running Windows 95 and later have a built-in terminal program called

"HyperTerminal" (tm). If HyperTerminal is not installed on your PC or you do not have the ver­

sion named "Private Edition", you should download a copy from Hilgraeve at hilgraeve.com. At this writing, HyperTerminal Private Edition (tm) is available at no charge for personal use.

HyperTerminal does not have a "Clear Screen" capability. It you start out using HyperTerminal, you will soon recognize the value of the Clear Screen feature and download HyperTerminal Private Edition. From here on, I will simply use the name "HyperTerminal"

What is a terminal? The term comes from the days of the teletype when a terminal looked like a typewrriter and was used to send and receive messages. For our purposes, HyperTerminal allows sending or receiving single ASCII characters, strings of ASCII charactcrs, or text files.

HyperTerminal can easily set up a serial port such as COM2 and use it to communicate with a PIC microcontroller-based black box.

It is not necessary to learn a programming language for the PC although doing so will enable you to create more sophisticated applications. The PC provides the on-screen user interface, file storage on disk, and printer plus data manipulation, graphing and display capabilities. The PIC microcontroller-based black box provides the controller, data acquisition system, instrument or whatever device you want to dream up.

113

"U-TURN" EXPERIMENT

The experiment that follows is an easy way to become familiar with terminal programs and to get a feel for how the PC end of things will work when connected to a PIC microcontroller-con­

trolled black box.

You will need an ultra-simple piece of test equipment made from a 9-pin female D-subminiature solder cup connector and a short piece of wire. The wire connects pins 2 and 3.

9-pin Female D-subminiature Solder Cup Connector

The objective is to send stuff out a PC serial port on the transmit data (TD) line and to have it make a U-turn and come right back in on the receive data line (RD) of the same port.

We will discuss hardware later. Now we will work toward understanding the HyperTerminal program and what can be done with it.

The first objective is to open HyperTerminal and select a group of settings which will work for our applications, and save the file (settings) so we don't have to go through the setup procedure each time we wrant to do something.

The second objective is to use the "U-turn" connector and send and receive a single character at a time.

We will create a communications setup file for use in our experiments.

Open HyperTerminal:

Start>All Programs>wherever it is>HyperTerminal Private Edition.

The Connection Description dialog box will appear.

Create a file name.

Choose an icon, but you will not need it.

Click OK.

The Connect To dialog box appears.

Select the serial port you wish to use (usually COM2).

Connect using: COM2.

Click OK.

The COM2 Properties dialog box appears.

Select the following:

Bits per second: 4800

Data bits: 8

The "filename" - HyperTerminal window is open.

View>Font.

Font: Courier or Courier New. The courier font is a monospace font.

Font Style: Regular Size: 10

File>Properties

The "filename" Properties dialog box appears.

Click on the Settings tab.

Emulation: ANSI

The remaining settings should be the default settings.

Click OK.

File>Save.

115

Strike a letter key. Nothing happens. The screen displays characters received, NOT characters sent.

Turn off your computer. Install the "U-turn" connector at the serial port you are going to use.

Generally this will be COM 2 as most systems have the mouse connected to COM 1. Turn on your computer. Open the settings file you just created in HyperTerminal. The HyperTerminal window should now be open, blank, and a cursor should be blinking in the upper left hand cor­

ner. Now type any character. The character will appear on-screen w^here the cursor was. The character displayed is actually the character received by the terminal program. If you type the letter "a", it will be transmitted out the COM 2 serial port on the TD line, make a U-turn, come back in the same serial port on the RD line and will be displayed on the screen. Note that the character sent is not displayed, the character received is. They happen to be the same in this case because of the U-turn.

Three ASCII control characters are useful for controlling the placement of ASCII alphanumeric characters on the screen of the PC as they are received. This is important because we want the information to be readable and also because we will want data to be formatted to be saved as a useful text file. The three ASCII control characters are:

As an example, a carriage return is sent when the control and "m" keys are pressed simultane­

ously (control m).

As you probably already know, carriage return causes placement of characters on the screen to move to the extreme left side. Line feed causes characters to be placed on the next line down the screen. Horizontal tab means tab over to the right. Theses terms come from the teletype days.

Try experimenting with the first three control characters to get a feel for how they control place­

ment of the characters on-screen (formatting).

To clear the screen, Edit>Clear Screen.

For our PIC microcontroller-based experiments, the microcontroller will send data to the PC where it will appear on the HyperTerminal screen. Examples appear in the Strings and Math And Manipulating Numbers chapters.

If you have two PCs available, you might like to do the following experiment to learn more about serial communication between twro PCs, both running HyperTerminal. A cable wrill be required to conncct the two serial ports. The simplest possible cable which will work consists of two data lines (one for each direction) and a ground line.

PC-TO-PC "2-LANE HIGHWAY" EXPERIMENT

Cable Assembly

Notice that the transmit data (TD) line on computer 1 is connected to the receive data (RD) line on computer 2 and visa versa. You can easily make your own cable assembly using twro 9-pin female D-subminiature connectors and three lengths of wire. Keep the cable as short as possible (8 feet works for me).

Both computers must communicate using the same settings for baud rate, etc. You can start by using the settings used previously in the setup examples.

Baud rate 4800

Data bits 8

Parity None

Stop bits 1

Flow control None

The objective is to establish bi-directional communication between two computers.

We will assume both computers are running HyperTerminal.

To establish bi-directional communication, connect the computers via the serial cable, turn both of them on and bring up HyperTerminal with your settings file in each. When you have the HyperTerminal window open in each computer, type a character in one of the computers. It will appear on the screen of the other computer. Now do the reverse. After you have played a little, clean off the screen in each computer by using Edit>Clear Screen.

117

The hardware side of 2-way communications between a PIC microcontroller and a PC will be described next.

In document c What Happens ebook (Page 121-126)