18
A DUAL THERMOMETER INSTRUMENT

Project 4

Fabrication of the dual thermometer will be described in detail. Once the basic techniques used in making this instrument have been demonstrated, the projects that follow will be described in somewhat lesser detail. Later, we will convert this instrument into a controller, and then in the last project we will turn it into a data logger.

The dual thermometer device (see Figure 18.1) is based on the National Semiconductor LM34 temperature sensors. These inexpensive three-wire integrated circuits provide a voltage signal that can be converted to degrees Fahrenheit by multiplying that signal by 100. What could be simpler? What we must do is decide on the circuitry for hooking them up to the 16F877A PIC and then setting the appropriate registers. Like all other programs in this tutorial, the program will be written in PICBASIC PRO. The temperature readings are to be displayed on a 2-line-by-16-character display about every 0.2 seconds.

The datasheets for the LM34 and the 16F877A can be downloaded from the Internet.

The components shown in Table 18.1 are needed to make the dual thermometer.

See the circuit diagram for this instrument in Figure 18.2.

Note PC boards, designed by me, that allow each of the relevant projects to be built on them are available from encodergeek.com. See the support Web site for pictures and other details. This boards fits in a box provided by All Electronics for a durable mounting for our projects. There is ample room in the box for potentiometers, switches, and so on to make other instruments on these boards

The design of this instrument is an exercise in reading analog voltages, converting them to digital format, processing the information digitally, and displaying it on an LCD. As such, these are the basic steps required to read and display any analog signal with a PIC microprocessor, and are thus the basic processes for all our instruments.

There are eight pins on the 16F877A that can be used for analog input. They are distributed across ports A and E. Pin A.4 is excluded.

f0260-01

Figure 18.1 Dual thermometer instrument.

Analog signal 0

PORTA pin 0

Analog signal

1 PORTA pin 1

Analog signal 2

PORTA pin 2

Analog signal 3

PORTA pin 3
PORTA pin 4 Pin is not for analog use! (see Table 18.2)

Analog signal 4

PORTA pin 5

Analog signal 5

PORTE pin 0

Analog signal 6

PORTE pin 1

Analog signal 7

PORTE pin 2

If you use the following ADCIN instruction:

 ADCIN 5, VAL_UE

you will need to have the signal connected to pin E0 and the value read will be placed in variable VAL_UE. The number (5) in the instruction is a reference to the fifth analog signal number in these eight analog input pins, not PIN A.5.

Since most projects require a liquid crystal display, and we often use the lines on PORTE to control the display, the five lines on PORTA will be the ones most commonly used as analog inputs. (However, all three lines on PORTE can also be used as analog inputs if the LCD control lines are assigned to another port with the DEFINEs, or if you are not using the LCD at all.)

As with any PIC microprocessor with analog capabilities, the 16F877A microprocessor comes up in the analog mode on reset and startup. A number of the analog

TABLE 18.1 LIST OF ITEMS NEEDED TO MAKE THE DUAL THERMOMETER DEVICE

ITEM

QTY

DESCRIPTION

1

1

Experimenter’s circuit board (available as a kit from encodergeek.com)
Alternatives: RadioShack generic board; Velleman striped board

2

1

16-char-by-2-line display LCD

3

1

Microchip 16F877A microcontroller

4

1

20-MHz crystal

5

2

22 picofarad capacitors

6

1

2 feet of hookup wire

7

1

10 K resistor 1/8 watt for MCLR

8

1

7805 voltage regulator

9

1

1.0 uF cap 10 volts

10

1

0.1 uF cap 10 volts

11

2

LM34 temperature detectors

12

1

Battery holder with nuts and screws

13

1

Battery pigtail

14

1

Red LED

15

1

Switch

16

1

470 ohm 1/8 watt resistor for red LED

17

1

Bracket to mount switch

18

1

9-volt battery

19

X

Miscellaneous items from the junk drawer

20

2

Three pole screw connectors for sensors

21

1

40-pin socket for the PIC

22

1

2 × 5 pin programming connector

23

1

Box to hold everything

pins can be changed over to digital mode by setting the 4 lower bits in the ADCON1 register as indicated in Table 18.2.

The full table is on the next page.

Note that pin PORTA.4 is not included in Table 18.2. This pin is an open collector.

