Serial LCD, Two 10-bit A/D, Two TTL Output, Counter, Two DS1820 Interfaces

Serial LCD Kit LCD #105


Sept, '09. This item has been discontinued.


Updated Sept 15, '03 to include examples using the Basic Stamp 2.

Ordering Info

This is a kit. For a similar design which is assembled with a 2X16 LCD and relay, see the LCD #110 Module.


This is the LCD #105 kit assembled on a Jameco JE25 solderless breadboard. The background light was high and thus the 20 X 4 LCD does not appear to be backlit. The MAX232 (closest to the serial port connector) is not required when interfacing with a Basic Stamp, BX24 or PIC. Note the two A/D inputs (green leads), the counter (white) and the two Dallas 1-W runs (yellow and black twisted).

The $14.95 kit includes the MAX232, the 18-pin PIC and all of the resistors and capacitors plus a supply of 22 awg wire and the famous Morgan Logic Probe (not shown) and figures. It does not include the LCD, the Jameco JE25 solderless breadboard, the 7805 power regulator or the serial port connector and cable.

The $24.95 package inludes everything in the $14.95 kit plus the Truly 20X4 LCD with yellow green LED backlight. Also included is pin stock for the 16 LCD terminals and a 39 Ohm (3W) resistor for limiting the backlight current to less than 250 mA.


Introduction.

The LCD #105 Kit consists of a programmed Microchip PIC18F1320 processor in an 18-pin DIP, a MAX232 (16-pin DIP) level converter and associated 1.0 uFd capacitors which may be used in applications requiring RS232 levels and a number of 1/2 watt resistors. The kit also includes lengths of 22 awg solid wire suitable for assembling the kit on a solder less breadboard, the famous Morgan Logic Probe which is used for testing during assembly and documentation which consists of a number of figures.

The kit does not include the source code for the programmed PIC nor does it include an LCD or a printed circuit board.

This design provides an interface between a 9600 baud serial line from a PIC, Basic Stamp, NetMedia BX24 and similar and a character LCD having an HD44780 interface. In addition, the design provides two 10-bit A/D converters, two TTL outputs which may be used to drive relays or similar and two runs to Dallas DS1820 / DS1822 temperature sensors. Up to sixteen DS1820 (DS18S20, DS18B22, DS1822) devices may be accommodated on each of the two runs. The design also includes a counter and a timer which may be used to count the number of events over a specified number of seconds, 1 to 9.

The kit was designed to interface with any 2X16, 2X20, 2X24, 2X40, 4X16 or 4X20 text LCD having an HD44780 interface capable of operating in a four bit transfer mode. The kit may be ordered with a Truly 20X4 Character LCD with Yellow - Green Back light.

The serial interface is TTL. That is, the idle state is a TTL logic one, or near +5 VDC. This is often termed non-inverting or true.

A MAX232 in a 16-pin package and associated capacitors is included in the kit for applications requiring RS232 levels, a logic one of less than -3 VDC and a logic zero of grater than +3 VDC, This would typically be used in an interface with a PC.

The baud rate is fixed at 9600. My feeling is that virtually any application can support 9600 baud and to provide the ability to change the baud rate in firmware is bad design as it opens the possibility of the unit inadvertently assuming a baud rate which is not compatible with the interfacing circuitry.

The processor does not provide for variable contrast nor variable LED back light intensity. Two fixed resistors, 4.7K and 330, are included in the kit to provide nominally 0.3 VDC for contrast. However, the user may wish to use a potentiometer. Back light intensity is controlled with a series limiting resistor.

The processor provides a 64 character buffer. However, to avoid framing errors, it is suggested the user initially provide a five millisecond delay between each character until I get a better handle on the performance.

A source of +5 VDC with a minimum current of 10 mA is required for this design. If the LCDs LED back light feature is used, the back light current must be limited to less than 250 mA.


LCD Commands.

Note that all commands are case sensitive. For example the command ?c is used to set the cursor style and ?C is used to implement the counter function.

