Device 16F877XTAL 4 Declare LCD_TYPE ALPHA ' Type of LCD Used is AlphaDeclare LCD_DTPIN PORTB.4 ' The control bits B0,B1,B2,B3Declare LCD_RSPIN PORTB.2 ' RS pin on B4Declare LCD_ENPIN PORTB.3 ' E pin on B5Declare LCD_LINES 2 ' Amount of LCD linesDeclare LCD_INTERFACE 4 ' Interface method is 4 bitPORTB_PULLUPS = trueALL_DIGITAL = TRUE Symbol DQ = PORTB.0 'Place the DS1820 on bit 1 of PORTA Dim Sign As ByteDim Cnt As ByteDim Count_Per_Deg As ByteDim Temp As WordDim Temp_Dec As Byte DelayMS 150 ' Let LCD warm upCls ' Send a command that creates the degreePrint $FE,$40,$06,$09,$09,$06,$00,$00,$00,$00 ' symbol on the LCD, to print it, simply address' the character as 0, eg,' Print At 1, 1, Value, 0, "Degrees"Again: 'Send calculate temperature commandOWrite DQ, 1,[$55,$10,$31,$C5,$C8,$00,$00,$00,$F4,$44] ' FF I1 I2 I3 I4 I5 I6 CRC ' FF - Family, Ix - Address bytes, ' CRC - checksumRepeat DelayMS 25 ' Wait until conversion is complete ORead DQ,4,[Cnt] ' Keep reading low pulses until Until Cnt 0 ' the DS1820 is finished 'Send read scratchpad commandOWrite DQ,1,[$55,$10,$31,$C5,$C8,$00,$00,$00,$F4,$BE] ' FF I1 I2 I3 I4 I5 I6 CRC ' FF - Family, Ix - Address bytes, ' CRC - checksum checksumORead DQ,2,[Temp.LowByte,Temp.HighByte,Cnt,Cnt,Cnt,Cnt,Cnt,Count_Per_Deg] ' Each Cnt is 1/16 of a degree, therefore 100/16 = 6.25, and that's our decimal valueTemp_Dec = (6.25 * Cnt) ' Bits 8-15 are 1 for a negative temperature and 0 for a positiveIf Temp.8=1 Then ' If negative then drop the first bit, and invert the value Temp=(Temp.LowByte ^ $FF) >> 1 ' If Cnt = 0 then increment Temp, as the 1820 does not do this If Cnt = 0 Then Temp = Temp + 1 ' Change the sign to a negative Sign = "-" Else ' If positive then drop the first byte Temp=(Temp >> 1) ' And change the sign value to positive Sign = "+" ' Invert the decimal value Temp_Dec = 100 - Temp_Dec EndIf ' Display the data on the LCDPrint At 1,1, Sign, Dec Temp, ".", Dec Dig Temp_Dec, 1, 0,"C " GoTo Again
Device 16F877Xtal 4 Declare LCD_Type ALPHA ' Type of LCD Used is AlphaDeclare LCD_DTPin PORTB.4 ' The control bits B0,B1,B2,B3Declare LCD_RSPin PORTB.2 ' RS pin on B4Declare LCD_ENPin PORTB.3 ' E pin on B5Declare LCD_Lines 2 ' Amount of LCD linesDeclare LCD_Interface 4 ' Interface method is 4 bitPortB_Pullups = trueAll_Digital = TRUE Symbol DQ = PORTB.0 'Place the DS1820 on bit 1 of PORTA Dim Sign As ByteDim Cnt As ByteDim Count_Per_Deg As ByteDim Temp As WordDim Temp_Dec As Byte DelayMS 150 ' Let LCD warm upCls ' Send a command that creates the degreePrint $FE,$40,$06,$09,$09,$06,$00,$00,$00,$00 ' symbol on the LCD, to print it, simply address' the character as 0, eg,' Print At 1, 1, Value, 0, "Degrees"Again: 'Send calculate temperature commandOWrite DQ, 1,[$55,$10,$31,$C5,$C8,$00,$00,$00,$F4,$44] ' FF I1 I2 I3 I4 I5 I6 CRC ' FF - Family, Ix - Address bytes, ' CRC - checksumRepeat DelayMS 25 ' Wait until conversion is complete ORead DQ,4,[Cnt] ' Keep reading low pulses untilUntil Cnt=0 ' the DS1820 is finished 'Send read scratchpad commandOWrite DQ,1,[$55,$10,$31,$C5,$C8,$00,$00,$00,$F4,$BE] ' FF I1 I2 I3 I4 I5 I6 CRC ' FF - Family, Ix - Address bytes, ' CRC - checksum checksumORead DQ,2,[Temp.LowByte,Temp.HighByte,Cnt,Cnt,Cnt,Cnt,Cnt,Count_Per_Deg] ' Each Cnt is 1/16 of a degree, therefore 100/16 = 6.25, and that's our decimal valueTemp_Dec = (6.25 * Cnt) ' Bits 8-15 are 1 for a negative temperature and 0 for a positiveIf Temp.8=1 Then ' If negative then drop the first bit, and invert the value Temp=(Temp.LowByte ^ $FF) >> 1 ' If Cnt = 0 then increment Temp, as the 1820 does not do this If Cnt = 0 Then Temp = Temp + 1 ' Change the sign to a negative Sign = "-" Else ' If positive then drop the first byte Temp=(Temp >> 1) ' And change the sign value to positive Sign = "+" ' Invert the decimal value Temp_Dec = 100 - Temp_Dec EndIf ' Display the data on the LCDPrint At 1,1, Sign, Dec Temp, ".", Dec Dig Temp_Dec, 1, 0,"C " GoTo Again