Pay special attention to how the reference voltages are specified at the various pins. See the datasheet.

f0262-01

Figure 18.2 Dual thermometer circuit diagram.

TABLE 18.2 DIGITAL/ANALOG SELECTIONS MADE WITH THE ADCON1 REGISTER (/X/IS USED TO INDICATE THE MISSING A.4 LINE)

t0263-01

NOTES

1. Settings 0110 and 0111 have identical results but are both shown, so all 16 combinations will be seen in the Table 18.2. The datasheet shows 011X for both lines.

2. Beware that AN7 to AN0 are the seven analog input identifications and Port E.2 to E.0 and A.5 to A.0 are pin identifications on the PIC.

3. A.4 is missing in this set, as stated earlier.

4. Do not confuse the pins with the analog inputs.

The preceding setting is described in detail on page 112 of the datasheet. Table 18.2 is a short form of that table. We do not have I/O access to lines A.7 and A.6 on the PIC 16F877A because they are internal to the processor. (They can, however, be read, and are related to the use of the parallel port capability of the PIC.)

In our particular case, we are interested in making pins A0 and A1 analog and using Vss and RA3 as the reference voltages to which we will compare the signal we receive from the LM34s. Since we have the LCD connected to PORTE, we need its pins to be digital. At this time, the status of the other lines is of no interest to us. The lines can be set to analog by selecting line 4 or 6 in the Table 18.2. For our purposes, which include using the LCD, all other lines can be digital. A useful selection is line 4, which makes all of PORTA analog and all of PORTE digital. In our case, PORTA.3 will be used as the reference voltage to which the incoming signals will be compared. We would specify that ADCON1 be set as follows:

 ADCON1=%00000011 or ADCON1=3

(ADCON1=%00000111, which is used throughout this book to match the usual microEngineering Labs settings; it sets all the A and E lines to digital.)

As shown in the wiring diagram, the output from the two LM34s will be fed into the 16F877A at PORTA on lines A0 and A1.

Notice that the way we are wiring in the LM34s is identical to the way we wire in a potentiometer. In either case, the device is placed between the high- and low-power supply rails, and we read what is the equivalent of the wiper. This is the standard way of reading a voltage into a PIC microcontroller. If other than 0 to 5 volts are to be read in, appropriate voltage dividers and the necessary safety precautions have to be provided. In our case, we will use pin A.3 as the reference voltage, and the voltage on this pin can be adjusted with the potentiometer provided for this purpose. See the earlier diagram in Figure 18.2. The pins that the reference voltages are impressed on must be selected as indicated in Table 18.2—meaning that only A.2 and A.3 can be used.

Let’s write the program to display the two signals on the 2-line-by-16-character display we are using for output. The display is to read as follows:

Temperatures

123°F 123°F

Provide two spaces on the left of the display so the display is centered in the LCD.

Let’s start out by setting the DEFINEs to specify where the LCD is located. In the wiring shown in Figure 18.2, the data will be on the four high lines of PORTB, while the three control lines will be on PORTD. This arrangement is shown in Program 18.1.

Program 18.1 Code for a two-temperature instrument

 CLEAR                   ; always clear variables
 DEFINE OSC 4            ; always define the oscillator
 DEFINE LCD_DREG PORTB   ; define LCD connections
 DEFINE LCD_DBIT 8       ; 8 bit data path is faster
 DEFINE LCD_RSREG PORTD  ; select register
 DEFINE LCD_RSBIT 7      ; select bit
 DEFINE LCD_EREG PORTD   ; enable register
 DEFINE LCD_EBIT 6       ; enable bit
 LOW PORTD.5             ; we can leave this low for write only ;

 DEFINE LCD_COMMANDUS 2000  ; delay in micro seconds
 DEFINE LCD_DATAUS  50      ; delay in micro seconds
                            ; since we will be reading analog 
                            ; lines, 
                            ; we need the DEFINES to specify the 
                            ; A-to-D conversions.
                            ; define the A2D settings
 DEFINE ADC_BITS 8          ; set number of bits in result
 DEFINE ADC_CLOCK 3         ; set internal clock source (3=rc)
 DEFINE ADC_SAMPLEUS 50     ; set sampling time in uS
                            ;
                            ; set the Analog-to-Digital control 
                            ; register
 ADCON1=%00000011           ; needed for the 16F877A LCD
 TEMP1 VAR BYTE             ; create adval to store first result 
 TEMP2 VAR BYTE             ; create adval to store second result
 ADCON1=%00000011           ; set digital ports
 PAUSE 500                  ; pause .500 second for the LCD
 LCDOUT $FE, 1              ; clear the display 
                            ;
 LOOP:                      ;
 ADCIN 0, TEMP1             ; read channel 0 to temp1
 ADCIN 1, TEMP2             ; read channel 1 to temp2
 TEMP1=TEMP1 * 100          ; convert to degrees F
 TEMP2=TEMP2 * 100          ; convert to degrees F
 LCDOUT $FE, $80, “Temp1=“,DEC3 TEMP1,”  ” ; display first temp
 LCDOUT $FE, $C0, “Temp2=“,DEC3 TEMP2,”  ” ; display second temp
 GOTO LOOP                  ; do it again
                            ;
 END                        ; all programs must end with end