If an invalid command is received, no feedback is provided is returned to the controlling processor.

The examples used in this discussion use the Basic Stamp BS2 language format.

Consider;

	SerOut 0, 84, 1, ["Hello?n"]	' output on P0 at 9600 with 1 ms pacing delay
The text "Hello World" is displayed followed by a new line. Note that the character ? is used to indicate a the next character is a command. The ? is followed by the specific command, in this case 'n' which moves the cursor to the beginning of the next line and clears this line.

A list of various commands follows. Note that the commands are case sensitive.

?a - set cursor to home position.
?b - destructive backspace
?f - clear LCD and leave cursor at home position
?h - backspace
?i - forward cursor
?j - up cursor
?l - clear current line and leave cursor at the beginning of the line
?m - carriage return.  Position the cursor at the beginning of the current line.
?n - new line.  Advance to the beginning of the next line and clear this line.
?t - tab.  Advance the cursor one tab.

?? - display the character '?'.
The cursor may be set to any position using the x and y commands;
?y0
?x15
Note that the line number, follows the 'y' command and the column number, consisting of two digits, follows the 'x' command.

Thus;

	SerOut 0, 84, 5, ["?y1?x10Hello"]
positions the cursor at column 10 of line 1 and then prints "Hello".

Note that the line and column numbers begins with 0. Thus, for a 4 X 20 LCD, valid lines are 0 - 3 and valid columns are 00 - 19.

Any of eight user defined characters may be displayed using the digits 0 - 7. Thus;

?5?2
causes user defined character 5 followed by user character 2 to be displayed on the LCD. Defining the user characters is discussed below.

Configuration Commands.

A number of parameters are stored in the processor's EEPROM. This includes the geometry of the LCD, the type of cursor, the number of spaces in a tab and the eight user defined characters. The settings of all of these are displayed briefly on the LCD when the processor boots.

The default is a 20 X 4 LCD, a tab size of 4, a full blinking cursor (3) and the eight special characters consisting of a single horizontal line, two horizontal lines, etc.

All of these parameters may be modified. Note that when the modifications are made, the new values are written to the processor's EEPROM. Thus, the user defined characters and the geometry of the LCD need only be modified one time.

Set LCD Geometry

Setting the geometry defines the configuration of the interfacing LCD.

?Gyxx
For example;
?G216
indicates the LCD is a 2X16 configuration.

Note that appropriate configurations include 2X16, 2X20, 2X24, 2X40, 4X16 and 4X20.

The configuration is important for the processor to properly position the cursor after each text character is displayed and in executing such commands as backspace, up cursor, down cursor, new line, etc.

Set Tab

The tab size may be set;

?s5
In this case, the tab size is set to five. Valid values of the tab are 1 - 8.

When this command is received by the processor, the new tab size is written to EEPROM and this value is used thereafter when executing the ?t command.

When executing the ?t command, the cursor is advanced, and any characters in its path are overwritten with a space. For example, if the cursor is in column 3 and the tab size is 5, the cursor will advance to column 5. Anything in columns 3 and 4 will be replaced with spaces.

Set Cursor Style

The style of the cursor may be set using the ?c command.

?c3
where the number is in the range of 0-3. A 0 configures as no cursor, a 2 as a non blinking cursor and a 3 as a blinking cursor. As with the set geometry and set tab, the style of the cursor is saved to EEPROM.

User Defined Characters.

User defined characters may be defined using the ?D command;

?D300000000001f1f1f
The number after the 'D' is the number associated with the user defined character, in this case user defined character 3. This is then followed by the eight data bytes expressed in two digit hexadecimal. Note that the hexadecimal letters must be lower case.

In this example, lines 0, 1, 2, 3 and 4 consist of no pixels and lines 5, 6 and 7 consist of all five pixels. Thus, when user defined character 3 is displayed using the command ?3, a character consisting of the lower three lines will be displayed.

