Combining Temp sensor (ds1620) with LCD 20x2 4-bit Screen
musclegearhead
Posts: 1
Hi, I am new to basic stamp, and for my school project I am trying to have a ds1620 temperature sensor to read the ambient temperature and display the temperature in a 20x2 Screen(Celsius in one line and Fahrenheit in the second line) using only 4 wires for data(4-bit mode) and refreshing every one or two seconds. I have all the parts and everything works perfectly separately but I can't put them together.
Can some one please help me with the code?
I found the codes in this website.
For the LCD code I have:
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ I/O Definitions ]
E PIN 1 ' Enable pin
RW PIN 2 ' Read/Write
RS CON 3 ' Register Select
LcdBus VAR OUTB ' 4-bit LCD data bus
'
[ Constants ]
LcdCls CON $01 ' clear the LCD
LcdHome CON $02 ' move cursor home
LcdCrsrL CON $10 ' move cursor left
LcdCrsrR CON $14 ' move cursor right
LcdDispL CON $18 ' shift chars left
LcdDispR CON $1C ' shift chars right
LcdDDRam CON $80 ' Display Data RAM control
LcdCGRam CON $40 ' Character Generator RAM
LcdLine1 CON $80 ' DDRAM address of line 1
LcdLine2 CON $C0 ' DDRAM address of line 2
#DEFINE _LcdReady = ($STAMP >= BS2P)
'
[ Variables ]
char VAR Byte ' character sent to LCD
newChar VAR Byte
idx1 VAR Byte ' loop counters
idx2 VAR Nib
'
[ EEPROM Data ]
Msg1 DATA "THE BASIC STAMP " ' preload EE with messages
Msg2 DATA " IS VERY COOL! ", 3
'
[ Initialization ]
Reset:
DIRL = %11111110 ' setup pins for LCD
PAUSE 100 ' let the LCD settle
Lcd_Setup:
LcdBus = %0011 ' 8-bit mode
PULSOUT E, 3
PAUSE 5
PULSOUT E, 3
PULSOUT E, 3
LcdBus = %0010 ' 4-bit mode
PULSOUT E, 1
char = %00101000 ' multi-line mode
GOSUB LCD_Cmd
char = %00001100 ' disp on, no crsr or blink
GOSUB LCD_Cmd
char = %00000110 ' inc crsr, no disp shift
GOSUB LCD_Cmd
Download_Chars: ' download custom chars
char = LcdCGRam ' point to CG RAM
GOSUB LCD_Cmd ' prepare to write CG data
'
[ Program Code ]
Main:
char = LcdCls ' clear the LCD
GOSUB LCD_Cmd
PAUSE 250
FOR idx1 = 0 TO 15 ' get message from EEPROM
READ (Msg1 + idx1), char ' read a character
GOSUB LCD_Out ' write it
NEXT
PAUSE 1000 ' wait 2 seconds
FOR idx1 = 0 TO 15 ' get message from EEPROM
READ (Msg2 + idx1), char ' read a character
GOSUB LCD_Out ' write it
NEXT
PAUSE 1000 ' wait 2 seconds
GOTO Main ' do it all over
'
[ Subroutines ]
LCD_Cmd:
LOW RS ' enter command mode
LCD_Out:
LcdBus = char.HIGHNIB ' output high nibble
PULSOUT E, 3 ' strobe the Enable line
LcdBus = char.LOWNIB ' output low nibble
PULSOUT E, 3
HIGH RS ' return to character mode
RETURN
And for the temp sensor code, I have:
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ I/O Definitions ]
DQ CON 10 ' DS1620.1 (data I/O)
Clock CON 9 ' DS1620.2
Reset CON 8 ' DS1620.3
'
[ Constants ]
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
RdCntr CON $A0 ' read counter
RdSlope CON $A9 ' read slope
StartC CON $EE ' start conversion
StopC CON $22 ' stop conversion
WrCfg CON $0C ' write config register
RdCfg CON $AC ' read config register
DegSym CON 186 ' degrees symbol
'
[ Variables ]
tempIn VAR Word ' raw temperature
sign VAR tempIn.BIT8 ' 1 = negative temperature
tC VAR Word ' Celsius
tF VAR Word ' Fahrenheit
'
[ Initialization ]
Setup:
HIGH Reset ' alert the DS1620
SHIFTOUT DQ, Clock, LSBFIRST, [WrCfg, %10] ' use with CPU; free-run
LOW Reset
PAUSE 10
HIGH Reset
SHIFTOUT DQ, Clock, LSBFIRST, [StartC] ' start conversions
LOW Reset
DEBUG CLS,
"DS1620 ", CR,
"
"
'
[ Program Code ]
Main:
DO
GOSUB Read_DS1620 ' get the temperature
Display_C:
DEBUG CRSRXY, 0, 2,
(tC.BIT15 * 13 + " "),
DEC (ABS tC / 10), ".", DEC1 (ABS tC),
DegSym, " C", CLREOL
Display_F:
DEBUG CRSRXY, 0, 3,
(tF.BIT15 * 13 + " "),
DEC (ABS tF / 10), ".", DEC1 (ABS tF),
DegSym, " F", CLREOL
PAUSE 1000 ' delay between readings
LOOP
'
[ Subroutines ]
Read_DS1620:
HIGH Reset ' alert the DS1620
SHIFTOUT DQ, Clock, LSBFIRST, [RdTmp] ' give command to read temp
SHIFTIN DQ, Clock, LSBPRE, [tempIn\9] ' read it in
LOW Reset ' release the DS1620
tempIn.BYTE1 = -sign ' extend sign bit
tC = tempIn * 5 ' convert to tenths
IF (tC.BIT15 = 0) THEN ' temp C is positive
tF = tC */ $01CC + 320 ' convert to F
ELSE ' temp C is negative
tF = 320 - ((ABS tC) */ $01CC) ' convert to F
ENDIF
RETURN
Please help me, I am lost.:frown:
Can some one please help me with the code?
I found the codes in this website.
For the LCD code I have:
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ I/O Definitions ]
E PIN 1 ' Enable pin
RW PIN 2 ' Read/Write
RS CON 3 ' Register Select
LcdBus VAR OUTB ' 4-bit LCD data bus
'
[ Constants ]
LcdCls CON $01 ' clear the LCD
LcdHome CON $02 ' move cursor home
LcdCrsrL CON $10 ' move cursor left
LcdCrsrR CON $14 ' move cursor right
LcdDispL CON $18 ' shift chars left
LcdDispR CON $1C ' shift chars right
LcdDDRam CON $80 ' Display Data RAM control
LcdCGRam CON $40 ' Character Generator RAM
LcdLine1 CON $80 ' DDRAM address of line 1
LcdLine2 CON $C0 ' DDRAM address of line 2
#DEFINE _LcdReady = ($STAMP >= BS2P)
'
[ Variables ]
char VAR Byte ' character sent to LCD
newChar VAR Byte
idx1 VAR Byte ' loop counters
idx2 VAR Nib
'
[ EEPROM Data ]
Msg1 DATA "THE BASIC STAMP " ' preload EE with messages
Msg2 DATA " IS VERY COOL! ", 3
'
[ Initialization ]
Reset:
DIRL = %11111110 ' setup pins for LCD
PAUSE 100 ' let the LCD settle
Lcd_Setup:
LcdBus = %0011 ' 8-bit mode
PULSOUT E, 3
PAUSE 5
PULSOUT E, 3
PULSOUT E, 3
LcdBus = %0010 ' 4-bit mode
PULSOUT E, 1
char = %00101000 ' multi-line mode
GOSUB LCD_Cmd
char = %00001100 ' disp on, no crsr or blink
GOSUB LCD_Cmd
char = %00000110 ' inc crsr, no disp shift
GOSUB LCD_Cmd
Download_Chars: ' download custom chars
char = LcdCGRam ' point to CG RAM
GOSUB LCD_Cmd ' prepare to write CG data
'
[ Program Code ]
Main:
char = LcdCls ' clear the LCD
GOSUB LCD_Cmd
PAUSE 250
FOR idx1 = 0 TO 15 ' get message from EEPROM
READ (Msg1 + idx1), char ' read a character
GOSUB LCD_Out ' write it
NEXT
PAUSE 1000 ' wait 2 seconds
FOR idx1 = 0 TO 15 ' get message from EEPROM
READ (Msg2 + idx1), char ' read a character
GOSUB LCD_Out ' write it
NEXT
PAUSE 1000 ' wait 2 seconds
GOTO Main ' do it all over
'
[ Subroutines ]
LCD_Cmd:
LOW RS ' enter command mode
LCD_Out:
LcdBus = char.HIGHNIB ' output high nibble
PULSOUT E, 3 ' strobe the Enable line
LcdBus = char.LOWNIB ' output low nibble
PULSOUT E, 3
HIGH RS ' return to character mode
RETURN
And for the temp sensor code, I have:
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ I/O Definitions ]
DQ CON 10 ' DS1620.1 (data I/O)
Clock CON 9 ' DS1620.2
Reset CON 8 ' DS1620.3
'
[ Constants ]
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
RdCntr CON $A0 ' read counter
RdSlope CON $A9 ' read slope
StartC CON $EE ' start conversion
StopC CON $22 ' stop conversion
WrCfg CON $0C ' write config register
RdCfg CON $AC ' read config register
DegSym CON 186 ' degrees symbol
'
[ Variables ]
tempIn VAR Word ' raw temperature
sign VAR tempIn.BIT8 ' 1 = negative temperature
tC VAR Word ' Celsius
tF VAR Word ' Fahrenheit
'
[ Initialization ]
Setup:
HIGH Reset ' alert the DS1620
SHIFTOUT DQ, Clock, LSBFIRST, [WrCfg, %10] ' use with CPU; free-run
LOW Reset
PAUSE 10
HIGH Reset
SHIFTOUT DQ, Clock, LSBFIRST, [StartC] ' start conversions
LOW Reset
DEBUG CLS,
"DS1620 ", CR,
"
"
'
[ Program Code ]
Main:
DO
GOSUB Read_DS1620 ' get the temperature
Display_C:
DEBUG CRSRXY, 0, 2,
(tC.BIT15 * 13 + " "),
DEC (ABS tC / 10), ".", DEC1 (ABS tC),
DegSym, " C", CLREOL
Display_F:
DEBUG CRSRXY, 0, 3,
(tF.BIT15 * 13 + " "),
DEC (ABS tF / 10), ".", DEC1 (ABS tF),
DegSym, " F", CLREOL
PAUSE 1000 ' delay between readings
LOOP
'
[ Subroutines ]
Read_DS1620:
HIGH Reset ' alert the DS1620
SHIFTOUT DQ, Clock, LSBFIRST, [RdTmp] ' give command to read temp
SHIFTIN DQ, Clock, LSBPRE, [tempIn\9] ' read it in
LOW Reset ' release the DS1620
tempIn.BYTE1 = -sign ' extend sign bit
tC = tempIn * 5 ' convert to tenths
IF (tC.BIT15 = 0) THEN ' temp C is positive
tF = tC */ $01CC + 320 ' convert to F
ELSE ' temp C is negative
tF = 320 - ((ABS tC) */ $01CC) ' convert to F
ENDIF
RETURN
Please help me, I am lost.:frown:
Comments
http://www.youtube.com/watch?v=Xgue6nWtUfs