Category Archives: Electronics

All electronics: analog, digital, mixed, vintage, …

PICAXE LCD Thermometer

Code and schematic for the LCD thermometer built with PICAXE 28X1.

Click on the schematic to get big picture, or click here for the pdf.

To copy the code, hover over the code and you’ll see four icons on top right. First icon opens new window with the code only, second icon copies the code to the clipboard, third icon prints the code (be careful with that, prints more than 5 pages, so don’t kill the trees).

; =================================================================
; author: Goran Poprzen
; filename: lcd-temp-0.7.bas
; target: picaxe28x1
; date: Jan 5, 2009
; version: 0.7
; description: Measures the temperature with DS18B20 temp. sensor
; and shows on the 2x16 char LCD display,
; also sends to the serial terminal, and
; checks against the limits and sets the output accordingly:
; high if temp. is lower then higher limit,
; low if temp. is higher then lower limit.
; Confusing, eh? Not really!
; =================================================================

; picaxe28x1 variables
; bit0-31, b0-27, w0-13

; variables used here:

symbol lcdchar = b4
symbol lcdtmp = b5
symbol counter = b6

symbol position = b7
symbol tenths = b8
symbol ones = b9

; 16-bit measured temperature
symbol temp12 = w5
symbol temp12lo = b10
symbol temp12hi = b11

; 16-bit lower temp. limit
symbol TL = w6
symbol TLlo = b12
symbol TLhi = b13

; 16-bit higher temp. limit
symbol TH = w7
symbol THlo = b14
symbol THhi = b15

; status of the output pin
symbol LED = bit0

; greetings message
eeprom 0,("GPtronics 2010Thermometer")

#rem
here is the table for
decimal part of the temp reading

0 / 16 = 0.0000 ==> 00
1 / 16 = 0.0625 ==> 06
2 / 16 = 0.1250 ==> 12
3 / 16 = 0.1875 ==> 19
4 / 16 = 0.2500 ==> 25
5 / 16 = 0.3125 ==> 31
6 / 16 = 0.3750 ==> 37
7 / 16 = 0.4375 ==> 44
8 / 16 = 0.5000 ==> 50
9 / 16 = 0.5625 ==> 56
10 / 16 = 0.6250 ==> 62
11 / 16 = 0.6875 ==> 69
12 / 16 = 0.7500 ==> 75
13 / 16 = 0.8125 ==> 81
14 / 16 = 0.8750 ==> 87
15 / 16 = 0.9375 ==> 94

#endrem
; two decimal digits rounding
; eeprom 32,("00061219253137445056626975818794")

; four decimal digits - not rounded
eeprom 32,("0000062512501875250031253750437550005625625068757500812587509375")

; =================================================================

main: ; executed at the beginning
gosub taketemp ; first measuring to determine initial status of the output relay
gosub init
gosub lcdinit ; initialization of the lcd display
gosub hello ; greeting message - just for fun :)

main2: ; loop
gosub taketemp ; temperature measuring
gosub checklimits ; checks measured temp. against limits and sets output accordingly
gosub sendtemp ; sends data to rs-232
gosub showtemp ; converts numbers and shows them on lcd

goto main2 ; loop again

; =================================================================

init:
counter = 0

TLhi = 22 ; lower temperature limit
TLlo = 0
THhi = 24 ; higher temperature limit
THlo = 0

if temp12 <= TH then : gosub ledon : else : gosub ledoff : endif

return

; =================================================================

lcdinit:
pins = 0
pause 200

pins = 48
gosub pulsing
gosub pulsing
gosub pulsing

pins = 32
gosub pulsing
gosub pulsing

pins = 128
gosub pulsing

lcdchar = 14
gosub wrins

lcdchar = 12
gosub wrins

return

pulsing:
pulsout 3,3
pause 10
return

; =================================================================

wrchr:
pins = lcdchar & 240
high 2
pulsout 3,1
lcdtmp = lcdchar * 16
pins = lcdtmp & 240
high 2
pulsout 3,1
return

wrins:
pins = lcdchar & 240
pulsout 3,1
lcdtmp = lcdchar *16
pins = lcdtmp & 240
pulsout 3,1
high 2
return

; =================================================================

hello:
lcdchar = 1
gosub wrins

lcdchar = 12
gosub wrins

for counter = 0 to 13
read counter, lcdchar
gosub wrchr
next counter

lcdchar = 192
gosub wrins

for counter = 14 to 24
read counter, lcdchar
gosub wrchr
next counter

pause 1000

lcdchar = 1
gosub wrins

return

; =================================================================

taketemp:
readtemp12 7, temp12
temp12 = temp12 * 16
temp12lo = temp12lo / 16
return

; =================================================================

sendtemp:
#rem
sertxd("Temp= ",#temp12hi," and ",#temp12lo,"/16 C",13,10)
sertxd("TH= ",#THhi," and ",#THlo,"/16 C",13,10)
sertxd("TL= ",#TLhi," and ",#TLlo,"/16 C",13,10)
return
#endrem
sertxd("Temp= ",#temp12,13,10)
sertxd("TH= ",#TH,13,10)
sertxd("TL= ",#TL,13,10)
return

; =================================================================

showtemp:
lcdchar = 2
gosub wrins

lcdchar = temp12hi / 100
if lcdchar = 0 then leadzero
lcdchar = lcdchar + 48
goto nonleadzero
leadzero:
lcdchar = lcdchar + 32
nonleadzero:
tenths = temp12hi // 100
gosub wrchr ; write 100s

lcdchar = tenths / 10
lcdchar = lcdchar + 48
ones = tenths // 10
gosub wrchr ; write 10s

lcdchar = ones
lcdchar = lcdchar + 48
gosub wrchr ; write 1s

lcdchar = "."
gosub wrchr

for counter = 0 to 3
position = temp12lo * 4 + 32 + counter
read position, lcdchar
gosub wrchr
next counter

lcdchar = 178
gosub wrchr

lcdchar = "C"
gosub wrchr

return

; =================================================================

checklimits:

if LED = 1 and temp12 > TH then gosub ledoff

if LED = 0 and temp12 < TL then gosub ledon

return

; =================================================================

ledoff:
low portc 1
LED = 0
return

ledon:
high portc 1
LED = 1
return

; =================================================================

[/sourcecode]

That’s it. In the next episode, I’ll break-up the code and try to explain each subroutine.