If the DEFINEs for the LCD are changed to the standard LAB-X1 display, Program 18.1 can be tested on the LAB-X1, and the positions of the wiper of POT0 and POT1 will be displayed on the screen. The necessary substitutions for the first few lines are shown in Program 18.2.

Program 18.2 Replacement code segment

 CLEAR                    ; clear variables
 DEFINE OSC 4             ; osc speed
 DEFINE LCD_DREG PORTD    ; data register
 DEFINE LCD_RSREG PORTE   ; register select
 DEFINE LCD_RSBIT 0       ; register select bit
 DEFINE LCD_EREG PORTE    ; enable register
 DEFINE LCD_EBIT 1        ; enable bit
 DEFINE LCD_RWREG PORTE   ; read/write register
 DEFINE LCD_RWBIT 2       ; read/write bit
 DEFINE LCD_BITS 4        ; width of data

Using the same addresses used by the LAB-X1 lets us test our software on a system we know to be operating properly. Just knowing that the software will send something to the display can be heartening for a beginner. Once our confidence level regarding the use of these microcontrollers gets higher, we can abandon this strategy.

It is a good idea to take the time to add the ten-pin connector that allows us to program the PIC without removing it from the board we are working on. This makes it just about painless to make changes to the software, and in a really useful instrument (that you yourself have designed and built and thus know intimately about) this is a powerful feature that allows you to modify the characteristics of the instrument whenever you need to with a few keystrokes.

Another very useful adjunct to the hardware is to provide an additional input line and output line. These two lines can then be programmed to interact with the instrument we have created and would allow us to respond to a signal coming in on the input line and express the response on the output line after we had processed the information from the two sensors.

If we replace one of the sensors with a potentiometer, we can control the signal coming in on that sensor pin. Meaning, of course, that we can set one sensor input to whatever value we want with the potentiometer. This value can then be used as a set point that the other sensor reading is compared to before deciding whether we want to make our output line go high or low.

We have now turned our instrument into a very easy-to-set thermostat. If we like, this thermostat can be controlled from our auxiliary input line and the input line can get its signal from a remote source (maybe even over the Internet!). Keep in mind that we are not limited to using single line inputs and outputs. Our imagination is the only limit.

The instrument we created is more powerful and more flexible than that. The signal we send out can be a pulse width modulated (PWM) signal, where the level of modulation is a function of how far the set point is from the desired condition. It could be a frequency we might want to broadcast to the world—and so the beginnings of intelligent control start to come together.

The photographs in Figures 18.3 and 18.4 show the front and back of the prototype dual thermometer controller I built. Color versions of these photographs are much easier to read, and are on the support Web site that supports this book. On this board, the PIC is completely connected to the power side but none of the inputs/outputs are connected to anything except the LCD. This allows you to connect the I/O to whatever you want with jumpers to the screw terminals. Solder points are provided at the PIC end, as well as all the screw terminals, to make this easy.

In Figure 18.3, the universal project board has been mounted on a box, and a potentiometer and switch have been added to the system for future use.

Figure 18.4 shows the wiring needed to connect the I/O. We can also see the wiring to the potentiometer and the switch. These additions have been made in preparation for the next use of the instrument: data logging.

f0267-01

Figure 18.3 Dual thermometer instrument—front. (Either a one- or two-line display can be used.)

f0267-02

Figure 18.4 Dual thermometer instrument—bottom of board.