Code:
'*********************************************
' Thermometer
' with Nokia 3310 display
@ DEVICE pic16F628A, XT_OSC, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_OFF, LVP_OFF, CPD_OFF, PROTECT_OFF
DEFINE NO_CLRWDT 1
DEFINE OSC 4
TRISB = %00001110 'portB configuration ; 0=out 1=in
TRISA = %10100010 'portA configuration
PortA = %11100010
CMCON = 7
VRCON = 0
INTCON = 0
OPTION_REG = %00000111 '
RST var PortA.4 'pin 3 Output reset display
SDIN var PortA.3 'pin 2 Output master data out
SCK var PortA.2 'pin 1 Output Spi clock
DQ var PortA.1 'pin 18 I/O DQ DS18b20
D_C var PortB.0 'pin 6 Output command/data sel. register
temperature var Word
tref var word
TempC var Byte
Float var Byte
Dummy var Byte
Flag var Byte
SignBit var Flag.0
Minus var Byte
LcdReg var Byte
x var Byte
y var Byte
Offset var Byte
Char var Byte
a var Byte
PosX var Byte
PosY var Byte
Chr var Byte
LcdData var Byte
V var Word
Sign var Byte
Busy VAR BIT ' Busy Status-Bit
DS18B20_12bit CON %01111111 ' 750ms, 0.0625°C
'====================================================================
DATA @10,$7E,$91,$89,$85,$7E,_ ';// 0
$00,$82,$FF,$80,$00,_ ';// 1
$82,$C1,$A1,$91,$8E,_ ';// 2
$42,$81,$89,$89,$76,_ ';// 3
$18,$14,$12,$FF,$10,_ ';// 4
$4F,$89,$89,$89,$71,_ ';// 5
$7E,$89,$89,$89,$72,_ ';// 6
$01,$E1,$11,$09,$07,_ ';// 7
$76,$89,$89,$89,$76,_ ';// 8
$46,$89,$89,$89,$7E,_ ';// 9
$08,$08,$3E,$08,$08,_ ';// + 10
$08,$08,$08,$08,$08,_ ';// - 11
$00,$C0,$C0,$00,$00 ';// . 12
'====================================================================
pause 100
RST = 1
PortB = 0
LcdReg = %00100001 'LCD Extended Commands.
call PrintCtrlLcd
LcdReg = $C8 'Set LCD Vop (Contrast)
call PrintCtrlLcd
LcdReg = %00000110 'Set Temp coefficent.
call PrintCtrlLcd
LcdReg = %00010011 'LCD bias mode 1:48.
call PrintCtrlLcd
LcdReg = %00100000 'LCD Standard Commands, Horizontal addressing mode.
call PrintCtrlLcd
LcdReg = $0C 'LCD in normal mode. $0D inverse
Call PrintCtrlLcd
Call CursorHome
Gosub LCD_Clear
'====================================================================
OWOut DQ, 1, [$CC, $4E, 0, 0, DS18B20_12bit]
Output DQ ' Make Pin Output
DQ=0 ' OneWire line Low
PauseUs 480 ' Keep down for 480 µS
Input DQ ' Make Pin Input
PauseUs 70 ' Wait 70 µS
OPTION_REG.7=1
TMR0=0
INTCON.2=0
W1:
OWIN DQ,4,[busy] 'Check for still busy converting
IF busy = 0 THEN W1 'Still busy?, then loop
CALL BorderLine
'====================================================================
Main:
ClockLoop: IF INTCON.2=0 THEN ClockLoop
INTCON.2=0
CALL BorderLine
OWOut DQ, 1, [$CC, $44]
OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [temperature.byte0, temperature.byte1]
SignBit = 0
if temperature.15 then
temperature = ~ temperature + 1
SignBit = 1
endif
Dummy = 625 * Temperature
TempC = DIV32 10
TempC = (Temperature & $7FF) >> 4
Float = ((Temperature.Lowbyte & $0F ) * 25 )>>2
Temperature=TempC*100 + Float
If SignBit then
V= 10000 - Temperature ' 25'C=12500 0'C=10000 -10'C=9000
SignBit = 0
else
V= 10000 + Temperature
EndIf
If V >= 10000 then ' Above 0 C.
Temperature=V-10000
else
Temperature=10000-V ' Below 0 C.
EndIf
GoSub SelectSign ' +/blank/- Sign
'================= setting cursor for display the temperature
LcdReg = %10001000 ' cursor X
call PrintCtrlLcd
LcdReg = %01000010 + 1 ' cursor Y
call PrintCtrlLcd
'================= now display temperature
if Tempc > 99 then
Char = 3
call PrintChar
else
Char = 12 ' + plus
if SignBit = 1 then Char = 13 ' - minus
call PrintChar
endif
Char = (temperature dig 3) + 2
call PrintChar
Char = (temperature dig 2) + 2
call PrintChar
Char = 14
call PrintChar
Char = (temperature dig 1) + 2
Call PrintChar
Char = (temperature dig 0) + 2
Call PrintChar
goto Main
'================== S U B R O U T I N E S ===========================
'========================== L C D ===================================
CursorHome: LcdReg = %10000000 ' cursor Home
call PrintCtrlLcd
LcdReg = %01000000 ' cursor Home
call PrintCtrlLcd
return
PrintChar: offset = Char * 5
for a = 1 to 5
read offset, LcdReg
call PrintDataLcd
offset = offset + 1
next a
LcdReg = 0
call PrintDataLcd
return
PrintCtrlLcd: D_C = 0
PrintDataLcd: for x = 1 to 8
SDIN = LcdReg.7
SCK = 1
LcdReg = LcdReg << 1
SCK = 0
next x
D_C = 1
return
LCD_Clear:
PosX=0:PosY=0:GOSUB LCD_GotoXY
HIGH D_C
FOR Chr=1 TO 252
LcdData=0:GOSUB LCD_ByteOut
LcdData=0:GOSUB LCD_ByteOut
NEXT Chr
RETURN
LCD_GotoXY:
LOW D_C
LcdData=%01000000 | PosY :GOSUB LCD_ByteOut
LcdData=%10000000 | PosX :GOSUB LCD_ByteOut
RETURN
LCD_ByteOut:
SHIFTOUT SDIN,SCK,1,[LcdData]
RETURN
'====================================================================
SelectSign:
If v = 10000 then ' Temperature = 0 C.
Sign=" " ' No sign
Else
If v < 10000 then ' <> 0
Sign="-" ' Temperature below 0 C.
Else
Sign="+" ' Temperature above 0 C.
EndIf
EndIf
Return
BorderLine:
' Raw 0
' Raw 1
' no space for post code here
Return
'===================================
End 'of program
Bookmarks