program trouble
I am having trouble with displaying the temperature on the LCD (exp.#28). The DS1620 circuit works and shows with DEBUG, so I think the problem is with my code. My last try is below. Could you please help me with this. Thanks!
Regards,
Stu
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' ==============================================================================
'
' Program Description
'
' This program measures temperature using the Dallas Semiconductor DS1620
' temperature sensor and displays the results in fahrenheit.
'
' I/O Definitions
'
DQ············· CON···· 0·················· ' DS1620.1 (data I/O)
Clock·········· CON···· 1··················· ' DS1620.2
Reset·········· CON···· 2···················· ' DS1620.3
E·············· CON···· 8···················· ' LCD Enable pin· (1 = enabled)
RS············· CON···· 3······················ ' Register Select (1 = char)
'
' Constants
'
ClrLCD········· CON· $01···················· ' clear the LCD
CrsrHm········· CON· $02····················· ' move cursor to home position
DispLf········· CON· $18······················ ' shift displayed chars left
DispRt········· CON· $1C······················· ' shift displayed chars right
DDRam·········· CON·· $80····················· ' Display Data RAM control
CGRam·········· CON· $40····················· ' Custom character RAM
Line1·········· CON· $80···················· ' DDRAM address of line 1
Line2·········· CON· $C0··················· ' DDRAM address of line 2
RdTmp·········· CON···· $AA···················· ' read temperature
WrHi··········· CON···· $01····················· ' write TH (high temp)
WrLo··········· CON···· $02······················ ' write TL (low temp)
RdHi··········· CON···· $A1······················· ' read TH
RdLo··········· CON···· $A2······················ ' read TL
StartC········· CON···· $EE····················· ' start conversion
StopC·········· CON···· $22···················· ' stop conversion
WrCfg·········· CON···· $0C··················· ' write config register
RdCfg·········· CON···· $AC·················· ' read config register
'
' Variables
'
tempIn········· VAR···· Word··················· ' raw temperature
sign··········· VAR···· tempIn.BIT8············ ' 1 = negative temperature
tSign·········· VAR···· Bit
tempC·········· VAR···· Word··················· ' Celsius
tempF·········· VAR···· Word··················· ' Fahrenheit
LCDout········· VAR···· OUTB··········· ' 4-bit LCD data
char··········· VAR···· Byte··········· ' character sent to LCD
'
' DATA statements
Msg· DATA· "Present Temperature = "
'
' Initialization
'
Initialize:
·DIRS = %1000000111111000··········· ' setup pins
· GOSUB LCDinit···················· ' initialize LCD for 4-bit mode
· HIGH Reset··································· ' alert the DS1620
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]WrCfg, %10]···· ' use with CPU; free-run
· LOW Reset
· PAUSE 10
· HIGH Reset
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]StartC]······· ' start conversions
· LOW Reset
· char = DDRAM
'
' Program Code
'
Main:
· GOSUB Get_Temperature························ ' read the DS1620
· DEBUG HOME
· DEBUG "<<<DS1620>>>·· ", CR
· DEBUG SDEC tempC, " C··· ", CR
· DEBUG SDEC tempF, " F··· ", CR
· GOSUB· LCDcommand
· READ (Msg + tempF), char
· GOSUB· LCDwrite
·· PAUSE 1000·································· ' pause between readings
· GOTO Main
'
' Subroutines
'
Get_Temperature:
· HIGH Reset··································· ' alert the DS1620
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]RdTmp]········· ' give command to read temp
· SHIFTIN DQ, Clock, LSBPRE, [noparse][[/noparse]tempIn\9]·········· ' read it in
· LOW Reset······································· ' release the DS1620
· tSign = sign································· ' save sign bit
· tempIn = tempIn / 2·························· ' round to whole degrees
· IF (tSign = 0) THEN No_Neg1
· tempIn = tempIn | 123························ ' extend sign bits for negative
No_Neg1:
· tempC = tempIn······························· ' save Celsius value
· tempIn = tempIn */ $01CC······················ ' multiply by 1.8
· IF (tSign = 0) THEN No_Neg2···················· ' if negative, extend sign bits
· tempIn = tempIn | 123
No_Neg2:
· tempIn = tempIn + 32························· ' finish C -> F conversion
· tempF = tempIn······························· ' save Fahrenheit value
· RETURN
·· LCDinit:
· PAUSE 500················· ' let the LCD settle
· LCDout = %0011·············· ' 8-bit mode
· PULSOUT E,1
· PAUSE 5
· PULSOUT E,1
· PULSOUT E,1
· LCDout = %0010·············· ' 4-bit mode
· PULSOUT E,1
· char = %00101000····················· ' multi-line mode
· GOSUB LCDcommand
· char = %00001100··········· ' disp on, crsr off, blink off
· GOSUB LCDcommand
· char = %00000110··········· ' inc crsr, no disp shift
· GOSUB LCDcommand
· RETURN
· LCDcommand:
·· LOW RS················· ' enter command mode
·· RETURN
· LCDwrite:
· LCDout = char.HIGHNIB········ ' output high nibble
· PULSOUT E,1·············· ' strobe the Enable line
· LCDout = char.LOWNIB········ ' output low nibble
· PULSOUT E,1
· HIGH RS················· ' return to character mode
· RETURN
Regards,
Stu
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' ==============================================================================
'
' Program Description
'
' This program measures temperature using the Dallas Semiconductor DS1620
' temperature sensor and displays the results in fahrenheit.
'
' I/O Definitions
'
DQ············· CON···· 0·················· ' DS1620.1 (data I/O)
Clock·········· CON···· 1··················· ' DS1620.2
Reset·········· CON···· 2···················· ' DS1620.3
E·············· CON···· 8···················· ' LCD Enable pin· (1 = enabled)
RS············· CON···· 3······················ ' Register Select (1 = char)
'
' Constants
'
ClrLCD········· CON· $01···················· ' clear the LCD
CrsrHm········· CON· $02····················· ' move cursor to home position
DispLf········· CON· $18······················ ' shift displayed chars left
DispRt········· CON· $1C······················· ' shift displayed chars right
DDRam·········· CON·· $80····················· ' Display Data RAM control
CGRam·········· CON· $40····················· ' Custom character RAM
Line1·········· CON· $80···················· ' DDRAM address of line 1
Line2·········· CON· $C0··················· ' DDRAM address of line 2
RdTmp·········· CON···· $AA···················· ' read temperature
WrHi··········· CON···· $01····················· ' write TH (high temp)
WrLo··········· CON···· $02······················ ' write TL (low temp)
RdHi··········· CON···· $A1······················· ' read TH
RdLo··········· CON···· $A2······················ ' read TL
StartC········· CON···· $EE····················· ' start conversion
StopC·········· CON···· $22···················· ' stop conversion
WrCfg·········· CON···· $0C··················· ' write config register
RdCfg·········· CON···· $AC·················· ' read config register
'
' Variables
'
tempIn········· VAR···· Word··················· ' raw temperature
sign··········· VAR···· tempIn.BIT8············ ' 1 = negative temperature
tSign·········· VAR···· Bit
tempC·········· VAR···· Word··················· ' Celsius
tempF·········· VAR···· Word··················· ' Fahrenheit
LCDout········· VAR···· OUTB··········· ' 4-bit LCD data
char··········· VAR···· Byte··········· ' character sent to LCD
'
' DATA statements
Msg· DATA· "Present Temperature = "
'
' Initialization
'
Initialize:
·DIRS = %1000000111111000··········· ' setup pins
· GOSUB LCDinit···················· ' initialize LCD for 4-bit mode
· HIGH Reset··································· ' alert the DS1620
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]WrCfg, %10]···· ' use with CPU; free-run
· LOW Reset
· PAUSE 10
· HIGH Reset
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]StartC]······· ' start conversions
· LOW Reset
· char = DDRAM
'
' Program Code
'
Main:
· GOSUB Get_Temperature························ ' read the DS1620
· DEBUG HOME
· DEBUG "<<<DS1620>>>·· ", CR
· DEBUG SDEC tempC, " C··· ", CR
· DEBUG SDEC tempF, " F··· ", CR
· GOSUB· LCDcommand
· READ (Msg + tempF), char
· GOSUB· LCDwrite
·· PAUSE 1000·································· ' pause between readings
· GOTO Main
'
' Subroutines
'
Get_Temperature:
· HIGH Reset··································· ' alert the DS1620
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]RdTmp]········· ' give command to read temp
· SHIFTIN DQ, Clock, LSBPRE, [noparse][[/noparse]tempIn\9]·········· ' read it in
· LOW Reset······································· ' release the DS1620
· tSign = sign································· ' save sign bit
· tempIn = tempIn / 2·························· ' round to whole degrees
· IF (tSign = 0) THEN No_Neg1
· tempIn = tempIn | 123························ ' extend sign bits for negative
No_Neg1:
· tempC = tempIn······························· ' save Celsius value
· tempIn = tempIn */ $01CC······················ ' multiply by 1.8
· IF (tSign = 0) THEN No_Neg2···················· ' if negative, extend sign bits
· tempIn = tempIn | 123
No_Neg2:
· tempIn = tempIn + 32························· ' finish C -> F conversion
· tempF = tempIn······························· ' save Fahrenheit value
· RETURN
·· LCDinit:
· PAUSE 500················· ' let the LCD settle
· LCDout = %0011·············· ' 8-bit mode
· PULSOUT E,1
· PAUSE 5
· PULSOUT E,1
· PULSOUT E,1
· LCDout = %0010·············· ' 4-bit mode
· PULSOUT E,1
· char = %00101000····················· ' multi-line mode
· GOSUB LCDcommand
· char = %00001100··········· ' disp on, crsr off, blink off
· GOSUB LCDcommand
· char = %00000110··········· ' inc crsr, no disp shift
· GOSUB LCDcommand
· RETURN
· LCDcommand:
·· LOW RS················· ' enter command mode
·· RETURN
· LCDwrite:
· LCDout = char.HIGHNIB········ ' output high nibble
· PULSOUT E,1·············· ' strobe the Enable line
· LCDout = char.LOWNIB········ ' output low nibble
· PULSOUT E,1
· HIGH RS················· ' return to character mode
· RETURN
Comments
This will give you an idea. After positioning the LCD cursor, you can use this routine to put "tempRaw" into the LCD:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' ==============================================================================
'
' Program Description
'
' This program measures temperature using the Dallas Semiconductor DS1620
' temperature sensor and displays the results in fahrenheit.
'
' I/O Definitions
'
DQ············· CON···· 0·················· ' DS1620.1 (data I/O)
Clock·········· CON···· 1··················· ' DS1620.2
Reset·········· CON···· 2···················· ' DS1620.3
E·············· CON···· 8···················· ' LCD Enable pin· (1 = enabled)
RS············· CON···· 3······················ ' Register Select (1 = char)
'
' Constants
'
ClrLCD········· CON· $01···················· ' clear the LCD
CrsrHm········· CON· $02····················· ' move cursor to home position
DispLf········· CON· $18······················ ' shift displayed chars left
DispRt········· CON· $1C······················· ' shift displayed chars right
DDRam·········· CON·· $80····················· ' Display Data RAM control
CGRam·········· CON· $40····················· ' Custom character RAM
Line1·········· CON· $80···················· ' DDRAM address of line 1
Line2·········· CON· $C0··················· ' DDRAM address of line 2
RdTmp·········· CON···· $AA···················· ' read temperature
WrHi··········· CON···· $01····················· ' write TH (high temp)
WrLo··········· CON···· $02······················ ' write TL (low temp)
RdHi··········· CON···· $A1······················· ' read TH
RdLo··········· CON···· $A2······················ ' read TL
StartC········· CON···· $EE····················· ' start conversion
StopC·········· CON···· $22···················· ' stop conversion
WrCfg·········· CON···· $0C··················· ' write config register
RdCfg·········· CON···· $AC·················· ' read config register
'
' Variables
'
tempIn········· VAR···· Word··················· ' raw temperature
sign··········· VAR···· tempIn.BIT8············ ' 1 = negative temperature
tSign·········· VAR···· Bit
tempC·········· VAR···· Word··················· ' Celsius
tempF·········· VAR···· Word··················· ' Fahrenheit
LCDbus········· VAR···· OUTB··········· ' 4-bit LCD data
char··········· VAR···· Byte············ ' character sent to LCD
index·········· VAR···· Byte············· ' space for index
'
' DATA statements
Msg· DATA· "Present Temperature = "
'
' Initialization
'
Initialize:
·DIRS = %1000000111111000··········· ' setup pins
· GOSUB LCDinit···················· ' initialize LCD for 4-bit mode
· HIGH Reset··································· ' alert the DS1620
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]WrCfg, %10]···· ' use with CPU; free-run
· LOW Reset
· PAUSE 10
· HIGH Reset
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]StartC]······· ' start conversions
· LOW Reset
'
' Program Code
'
Main:
· GOSUB Get_Temperature························ ' read the DS1620
· DEBUG HOME
· DEBUG "<<<DS1620>>>·· ", CR
· DEBUG SDEC tempC, " C··· ", CR
· DEBUG SDEC tempF, " F··· ", CR
· GOSUB· LCDcommand
· READ (Msg + index), char
· GOSUB· ShowTemp_LCD
·· PAUSE 1000·································· ' pause between readings
· GOTO Main
'
' Subroutines
'
Get_Temperature:
· HIGH Reset··································· ' alert the DS1620
· SHIFTOUT DQ, Clock, LSBFIRST, [noparse][[/noparse]RdTmp]········· ' give command to read temp
· SHIFTIN DQ, Clock, LSBPRE, [noparse][[/noparse]tempIn\9]·········· ' read it in
· LOW Reset······································· ' release the DS1620
· tSign = sign································· ' save sign bit
· tempIn = tempIn / 2·························· ' round to whole degrees
· IF (tSign = 0) THEN No_Neg1
· tempIn = tempIn | $FF00························ ' extend sign bits for negative
No_Neg1:
· tempC = tempIn······························· ' save Celsius value
· tempIn = tempIn */ $01CC······················ ' multiply by 1.8
· IF (tSign = 0) THEN No_Neg2···················· ' if negative, extend sign bits
· tempIn = tempIn | $FF00
No_Neg2:
· tempIn = tempIn + 32························· ' finish C -> F conversion
· tempF = tempIn······························· ' save Fahrenheit value
· RETURN
·· LCDinit:
· PAUSE 500················· ' let the LCD settle
· LCDbus = %0011·············· ' 8-bit mode
· PULSOUT E,1
· PAUSE 5
· PULSOUT E,1
· PULSOUT E,1
· LCDbus = %0010·············· ' 4-bit mode
· PULSOUT E,1
· char = %00101000····················· ' multi-line mode
· GOSUB LCDcommand
· char = %00001100··········· ' disp on, crsr off, blink off
· GOSUB LCDcommand
· char = %00000110··········· ' inc crsr, no disp shift
· GOSUB LCDcommand
· RETURN
· LCDcommand:
·· LOW RS················· ' enter command mode
·· RETURN
LCDwrite:
· LCDbus = char.HIGHNIB··········· ' output high nibble
· PULSOUT E,1·············· ' strobe the Enable line
· LCDbus = char.LOWNIB··········· ' output low nibble
· PULSOUT E,1
· HIGH RS················· ' return to character mode
· RETURN
· ShowTemp_LCD:
· IF (tempIn.BIT15 = 1) THEN········· ' check sign bit
··· char = "-"························ ' - for negative temp
· ELSE
··· char = " "························ ' blank for positive temp
· ENDIF
· GOSUB LCDwrite······················ ' write sign character
· tempIn = ABS tempIn··············· ' convert tempt to absolute value
· FOR index = 2 TO 0···················· ' loop through three digits
·· index = tempIn DIG index + "0"······ ' get digit from temp, convert to decimal
··· GOSUB LCDwrite···················· ' write digit
· NEXT
· RETURN
I've attached the code.· Much of it is based on Jon William's work with the DS1620 and DS1302, as well as some mods from Tracy Allen.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Knight Designs
324 West Main Street
P.O. Box 97
Montour Falls, NY 14865
(607) 535-6777
Business Page:·· http://www.knightdesigns.com
Personal Page:··· http://www.lightlink.com/dream/chris
Designs Page:··· http://www.lightlink.com/dream/designs