Shop OBEX P1 Docs P2 Docs Learn Events
Combining Temp sensor (ds1620) with LCD 20x2 4-bit Screen — Parallax Forums

Combining Temp sensor (ds1620) with LCD 20x2 4-bit Screen

musclegearheadmusclegearhead Posts: 1
edited 2013-03-28 09:45 in BASIC Stamp
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:

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-03-28 09:45
    The following video describes some of the concepts involved in merging two programs. One advantage you have is that the I/O pins and variables used in the two programs don't conflict, so mergin will be relatively easy. However, the LCD code is designed to read data serially from EEPROM, while the temperature sensor code returns values in variables. So once you merge the code you will have to adjust the display section to handle sending the data from the temperature sensor to the display. Once you have the code merged post that (as an attachment) and we'll see where you're stuck at that point.

    http://www.youtube.com/watch?v=Xgue6nWtUfs
Sign In or Register to comment.