Max Power
- 14th July 2009, 20:29
Hey everybody,
Ok, so I was encouraged to see working programs for the DS18B20 here on the fourm. Now I'm trying to make one work on a 16F628A, and I'm getting stuck. I grabbed the code at the bottom of this thread called 'DS18B20' by Ahmed salah (the most recent 18B20 post). I also tried the bit of code from fratello in that same post. I've got pbp running in MPlab, pickit2, connected to a olimex pic-18 development board. I started with a character lcd connected, but now I'd just like to get temperature data out over RS-232..
What I'm seeing is that when the first owout command is executed, you see data on the one-wire that never stops... I never get to the 'waiting for presence pulse' debug message. It's like I'm in an infinite loop sending data.
also, what is modedefs.bas? I don't have any file named modedefs, but it compiles Ok.
Thanks a lot,
Max Power
here is the code
'PIC16F628A Configuration
' ================
@ __CONFIG _HS_OSC & _MCLRE_ON & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF
DEFINE OSC 20 ' external oscillator
DEFINE DEBUG_REG PORTB 'RS-232 output on PORTB.4
DEFINE DEBUG_BIT 4
DEFINE DEBUG_BAUD 19200
DEFINE DEBUG_MODE 0 'not inverted. using RS-232 module
DEFINE LCD_LINES 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 100
CMCON=7
TRISB = %00000100
TRISA = %00000000
DEBUG REP $00\8,13,10,"Power Up"
INCLUDE "modedefs.bas"
Temperature1 Var Word ' Temperature storage
TempC Var Word
Float Var Word
Sign Var Byte ' +/- sign
Mode Var Byte ' 0=Temp. display, 1=Set Temp, 2=Set Hysteresis
DQ Var PORTB.0 ' One-wire data pin
Serial Var PORTB.1
Twist Var Bit
Dummy Var Byte
V Var Word
Busy Var BIT
DS18B20_1_12bit CON %01111111 ; 750ms, 0.0625°C (default)
PushB var PORTB.2
REDLED var PORTB.3
REDLED = 1
pause 1000
REDLED = 0
Twist = 0
MainLoop:
DEBUG REP $00\8,13,10,"Starting 'MainLoop'"
DEBUG REP $00\8,13,10,"push button to run owout"
gosub waitfor_b_press
'''''''''''''OWOut DQ, 1, [$CC, $4E,DS18B20_1_12bit]
OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_1_12bit]
Output DQ ' Make Pin Output
DQ=0 ' OneWire line Low
PauseUs 480 ' Keep down for 480 µS
Input DQ ' Make Pin Input
PauseUs 70 ' Wait 70 µS
DEBUG REP $00\8,13,10,"waiting for presence pulse"
If DQ=1 then ' No presence pulse from DS1820
DEBUG REP $00\8,13,10,"No sensor" ' Show message
Pause 1000 ' Wait 1 Sec.
Goto MainLoop ' Check again
EndIf
DEBUG REP $00\8,13,10,"sensor is good"
gosub waitfor_b_press
'Main :
Init1:
''''''''''''''''''''OWOut DQ, 1, [$CC,$4E,$FF,$FF,$7F]
''''''''''''''''''''OWOut DQ, 1, [$CC,$4E,DS18B20_1_12bit]
OWOUT DQ, 1, [$CC,$4E, 0, 0, DS18B20_1_12bit]
OWOut DQ, 1, [$CC,$48]
OWOut DQ, 1, [$CC,$B8]
OWOut DQ, 1, [$CC,$BE]
Pause 1000
'''''''''''''''''''''OWin DQ, 0, [Temperature1.Byte0, Temperature1.Byte1]
OWIn DQ, 2, [temperature1.byte0, temperature1.byte1]
DEBUG REP $00\8,13,10," sensor OK"
'================================================= ================================================== ===============
Main :
Part1:
OWOut DQ, 1, [$CC,$44] ' Start temperature conversion Sensor 1
WaitLoop:
OWIn DQ, 4, [Busy] ' Check for still busy converting
REDLED = 1 ' Show waiting for conv complete
If Busy = 0 Then
GOTO waitloop
ENDIF
REDLED = 0
OWOut DQ, 1, [$CC,$BE]
Pause 500
'''''''''''''''''''''''OWIn DQ, 0, [Temperature1.Byte0, Temperature1.Byte1]
OWIn DQ, 2, [temperature1.byte0, temperature1.byte1]
'''''''''''''''''''''''
Twist = 0
'''''''''''''''''''''''
If Temperature1.15 then
Temperature1= ~Temperature1 +1
Twist = 1
Endif
Dummy = 625 * Temperature1
TempC = DIV32 10
TempC = (Temperature1 & $7FF) >> 4
Float = ((Temperature1.Lowbyte & $0F ) * 25 )>>2
Temperature1 = TempC*100 + Float
If Twist then
V= 10000 - Temperature1 ' 25 C=12500 0 C=10000 -10 C=9000
Twist = 0
else
V= 10000 + Temperature1
EndIf
If V >= 10000 then ' Above 0 C.
Temperature1=V-10000
Else
Temperature1=10000-V ' Below 0 C.
EndIf
GoSub SelectSign
DEBUG REP $00\8,13,10, "IN ", Sign, DEC (Temperature1 / 100), ".", DEC2 Temperature1, " ",223,"C "
DEBUG REP $00\8,13,10, BIN16 Temperature1
END
'================================================= ================================================== ================
'GoSub SelectSign ' +/blank/- Sign
'Goto Main ' Oare merge pus asta aici ?
'Goto MainLoop ' Do it forever
'================================================= ================================================== =======================
SelectSign:
If v = 10000 then ' Temperature = 0 C.
Sign=" " ' No sign
Else
If v < 10000 then ' <> 0
Sign="-" ' Temperature below 0 C.
Else
Sign="+" ' Temperature above 0 C.
EndIf
EndIf
Return
waitfor_b_press:
REDLED = 1
WHILE PushB = 1
WEND
REDLED = 0
RETURN
Ok, so I was encouraged to see working programs for the DS18B20 here on the fourm. Now I'm trying to make one work on a 16F628A, and I'm getting stuck. I grabbed the code at the bottom of this thread called 'DS18B20' by Ahmed salah (the most recent 18B20 post). I also tried the bit of code from fratello in that same post. I've got pbp running in MPlab, pickit2, connected to a olimex pic-18 development board. I started with a character lcd connected, but now I'd just like to get temperature data out over RS-232..
What I'm seeing is that when the first owout command is executed, you see data on the one-wire that never stops... I never get to the 'waiting for presence pulse' debug message. It's like I'm in an infinite loop sending data.
also, what is modedefs.bas? I don't have any file named modedefs, but it compiles Ok.
Thanks a lot,
Max Power
here is the code
'PIC16F628A Configuration
' ================
@ __CONFIG _HS_OSC & _MCLRE_ON & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF
DEFINE OSC 20 ' external oscillator
DEFINE DEBUG_REG PORTB 'RS-232 output on PORTB.4
DEFINE DEBUG_BIT 4
DEFINE DEBUG_BAUD 19200
DEFINE DEBUG_MODE 0 'not inverted. using RS-232 module
DEFINE LCD_LINES 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 100
CMCON=7
TRISB = %00000100
TRISA = %00000000
DEBUG REP $00\8,13,10,"Power Up"
INCLUDE "modedefs.bas"
Temperature1 Var Word ' Temperature storage
TempC Var Word
Float Var Word
Sign Var Byte ' +/- sign
Mode Var Byte ' 0=Temp. display, 1=Set Temp, 2=Set Hysteresis
DQ Var PORTB.0 ' One-wire data pin
Serial Var PORTB.1
Twist Var Bit
Dummy Var Byte
V Var Word
Busy Var BIT
DS18B20_1_12bit CON %01111111 ; 750ms, 0.0625°C (default)
PushB var PORTB.2
REDLED var PORTB.3
REDLED = 1
pause 1000
REDLED = 0
Twist = 0
MainLoop:
DEBUG REP $00\8,13,10,"Starting 'MainLoop'"
DEBUG REP $00\8,13,10,"push button to run owout"
gosub waitfor_b_press
'''''''''''''OWOut DQ, 1, [$CC, $4E,DS18B20_1_12bit]
OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_1_12bit]
Output DQ ' Make Pin Output
DQ=0 ' OneWire line Low
PauseUs 480 ' Keep down for 480 µS
Input DQ ' Make Pin Input
PauseUs 70 ' Wait 70 µS
DEBUG REP $00\8,13,10,"waiting for presence pulse"
If DQ=1 then ' No presence pulse from DS1820
DEBUG REP $00\8,13,10,"No sensor" ' Show message
Pause 1000 ' Wait 1 Sec.
Goto MainLoop ' Check again
EndIf
DEBUG REP $00\8,13,10,"sensor is good"
gosub waitfor_b_press
'Main :
Init1:
''''''''''''''''''''OWOut DQ, 1, [$CC,$4E,$FF,$FF,$7F]
''''''''''''''''''''OWOut DQ, 1, [$CC,$4E,DS18B20_1_12bit]
OWOUT DQ, 1, [$CC,$4E, 0, 0, DS18B20_1_12bit]
OWOut DQ, 1, [$CC,$48]
OWOut DQ, 1, [$CC,$B8]
OWOut DQ, 1, [$CC,$BE]
Pause 1000
'''''''''''''''''''''OWin DQ, 0, [Temperature1.Byte0, Temperature1.Byte1]
OWIn DQ, 2, [temperature1.byte0, temperature1.byte1]
DEBUG REP $00\8,13,10," sensor OK"
'================================================= ================================================== ===============
Main :
Part1:
OWOut DQ, 1, [$CC,$44] ' Start temperature conversion Sensor 1
WaitLoop:
OWIn DQ, 4, [Busy] ' Check for still busy converting
REDLED = 1 ' Show waiting for conv complete
If Busy = 0 Then
GOTO waitloop
ENDIF
REDLED = 0
OWOut DQ, 1, [$CC,$BE]
Pause 500
'''''''''''''''''''''''OWIn DQ, 0, [Temperature1.Byte0, Temperature1.Byte1]
OWIn DQ, 2, [temperature1.byte0, temperature1.byte1]
'''''''''''''''''''''''
Twist = 0
'''''''''''''''''''''''
If Temperature1.15 then
Temperature1= ~Temperature1 +1
Twist = 1
Endif
Dummy = 625 * Temperature1
TempC = DIV32 10
TempC = (Temperature1 & $7FF) >> 4
Float = ((Temperature1.Lowbyte & $0F ) * 25 )>>2
Temperature1 = TempC*100 + Float
If Twist then
V= 10000 - Temperature1 ' 25 C=12500 0 C=10000 -10 C=9000
Twist = 0
else
V= 10000 + Temperature1
EndIf
If V >= 10000 then ' Above 0 C.
Temperature1=V-10000
Else
Temperature1=10000-V ' Below 0 C.
EndIf
GoSub SelectSign
DEBUG REP $00\8,13,10, "IN ", Sign, DEC (Temperature1 / 100), ".", DEC2 Temperature1, " ",223,"C "
DEBUG REP $00\8,13,10, BIN16 Temperature1
END
'================================================= ================================================== ================
'GoSub SelectSign ' +/blank/- Sign
'Goto Main ' Oare merge pus asta aici ?
'Goto MainLoop ' Do it forever
'================================================= ================================================== =======================
SelectSign:
If v = 10000 then ' Temperature = 0 C.
Sign=" " ' No sign
Else
If v < 10000 then ' <> 0
Sign="-" ' Temperature below 0 C.
Else
Sign="+" ' Temperature above 0 C.
EndIf
EndIf
Return
waitfor_b_press:
REDLED = 1
WHILE PushB = 1
WEND
REDLED = 0
RETURN