' -----[ Title ]----------------------------------------------------------- ' ' File...... DS1820-x.BSP ' Purpose... BS2p <--> Multiple DS1820s Demo ' Author.... Jon Williams ' E-mail.... jonwms@aol.com ' {$STAMP BS2p} ' -----[ Program Description ]--------------------------------------------- ' ' This program reads and displays the temperature from multiple DS1820 ' (1-wire) sensors. ' ' Program requires 2x16 LCD ' - LCD.E --> Pin0 (pulled down [to ground] through 4.7K) ' - LCD.R/W --> Pin2 (or grounded for write-only operation) ' - LCD.RS --> Pin3 ' - LCD.D4 --> Pin4 ' - LCD.D5 --> Pin5 ' - LCD.D6 --> Pin6 ' - LCD.D7 --> Pin7 ' -----[ I/O Definitions ]------------------------------------------------- ' LCDpin CON 0 ' data on pins 4 - 7 DS1820pin CON 13 ' DS1820 buss on pin 13 ' -----[ Constants ]------------------------------------------------------- ' ' LCD control characters ' NoCmd CON $00 ' just print ClrLCD CON $01 ' clear the LCD CrsrHm CON $02 ' cursor home CrsrLf CON $10 ' cursor left CrsrRt CON $14 ' move cursor right DispLf CON $18 ' shift display left DispRt CON $1C ' shift displayright DDRam CON $80 ' Display Data RAM control Line1 CON $80 ' address of line 1 Line2 CON $C0 ' address of line 2 DegSym CON 223 ' degrees symbol ' 1-Wire Support ' OW_FERst CON %0001 ' Front-End Reset OW_BERst CON %0010 ' Back-End Reset OW_BitMode CON %0100 OW_HighSpd CON %1000 ReadROM CON $33 ' read ID, serial num, CRC MatchROM CON $55 ' look for specific device SkipROM CON $CC ' skip ROM (one device) SearchROM CON $F0 ' search ' DS1820 control ' ConvertTemp CON $44 ' do temperature conversion ReadScratch CON $BE ' read DS1820 scratchpad NumSensors CON 2 ' number of DS1820s ' -----[ Variables ]------------------------------------------------------- ' sensor VAR Byte ' sensor number to process idx VAR Byte ' loop counter eeAddr VAR Byte ' ee address of ROM match romData VAR Byte(8) ' ROM data to DS1820 tempIn VAR Word ' raw temperature sign VAR tempIn.Bit8 ' 1 = negative temperature tInLow VAR tempIn.LowByte tInHigh VAR tempIn.HighByte tSign VAR Bit tempC VAR Word ' Celsius tempF VAR Word ' Fahrenheit rjNum VAR tempIn ' right justified number rjSign VAR Bit ' sign for rj number pos VAR Byte ' position to print digits VAR Nib ' digits in rjNum width VAR Nib ' width of display ' -----[ EEPROM Data ]----------------------------------------------------- ' ' ROM codes for connected sensors Temp1 DATA $10,$C9,$70,$45,$00,$00,$00,$FE Temp2 DATA $10,$81,$CF,$45,$00,$00,$00,$0A ' -----[ Initialization ]-------------------------------------------------- ' LCD_Setup: PAUSE 500 LCDCMD LCDpin,%00110000 : PAUSE 5 ' 8-bit mode LCDCMD LCDpin,%00110000 : PAUSE 0 LCDCMD LCDpin,%00110000 : PAUSE 0 LCDCMD LCDpin,%00100000 ' 4-bit mode LCDCMD LCDpin,%00101000 ' 2-line mode LCDCMD LCDpin,%00001100 ' no crsr, no blink LCDCMD LCDpin,%00000110 ' inc crsr, no disp shift ' -----[ Main Code ]------------------------------------------------------- ' Main: LCDOUT LCDpin,ClrLCD,["BSP <---> DS1820"] LCDOUT LCDpin,Line2, ["Multiple Sensors"] PAUSE 2000 LCDCMD LCDpin,ClrLCD ShowDS1820s: LOOKUP sensor,[Temp1,Temp2],eeAddr ' point to ROM code GOSUB GetTemp LCDOUT LCDpin,Line1,["Sensor ",("1"+sensor),":"] PAUSE 1000 sensor = sensor + 1 // NumSensors ' loop through all sensors PAUSE 1000 GOTO ShowDS1820s END ' -----[ Subroutines ]----------------------------------------------------- ' GetTemp: FOR idx = 0 TO 7 ' load ROM pattern READ (eeAddr+idx),romData(idx) NEXT OWOUT DS1820pin,OW_FERst,[MatchROM,STR romData\8,ConvertTemp] WaitForConversion: PAUSE 20 OWIN DS1820pin,OW_BitMode,[tempIn] IF tempIn = 0 THEN WaitForConversion OWOUT DS1820pin,OW_FERst,[MatchROM,STR romData\8,ReadScratch] OWIN DS1820pin,OW_BERst,[tInLow,tInHigh] tSign = sign ' save sign bit tempIn = tempIn/2 ' round to whole degrees IF tSign = 0 THEN NoNeg1 tempIn = tempIn | $FF00 ' extend sign bits for negs NoNeg1: tempC = tempIn ' save Celsius value tempIn = tempIn */ $01CC ' multiply by 1.8 IF tSign = 0 THEN NoNeg2 ' if neg, extend sign bits tempIn = tempIn | $FF00 NoNeg2: tempF = tempIn+32 ' finish C -> F conversion ShowTemps: rjNum = tempC : width = 4 : pos = Line1+10 GOSUB RJ_Print LCDOUT LCDpin,NoCmd,[DegSym,"C"] rjNum = tempF : width = 4 : pos = Line2+10 GOSUB RJ_Print LCDOUT LCDpin,NoCmd,[DegSym,"F"] RETURN RJ_Print: rjSign = rjNum.Bit15 rjNum = ABS(rjNum) digits = width LOOKDOWN rjNum,<[0,10,100,1000,65535],digits LCDOUT LCDpin,pos,[REP " "\(width-digits-1),13 * rjSign + " ",DEC rjNum] RETURN