Device = 16F877A
XTAL = 4
ALL_DIGITAL = False
PORTB_PULLUPS = True
LCD_DTPIN = PORTB.4
LCD_RSPIN = PORTB.2
LCD_ENPIN = PORTB.3
LCD_INTERFACE = 4 ' Setup the LCD
LCD_LINES = 2
LCD_TYPE = 0
Declare SCL_PIN PORTC.0
Declare SDA_PIN PORTC.1
Dim Result As Float ' Create a register accurate from -2147483646.999 to +2147483646.999
Dim Sign As Byte
Dim Cnt As Byte
Dim Count_Per_Deg As Byte
Dim Temp As Word
Dim Temp_Dec As Byte
Symbol DQ=PORTD.1 'Place the DS1820 on bit 1 of PORTD
DelayMS 150
Cls
Print At 2, 1, "Power is On"
TRISA = %11111111
ADCON1 = %10000000
Main:
Result = ADIn 0
EWrite 00, Result
Print At 1,1, DEC2 Result * 5 / 1023, "V"
BStart ' Send the start command on I2C
BusOut %10100010,$00,[Result] ' Send the byte to the eeprom
BStop ' Send the stop command on I2C
OWrite DQ, 1,[$55,$10,$92,$1F,$05,$01,$08,$00,$6B,$44] 'Send calculate temperature command
' FF I1 I2 I3 I4 I5 I6 CRC ' FF - Family, Ix - Address byte, CRC - checksum
Repeat
DelayMS 25 'Wait until conversion is compleate
ORead DQ,4,[Cnt] 'Keep reading low puses until
Until Cnt <> 0 'the DS1820 is finished
OWrite DQ,1,[$55,$10,$92,$1F,$05,$01,$08,$00,$6B,$BE] 'Send read scratcpad command
' FF I1 I2 I3 I4 I5 I6 CRC ' FF - Family, Ix - Address byte, CRC - checksum
ORead DQ,2,[Temp.LowByte,Temp.HighByte,Cnt,Cnt,Cnt,Cnt,Cnt,Count_Per_Deg]
Temp_Dec = (6.25 * Cnt) ' Each Cnt is 1/16 of a degree, therefor 100/16 = 6.25, and thats our decimal value
If Temp.8=1 Then ' Bits 8-15 are 1 for a negative temperature and 0 for a positive
Temp=(Temp.LowByte ^ $FF) >> 1 ' If negative then drop the first bit, and invert the value
If Cnt = 0 Then Temp = Temp + 1 ' If Cnt = 0 then increment Temp, as the 1820 does not do this
Sign = "-" ' Change the sign to a negative
Else
Temp=(Temp >> 1) ' If positive then drop the first byte
Temp_Dec = 100 - Temp_Dec
Sign = "+" ' And change the sign value to positive
EndIf
Print At 1,6, Sign, Dec2 Temp, ".", Dec1 Dig Temp_Dec, 1, " DegC "
GoTo Main