I am having trouble getting a DS18B20 to work.
The program is basically one of the examples provided with MB.
I am sure I have had it working before, but now it gives an output of
+85.0000 C. and does not change.
Can anyone please help.
Best regards
PortlandJohn
Code: Select all
'******************************************************************************
' microcontroller : P18F8722
'
' Project: onewire
'
' Date/Author: 2006-02-07 (MJ)
' Description:
' This project is designed to work with PIC P18F8722
' with minor adjustments, it should work with any other PIC MCU
'
' This code demonstrates one-wire communication with temperature sensor DS18B20
' connected to RA5 pin.
' After reset, PIC obtaines temperature from sensor and prints it on LCD 4 bit
'******************************************************************************
program onewire
dim Raw_temp, TempC, comma as word
dim i, j1, j2, minus as byte
dim text as string[14]
'* Initialise LCD **************************************************************
Sub Procedure Init_LCD
Lcd_Config(PORTH,7,6,5,4,PORTH,1,2,3)' set the Port and pin used for LCD
LCD_Init(PORTH) ' configure PORTB 4 line LCD
LCD_Cmd(LCD_CLEAR) ' Clear display
LCD_Cmd(LCD_RETURN_HOME) ' Turn cursor home
Lcd_Cmd(LCD_CURSOR_Off) ' Turn cursor OFF
Delay_ms(500)
LCD_Out(1,1,"BIGPIC4 10MHz ") ' print to row 1 col 1
LCD_Out(2,1,"Temp DS18B20 ") ' print to row 2 col 2
LCD_Out(4,1,"V1 Port J,0 ") ' print to row 2 col 2
Delay_ms(2000)
LCD_Cmd(LCD_CLEAR) ' Clear display
End Sub
'* Read Temperature ************************************************************
Sub Procedure Read_Temp
ow_reset(PORTJ, 1) ' onewire reset signal
ow_write(PORTJ, 1, $CC) ' SKIP ROM [CCh]
ow_write(PORTJ, 1, $44) ' CONVERT T [44h]
delay_us(120)
i = ow_reset(PORTJ, 1) ' 0 - present, 1 - not present
ow_write(PORTJ, 1, $CC) ' SKIP ROM [CCh]
ow_write(PORTJ, 1, $BE) ' Write to SCRATCHPAD [BEh]
j1 = ow_Read(PORTJ, 1) ' Read LSB
j2 = ow_Read(PORTJ, 1) ' Read MSB
minus = j2
minus = minus >> 3
if minus = $1F then ' Checking temp.(+ or -)
j2 = not j2
j1 = not j1
j1 = j1 + 1
end if
Raw_temp = (j2 << 8) or j1 ' Getting RAW data
if minus = $1F then
Lcd_Chr(2, 1, "-") ' Write temp. sign (+ or -) on LCD
else
Lcd_Chr(2, 1, "+")
end if
TempC = (Raw_temp and $0FF0) >> 4 ' WHOLE NUMBER
comma = (j1 and $0F) * 625 ' DECIMAL
End Sub
'* Display Temperature *********************************************************
Sub Procedure Disp_Temp
WordToStrWithZeros(TempC, text) ' Write temp. whole number on LCD
Lcd_Chr(2, 3, text[3])
Lcd_Chr(2, 4, text[4])
Lcd_Chr(2, 5, ".")
WordToStrWithZeros(comma, text) ' Write temp. decimal number on LCD
Lcd_Chr(2, 6, text[1])
Lcd_Chr(2, 7, text[2])
Lcd_Chr(2, 8, text[3])
Lcd_Chr(2, 9, text[4])
Lcd_Chr(2, 10, 223) ' degree' character
Lcd_Chr(2, 11, "C")
Delay_ms(500)
' endless loop
End Sub
'**** main Program ************************************************************
main:
CMCON = CMCON or $07 ' turn off comparators
ADCON1 = $0F ' set analog inputs
MEMCON.EBDIS = 1 ' disable external memory bus
TRISJ = %11111111 ' designate porte as input
Init_LCD
while TRUE
Read_temp
Disp_Temp
wend
end.
'********* End of Program ****************************************************