Each user defined character is saved in EEPROM.

The design provides a 64 byte serial receive buffer. However, be careful. If one is defining all eight user defined characters, this involves sending 19 * 8 or 152 characters. Writing each of the eight bytes to EEPROM requires 15 ms or more and thus, one can easily over run the buffer. Rather, provide a one second delay after defining each character. For example;

	serout 0, 84, 1, ["?D000000000000001f"]
	delay 1000		' to assure time for saving to EEPROM
	serout 0, 84, 1, ["?D1000000000001f1f"]
	delay 1000

Direct Control of the LCD.

Commands may be directly passed to the LCD using the ?! command;

?!01
In this example the command 01 is sent directly to the LCD which clears the LCD.

A word of caution. With all other commands the program keeps track of the current cursor position. This is not done with commands sent directly to the LCD using the ?! command. Thus, if the user configures the LCD such that the cursor is located at some point, subsequent line feeds and similar will not work correctly as the program does not know the current cursor location.

Clearly, if the ?! command is used to place the LCD in a mode such that the cursor is decremented with each character or the display itself is scrolled, the subsequent operation of the LCD which assumes an incrementing cursor and a fixed display will give unpredictable results. If you do use this capability, it is up to you to keep track of the cursor location. If you then desire to use the ? style commands which sets the cursor to home; e.g., ?a (home cursor) or ?f (clear LCD) or similar. The program then knows the current position of the cursor.

TTL Outputs

The processor provides two outputs which may be used to control an external transistor, FET or relay which may brought high or low using the ?H and ?L commands.

?H0
?L1
In the above, output 0 is brought high and output 1 is brought low.

The source and sink current for each of these should be limited to 15 mA.

The state of either of the outputs may be read using the ?S command. For example

?S0
The LCD unit then responds with the state of the output followed by a new line consisting of a carriage return and a linefeed;
1

Analog to Digital Converters

Two ten bit analog to digital converters are provided. Note that the processor's positive supply and ground are the high and low references for the conversion. An A/D reading will be performed on the specified A/D in response to the ?A command.

?A0
The LCD unit responds with the decimal value in the range of 0 to 1023;
997
Note that the voltage may be calculated as 997 / 1024 * V_supply where V_supply is nominally 5.0 VDC.

Dallas DS1820 Temperature Sensor Interface.

The LCD unit provides two twisted pair runs, each of which may accommodate up to sixteen DS18S20s or DS18B20s or any mix of the two with a maximum of 200 feet of twisted pair cable.

Temperatures on either of the runs are performed in response to the ?T command; ?T0 The unit responds with the temperature measurements;


	0 27.50 10 f33a
	1 26.67 22 3b3b
	2 25.32 28 3cd2
	3 -5.26  etc
	4 3.04 etc
In this example, four devices were found on run 0. As each was found, it was assigned a sequential number in the range of 0 - F. This is followed by the temperature in degrees C, followed by the device type. A 10 indicates a DS18S20 was found, 22 a DS1822 and 28, a DS18B20 . This is then followed by a 16-bit serial number which is assigned specifically to the DS1820 device.

A few notes;

Counter.

The counter function is implemented using the ?C command;

?C3
The number of TTL transitions on input T0CKI are counted over the specified number of seconds, in this case, three seconds. The design provides for 1 to 9 seconds.

The unit responds with the number of counts expressed as a decimal number which occurred over the specified period of time.

1234
The maximum number of counts is 65,535.

When the processor is booted, the number of counts is set to zero and the counter is enabled. The counter may be read at any time using the ?C0 command. An application that comes to mind is in counting tipping bucket pulses in a rainfall gauge. However, it is important to note that value of this counter is not continually written to EEPROM and if power is lost, the processor is rebooted and the number of counts is initialized to zero.


Assembly

Content of the Kit.

Please take a few seconds to acquaint yourself with the content of the kit.

It consists of;

