How come my DS18S20 code still works?
I have been trying for 2 weeks to get my project working properly and now I am at my wits end.
Part of my project is to read a DS18S20 and display the correct termperature on a 2x16 lcd display. I have searched the forum and all the examples for reading the device are the same so I just used it and it worked great.
Problem is I cannot understand what the count_remain variable actually does. I am also confused about the count_per_c variable. So I did what any older person would do and just remarked them out of the code to see what would happen. But nothing did. Everything worked the same.
the code is as follows:
Hope someone can help
Wilson
@ DEVICE pic16F628a, INTRC_OSC_NOCLKOUT,LVP_OFF,WDT_OFF,MCLR_OFF,PROTEC T_OFF
@ DEVICE pic16F628a, CPD_OFF
trisa =%00100000
trisb =%00000000
cmcon =%00000111 'Comparators Off
Define LCD_BITS 4
Define LCD_DREG PORTB
Define LCD_DBIT 0
Define LCD_RWREG PORTA
Define LCD_RWBIT 4
Define LCD_RSREG PORTA
Define LCD_RSBIT 3
Define LCD_EREG PORTA
Define LCD_EBIT 2
Define LCD_COMMANDUS 5000
Define LCD_DATAUS 100
Define LCD_INITMS 2
pause 100
DQ VAR PORTB.5 ' One-wire data pin
temperature VAR WORD ' Temperature storage
'I removed the next two lines
'count_remain VAR BYTE ' Count remaining
'count_per_c VAR BYTE ' Count per degree C
loop:
LCDOUT $FE, 1
LCDOut $FE, $C0, "Temp ", dec(temperature / 100)," C "
OWOut DQ, 1, [$CC, $44] ' Start temperature conversion
pause 750 'NEEDED FOR TEMP STABILIZING
'and the following three lines
'waitloop:
'OWIn DQ, 4, [count_remain] ' Check for still busy converting
'IF count_remain = 0 Then waitloop
OWOut DQ, 1, [$CC, $BE] ' Read the temperature
'as well as the last bits of the next two lines
OWIn DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]', Skip 4, count_remain, count_per_c]
temperature = (((temperature >> 1) * 100) - 25)' + (((count_per_c - count_remain) * 100) / count_per_c)
goto loop
Re: How come my DS18S20 code still works?
my code just gives me the same output, I'm trying to use 4 7 segment displays can you all help me I am useing a 18f452
clear
define osc 20
include "modedefs.bas"
trisa=%11111111 ' a as input
trisb=0 'port b as output
trisd=%00001111 'portd as output 7-4 & 0-3 as input
digit var byte
pattern var byte
digit1 var portd.7
digit2 var portd.6
digit3 var portd.5
digit4 var portd.4
'DS18S20_9bit con %00011111;93.75ms,0.5c
'DS18S20_10bit con %00111111;187.5ms,0.25c
'DS18S20_11bit con %01011111;375ms,0.125c
DS18S20_12bit con %01111111;750ms,0.0625c default
i var word
DQ var PORTA.4 ' one wire data pin
Temp_C var word
SignC var word
Busy var byte
Negative var word
Positive var word
Temper var word
count_remain var byte
count_per_c var word
digit4=0
digit3=0
digit2=0
digit1=0
Start:
temper.lowbyte=0
temper.highbyte=0
count_remain=0
owout DQ,1,[$CC,$B4] 'test for parasite power or external supplies
pause 10 'wait for power test
owout DQ,1,[$CC,$4E,0,0,DS18S20_12bit]
owout DQ,1,[$CC,$44] 'start conversion
Waitloop: ' check for still busy converting
owin DQ,4,[count_remain]
if Count_remain=0 then goto Waitloop
owout DQ,1,[$CC,$BE] 'read scratchpad
owin DQ,0,[temper.lowbyte,temper.highbyte,skip 4,count_remain,count_per_c]
'calculate temperature in c to 2 decimal
temp_C=((( Temper>>1)*100)-25)+(((count_per_c - count_remain)*100)/count_per_c)
' 7 segment 4 led display
digit=Temp_C dig 3 'get 1000s
gosub convert
portb=pattern
digit4=0
digit3=0
digit2=0
digit1=1
pause 5
digit=Temp_C dig 2 'get 100s
gosub convert
portb=pattern
digit4=0
digit3=0
digit2=1
digit1=0
portb.7=1
pause 5
digit=Temp_C dig 1 'get 10s
gosub convert
portb=pattern
digit4=0
digit3=1
digit2=0
digit1=0
portb.7=0
pause 5
digit=Temp_C dig 0 'get 1s
gosub convert
portb=pattern
digit4=1
digit3=0
digit2=0
digit1=0
pause 5
digit4=0
goto Start
end
convert:
lookup digit,[$3f,$06,$5b,$4f,$66,$6d,$7d,$07,$7f,$6f],pattern
portb=pattern
return