1 	Programmed Microchip PIC18F1320 Processor (18-pin DIP).
1	MAX232 (HIN232) RS232 Level Translator (16-pin DIP).
4	1.0 uFd capacitors.
3 	4.7K Resistors (Yellow, Violet Red) for use on /MCLR and the two DS1820 runs.
1	100K Resistor (Brown, Black, Yellow) for use on the counter input.

1 	4.7K Resistor for LCD contrast.
1	330 Ohm (Orange, Orange, Brown) for LCD contrast.

1	10K Resistor (Brown, Black, Orange) for testing the A/D converter.
1	4.7K Resistor for testing the A/D converter.

1	5V LED with integrated series limiting resistor for testing the TTL outputs.

1	Morgan Logic Probe.  Note that this is a simple device which indicates if a point is at a logic one (LED on), logic zero (LED off), pulsing (LED flashing).  It also provides sources of nominally 1 and 10  pulses per second.

1	Bundle of 22 awg solid wire suitable for use with a solder less breadboard.

Assembly.

Wire the circuitry as shown in Fig #1, LCD Overview.

Note that the programmed PIC is an 18-pin DIP device with terminals 1-9 on one side and terminals 10 (across from terminal 9) through 18 on the other side. Integrated circuits are descendants of vacuum tubes and the terminal assignment is in a circular counter clockwise fashion as viewed from the top.

The layout of the LCD terminals is illustrated in Fig #3 - LCD Terminal Assignment. It has been my experience that most LCDs have either an array of 14 terminals arranged in a single line or 2 lines of 7 terminals. If the LCD has an LED back light there may also be terminals 15 and 16. However, occasionally, I have seen other arrangements and it is best to consult the LCD data sheet for the LCD terminal numbers.

Note that there is a 4.7K pull up resistor to +5 VDC on the /MCLR terminal (term 4 of the PIC processor). The processor may either be reset by applying power, or by momentarily grounding the /MCLR terminal.

Prior to applying +5 VDC and GRD to the circuit, carefully check that +5 VDC is on terminal 14 of the PIC and terminal 2 of the LCD. Carefully, check to verify ground is on terminal 5 of the PIC and terminal 1 of the LCD.

Apply power and verify the LCD displays the geometry of the LCD, the tab size, eight user defined characters and a blinking cursor. The unit is shipped with a 4 X 20 geometry, a tab size of 4, user defined characters of a single row of pixels, two rows, three rows, etc and a blinking cursor.

This screen message appears for about two seconds and the LCD is cleared and the cursor is positioned at the home position. The LCD is now awaiting the receipt of characters to display. Momentarily ground the /MCLR (term 4) on the PIC processor to reboot the processor and the message will again appear for two seconds.

If the message does appear, it verifies that both the processor and the LCD are operational and the associated wiring is correct.

Troubleshooting the LCD circuitry

If the screen message does not appear.

Assembly Continued.

The easiest test tool in verifying the serial capabilities of the LCD is a PC or similar using a terminal emulation package such as HyperTerm. This has typically been a part of the Windows' distributions. It may also be downloaded from Hilgraeve (http://www.hilgraeve.com/htpe/). Configure the port as a direct connection to the COM port, 9600 baud, 8-N-1, no flow control. Note that if you do not have a serial port, consider a USB - RS232 converter.

Using the PC has the advantage of allowing you to verify each command by simply typing the command from the keyboard.

First a few words about RS232 communication. Normally when the line is idle, the line is at a TTL logic one (near +5VDC). A start bit, is a TTL logic zero pulse for one bit time (1/9600 or about 102 usecs). This is then followed by each of the eight bits followed by a return to idle, a TTL logic one.

However, the actual transmission uses levels which are less than -3.0 VDC (typically -8.0) for a logic one and greater than +3 VDC (typically +8.0) for a logic zero. This level inversion and shift is implemented in a PC or similar using a circuit similar to the MAX232 and thus if you measure the TX terminal of a PC when the port is idle, you will measure something in the range of -8.0 VDC.

The PIC18F1320 associated with this design uses TTL levels, a thus a MAX232 level translator is required to convert the -8.0 VDC level from the PC to a TTL logic one (near +5 VDC) and the +8.0 VDC to a TTL logic zero (near ground).

The wiring details are illustrated in Figure #2. Note that the MAX232 uses charge pumps to generate voltages of nominally +/- 8.0 VDC. When wiring, observe the polarities of the capacitors.

Apply power and connect to the PC and connect to the PC COM port using Hyperterm of similar. Simply type a few characters and verify they appear on the LCD. Also type ?A0 (Measure A/D value on Channel 0) and observe the A/D result on your PC monitor.

Troubleshooting

If you have a voltmeter, you might verify that the voltage at V+ (term 2 or the MAX232) is nominally +8.0 VDC and that at V- (terminal 6) of the MAX232 is at -8.0 VDC.

Verify voltages as follows (or use the Morgan Logic Probe) when the Com port is idle;

Use Hyperterm or similar. With the probe attached to Point A (Fig #2) terminal 8 of the MAX232, verify the probe LED is off when the COM port is idle. Type a character and observe a brief flash of the LED. If you do not see this, there is a problem with your COM port. Attach the probe to Point B (term 10 of the PIC). When idle, the probe LED should be on. Type a character and note a brief flash.

If you able to send a character and it is written to the LCD, but there is no response to the ?A0 command, place the logic probe at Point C (terminal 11 of the MAX232). When idle, the probe LED should be on. In response to the ?A0 command, the processor will send the data and the probe LED should briefly flash. At Point D (terminal 14 of the MAX232), the probe LED should be off when idle. With the response to the ?A0 command, the probe LED should flash indicating data is being sent to the PC.

Assembly and Test Continued

The PIC processor is shipped with a default LCD geometry of 4 X 20, a tab size of 4, a blinking cursor (3) and eight user defined characters consisting of a single horizontal line of pixels, two lines, three lines, etc. All of this is stored in EEPROM. These values may be modified and the new values are stored to EEPROM alleviating the need to modify the parameters each time the processor is booted.

You may modify the LCD geometry using the ?G command. For example to configure as a 2 X 16 LCD

?G216
Boot the processor using by momentarily grounding the /MCLR lead on term 4 of the PIC processor and observe the screen message now shows the geometry as 2 X 16.

You may modify the tab setting and the cursor type using the ?s and ?c commands;

?s2?c0
This sets the tab size to 2 and the cursor type to 0 (no cursor). On booting the processor, you should now see the tab size in the initial message is now 2 and there is no cursor.

You may modify any of the user defined characters using the ?D command. For example, to define user defined character 3 as a narrow vertical line;

?D30101010101010101
On boot, you should now see user defined character 3 is a narrow vertical line.

To return to the default;

?s4?c3
?D3000000001f1f1f1f

Testing - Continued

In developing this design, I attempted to provide a rich command structure. In all probability, you may never use half of these. However, you may wish to write text to the LCD and tinker with the following commands.

For example;

Hellooo?b?b?tWorld?nLCD #105?k?l?x05?y1?l?0?1?2
?a - set cursor to home position.
?b - destructive backspace
?f - clear LCD and leave cursor at home position
?h - backspace - non destructive
?i - forward cursor - non destructive
?j - up cursor
?k - down cursor
?l - clear the current line and leave the cursor at the beginning of the line
?m - carriage return.  Position the cursor at the beginning of the current line.
?n - new line.  Advance to the beginning of the next line and clear this line.
?t - tab.  Advance the cursor one tab.

?? - display the character '?'.

?0 - display user defined character 0
?1 - display user defined character 1
?2 - display user defined character 2
...
?7 - display user defined character 7

Aside

The proper operation of many of these commands is dependent on the geometry of the LCD. In designing the LCD #105 PIC, my intent was to accommodate all common LCD geometries; 2X16, 2X20, 2X24, 2X40, 4X16, 4X20, However, in testing, I only had 2 X 16 and 4 X 20 LCDs and there is one thing about design, "whatever error can occur, will". If you successfully use the LCD #105 with an LCD having a geometry other than 2 X 16 or 4 X 20, please let me know. Also, if I have made an error with other LCDs, please let me know and I will correct any errors I have made and provide you with a new PIC. It would be of great help to me if I could borrow your LCD.

Analog to Digital Converters

See Figure #4. The design provides two 10-bit A/D inputs. To read either channel 0 or channel 1;

?A0
?A1
The processor will return the A/D value in the range of 0 to 1023 in decimal format;
512
The voltage appearing at the input of the A/D channel may then be calculated as;
	V_ad = V_supply * ad_val / 1024
where V_supply is the value of the supply voltage, nominally 5.0 VDC. Thus, if ad_val is 512, the voltage appearing at the input is calculated as 2.5 VDC.

Two test resistors, 4.7K and 10K, have been provided which may be configured in two different voltage divider arrangements to provide input voltages of nominally 3.4 and 1.60 VDC. Use the ?A0 or ?A1 commands to verify the A/D values are nominally 697 and 327.

Note;

	V_ad = 5.0 * 697 / 1024 = 3.40 VDC
	V_ad = 5.0 * 327 / 1024 = 1.60 VDC

Counter

See Figure #5. The design provides a single TTL counter input. Commands are provided to either read the total number of counts (?C0) or to measure the number of counts over a period of time.

Connect the nominal 10 PPS output of the Morgan Logic Probe to the counter input (terminal 3 of the PIC). You might then periodically issue the command;

?C0
and note the total number of counts in decimal format. Note the maximum number of counts is 65,535 and then counter then rolls over to zero.

To measure the number of counts over fives seconds;

?C5
In theory, the value 50 should be returned. However, the logic probe's output is not precisely 10 PPS.

I have tested the design using an expensive HP function generator and found the results to agree within 0.2 percent. That is, with the function generator set to 1000 PPS, the ?C1 command result was 998.

DS1820 Temperature Runs

The LCD unit provides two twisted pair runs, each of which may accommodate up to sixteen DS18S20s or DS18B20s or any mix of the two with a maximum of 200 feet of twisted pair cable.

Temperatures on either of the runs are performed in response to the ?T command;

?T0
The unit responds with the temperature measurements;

	0 27.50 10 f33a
	1 26.67 22 3b3b
	2 25.32 28 3cd2
	3 -5.26  etc
	4 3.04 etc

Note that the device number is simply a number assigned as each device is found. This is followed by the temperature in degrees C. The next field identifies the type of device (10 - DS18S20, 22 - DS18B20, 28 - DS18B20) and the last field expressed in 4-digit hexadecimal is a unique identifier for that particular device.

Each temperature measurement requires nominally one second.

If no devices are found on a run, no data is returned.

I routinely test each design with 200 feet of twisted pair cable and I am probably being conservative in noting the 200 feet limitation. Chances are that you can extend this, particularly if you use low capacitance cable such as CAT-5. However, it is important to note that once the capacitance (cable length) is too large, the entire run of devices will fail. That is, you will be no more able to reliably perform a measurement on the device closest to the PIC processor than on the device which is at the greatest distance.

If there is a communications error, the temperature will be reported as -88.88. This may occur occasionally if there is a burst of noise. However, if it occurs often, it is a "red flag" that there is too much cable on the run.

Unlike most of my designs, the LCD #105 Kit will accommodate any combination of DS18S20s, DS18B20s and DS1822s on the same run. However, the resolution of the temperature for the DS18S20 is 0.5 degrees C and 0.06 for the DS18B22. My suggestion is to use any DS18S20's (or the earlier DS1820 in a PR35 package) if you already have invested in them and can live with the 0.5 degree resolution. However, use DS18B20s if you are buying new devices. They provide the better resolution and also are a bit less expensive. (Note that the DS1822 may also be used. This is supposed to be a low cost version of the DS18B20 with a sacrifice in accuracy, but for some reason costs about the same as the DS18B20. Why pay the same amount for an inferior device.)

TTL Outputs

See Figure #7. The LCD #105 Kit provides two outputs which may be used to control an external transistor, FET or relay which may brought high or low using the ?H and ?L commands.

?H0
?L1
In the above, output 0 is brought high and output 1 is brought low.

The state of either of the outputs may be read using the ?S command. For example

?S0
The LCD unit then responds with the state of the output.
1
A discrete LED with an internal 330 Ohm series limiting resistor is provide to test the TTL outputs.


Interfacing with a Basic Stamp 2, BX24, PIC or similar

Note that when interfacing with a Basic Stamp 2, BasicX BX-24, PIC processor or similar, the MAX232 level shifter is not required. That is, the communication is at TTL levels, where the idle condition is a TTL logic one, near +5 VDC. This is illustrated on the lower half of Figure #2.

For the Basic Stamp 2, text and commands are sent using SerOut and received using the SerIn.

A number of examples follow;


' MeasAD.BS2
'
' Illustrates an interface with the Serial LCD #105 Kit to perform A/D measurment,
' read the state of Ser LCD Output 0 and reverse the state of the output.
'
'  BS2	                             Ser LCD #105 PIC
'
'  term 5 ---------------------------- RX (term 10)
'  term 6<--------------------------- TX (term 9)
'
' copyright, Peter H Anderson, Baltimore, MD, Sept, '03

      BAUDRATE CON 84	' 9600 noniverted

      ADVal     Var Word
      N	        Var Byte
      M 	Var Byte
      RelayState  Var Byte

      OUT0 = 1
      DIR0 = 1	' configure serial output

      DIR1 = 0	' serial input

      Pause 2000	' be sure the LCD has booted
      SerOut 0, BAUDRATE, 1, ["?c0"]     ' configure for no cursor

TOP:
      SerOut 0, BAUDRATE, 1, ["?f"]
      SerOut 0, BAUDRATE, 1, ["  A/D Demo?n"]   ' display on first line followed by new line

      For N = 1 to 100
	     SerOut 0, BAUDRATE, 1, ["?A0"]    ' command for an A/D reading on channel 0
         Serin 1, BAUDRATE, 300, ADError, [Dec ADVal, Skip 2]
	     SerOut 0, BAUDRATE, 1, ["?l", Dec ADVal]	' clear line and display result

ADContinue:

         Pause 1000
         GoSub ToggleLED
      Next
      Pause 5000
      GoTo TOP

ADError:	' if time out in receiving result from Ser LCD
      SerOut 0, BAUDRATE, 1, ["?lError"]
      GoTo ADContinue


ToggleLED:
       SerOut 0, BAUDRATE, 1, ["?S0"]	' fetch state of OUT0
       Serin 1, BAUDRATE, 200, ToggleLEDError, [Dec RelayState]

       If RelayState = 0 Then TurnOnLED
       SerOut 0, BAUDRATE, 1, ["?L0"]	' turn off LED
       Return

TurnOnLED:
       SerOut 0, BAUDRATE, 1, ["?H0"]	' turn on LED
       Return

ToggleLEDError:	' if timeout in receiving response to ?S0 command
      Return


' Counter.BS2
'
' Illustrates an interface with the Serial LCD #105 Kit to fetch
' the number of counts.
'
'  BS2	                             Ser LCD #105 PIC
'
'  term 5 ---------------------------- RX (term 10)
'  term 6<--------------------------- TX (term 9)
'
' copyright, Peter H Anderson, Baltimore, MD, Sept, '03

      BAUDRATE CON 84	' 9600 noniverted

      CountVal	Var Word
      N         Var Byte

      OUT0 = 1
      DIR0 = 1	' configure serial output

      DIR1 = 0	' serial input

      Pause 5000	' be sure the LCD has booted
      SerOut 0, BAUDRATE, 1, ["?c0"]     ' configure for no cursor

TOP:
      SerOut 0, BAUDRATE, 1, ["?f"]
      SerOut 0, BAUDRATE, 1, ["  Counter Demo"]   ' display on first line
      Pause 2000


      For N = 1 to 100
	     SerOut 0, BAUDRATE, 1, ["?l?x06?C0"]    ' clr line, advance to col 06 and count command
         Serin 1, BAUDRATE, 200, CountError, [Dec CountVal, Skip 2]
	     SerOut 0, BAUDRATE, 1, [Dec CountVal]	'  display result

CountContinue:
         Pause 1000

      Next
      Pause 5000
      GoTo TOP

CountError:	' if time out in receiving result from Ser LCD
      SerOut 0, BAUDRATE, 1, ["?lError"]
      GoTo CountContinue


' MeasT1.BS2
'
' Illustrates an interface with the Serial LCD #105 Kit to read four DS1820 devices
' on the Dallas interface and display the temperatures on the LCD.  It also displays
' the device numbers, temperatures, device types and serial numbers of each device on
' the single twisted pair run.
'
'  BS2	                             Ser LCD #105 PIC
'
'  term 5 ---------------------------- RX (term 10)
'  term 6<--------------------------- TX (term 9)
'
' copyright, Peter H Anderson, Baltimore, MD, Sept, '03

      BAUDRATE CON 84			' 9600 baud

      Device	Var Byte
      TWhole 	Var Byte
      TFract 	Var Byte
      DeviceType	Var Byte
      DeviceID	Var Word

      N		Var Byte

      OUT0 = 1
      DIR0 = 1	' serout

      DIR1 = 0	' serial input

      Pause 2000


      SerOut 0, BAUDRATE, 1,["?f?c0   Temp Demo"]		' clear display, cursor type 0
      ' Pause 10


      Pause 2000
      SerOut 0, BAUDRATE, 1, ["?f"]      			' clear the LCD

Top:

      SerOut 0, BAUDRATE, 1, ["?T0"]	' command to begin temperature measurements

      For N = 1 To 4 ' can be up to 16, but limit to 4 for display
                     ' on 2X16 LCD

                        ' 1    2    3    4
         Branch N, [PT1, PT1, PT2, PT3, PT2]

PT1:     SerOut 0, BAUDRATE, 1, ["?f"]	  ' home the cursor
         GoTo Continue

PT2:     SerOut 0, BAUDRATE, 1, ["?x08"]
         GoTo Continue

PT3:	 SerOut 0, BAUDRATE, 1, ["?y1?x00"]
         GoTo Continue

Continue:

         Serin 1, BAUDRATE, 3000, TempDone, [Dec Device, SDec TWhole, Skip 1, Dec2 TFract, Dec2 DeviceType, Hex4 DeviceID, Skip 2]
         ' important to get back to read the next sensor within 1.5 seconds

         Debug Dec Device, " "

         If TWhole < 128 Then Around
           TWhole = TWhole ^ $ff + 1	' if its negative, do 2's comp
           Debug "-"
           Serout 0, BAUDRATE, 1, ["-"]

Around:
	     Debug Dec TWhole, ".", Dec2 TFract, " ", Dec2 DeviceType, " ", Hex4 DeviceID, 13
	 		' display on terminal
	     SerOut 0, BAUDRATE, 1, [Dec TWhole, ".", Dec2 TFract] ' display on LCD
	                '
         Next

         Goto TOP

TempDone:
         Pause 5000
         SerOut 0, BAUDRATE,1, ["?f"]
         SerOut 0, BAUDRATE, 1, ["Done"]		' clear the LCD and display message
	   Pause 2000
         SerOut 0, BAUDRATE, 1, ["?f"]

      Goto Top



Over the next few weeks, I plan to develop sample routines for use with the NetMedia BX24.