O.K., I worked that one out. Stay tuned, I may have this licked.......
O.K., I worked that one out. Stay tuned, I may have this licked.......
Can't seem to find the subroutine "ReceiveData" anywhere, so my program just does some kind of permanent loop. There must be a wall nearby I can bang my head off a few times....................
This is the code I'm using, but it gets stuck at the WHILE/WEND bits in the ReadSensor subroutine. HELP!!!!!!!!!!!
INCLUDE "modedefs.bas"
CRC VAR word
tempc var word
signc var word
result var word
readhumidity var word
sensorcmd var word
readtemp var word
w var word
x var word
temp var word
rhtempcomp var word
rhlinear var word
byte0 var word
byte1 var word
databyte var word
databit var word
i var word
'include "16F877A"
' Define LCD pins
Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 1
Define LCD_EREG PORTB
Define LCD_EBIT 3
Low PORTB.2 ' LCD R/W line low (W)
Pause 100 ' Wait for LCD to start
lcdout $fe,1, "Temperature and" ' show on LCD
lcdout $fe, $c0 ,"Humidity sensor." ' show on LCD
pause 2000 ' Wait 2 seconds
lcdout $fe,1 ' Clear screen
Goto mainloop ' Skip subroutines, start main program
mainloop:
lcdout $fe,1,"Temp ", # tempc
lcdout $fe,$c0,"Humidity ",#RHTempComp
gosub ReadSHT71
pause 100
goto mainloop
end
'read the sensor thingie
ReadSHT71:
Sensorcmd = ReadTemp
gosub readsensor
if result => 4000 then
tempC = result - 4000
SignC = 160
else
tempC = 4000 - result
SignC = "-"
endif
gosub Initialize
Sensorcmd = ReadHumidity
gosub readsensor
'--------------------- Calculate Relative Humidity -----------------------------
w = result * 405
w = div32 100
x = result * result
x = div32 1000
x = x * 28
x = div32 100
RHLinear = w - x - 400
'---------- Calculate Relative Humidity with Temperature Compensation ----------
w = (8 * result + 1000) / 10
if SignC = 160 then
if tempC > 2500 then
x = (tempC - 2500) * w
x = div32 100
RHTempComp = RHLinear + x / 100
else
x = (2500 - temp) * w
x = div32 100
RHTempComp = RHLinear - x / 100
endif
else
x = (2500 + tempC) * w
x = div32 10000
RHTempComp = RHLinear - x
endif
sleep 1
return
'---------------------------- Initialize the Sensor ----------------------------
Initialize:
high PORTD.0
low PORTD.1
for i = 1 to 10
high PORTD.1
pause 1
low PORTD.1
pause 1
next i
call TransferStart
return
'---------------------------- Get Data from Sensor -----------------------------
ReadSensor:
gosub TransferStart
gosub WaitSensor
shiftout PORTD.0,PORTD.1,1,[Sensorcmd\8] ' Send command byte
input PORTD.0 ' Wait for acknowledge
low PORTD.1
while PORTD.0 = 1
wend
pulsout PORTD.1,10 ' Send acknowledge
while PORTD.0 = 0
wend
while PORTD.0 = 1 ' Wait for conversion to complete
wend
low PORTD.1
shiftin PORTD.0,PORTD.1,0,[result.byte1\8] ' Get the first byte, 8 bits
low PORTD.0
pulsout PORTD.1,10 ' Send acknowledge
shiftin PORTD.0,PORTD.1,0,[result.byte0\8] ' Get the second byte, 8 bits
low PORTD.0
pulsout PORTD.1,10 ' Send acknowledge
shiftin PORTD.0,PORTD.1,0,[crc\8] ' Get third byte, 8 bits, CRC
high PORTD.0
pulsout PORTD.1,10 ' Send acknowledge
input PORTD.0 ' End of Transmission
input PORTD.1
return
'---------------------------- Start Transfer -----------------------------------
TransferStart:
high PORTD.1
pause 1
low PORTD.0
pause 1
low PORTD.1
pause 1
high PORTD.1
pause 1
high PORTD.0
pause 1
low PORTD.1
return
'---------------------------- Wait for Sensor ----------------------------------
WaitSensor:
result = 4096
Loop:
result = result - 1
if PORTD.0 && result.bit11 then Loop
return
Hello gringobomba14,
If you have any extra ports available you could put some code in those empty While Wend loops to light leds to tell you which loop is stalled, or add some serout commands to do the same. or put in an escape counter in the loops.
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Try this:
Had a couple of subtle math errors, parenthesis type things..Code:Define LCD_DREG PORTB Define LCD_DBIT 4 Define LCD_RSREG PORTB Define LCD_RSBIT 1 Define LCD_EREG PORTB Define LCD_EBIT 3 INCLUDE "modedefs.bas" crc var word:tempc var word:signc var word:result var word readhumidity var word:sensorcmd var word:readtemp var word:w var word x var word:temp var word:rhtempcomp var word:rhlinear var word:byte0 var byte byte1 var byte:databyte var byte:databit var bit:i var word:dpin var portd.0 cpin var portd.1:portb.2=0:output portb.2:Pause 1000 lcdout $fe,1, "Temperature and",$fe, $c0 ,"Humidity sensor.":pause 2000 lcdout $fe,1:Goto mainloop ' Skip subroutines, start main program mainloop: lcdout $fe,1,"Temp ", #tempc,$fe,$c0,"Humidity ",#RHTempComp gosub ReadSHT71:pause 100:goto mainloop ReadSHT71: Sensorcmd = ReadTemp : gosub readsensor if result => 4000 then tempC = result - 4000 : SignC = 160 else tempC = 4000 - result : SignC = "-" endif gosub Initialize : Sensorcmd = ReadHumidity : gosub readsensor w = result * 405 : w = div32 100 : x = result * result : x = div32 1000 x = x * 28 : x = div32 100 : RHLinear = w - x - 400 w = ( (8 * result ) + 1000) / 10 if SignC = 160 then if tempC > 2500 then x = (tempC - 2500) * w : x = div32 100 RHTempComp = ( RHLinear + x ) / 100 else x = (2500 - temp) * w : x = div32 100 RHTempComp = ( RHLinear - x ) / 100 endif else x = (2500 + tempC) * w : x = div32 10000 RHTempComp = RHLinear - x endif sleep 1 : return Initialize: dpin=1:cpin=0:for i=1 to 10:cpin=1:pause 1:cpin=0:pause 1:next i gosub TransferStart : return ReadSensor: gosub TransferStart : gosub WaitSensor shiftout dpin,cpin,0,[Sensorcmd\8] input dpin : cpin = 0 while dpin = 1 : wend cpin = 1 : pause 1 : cpin = 0 while dpin = 0 : wend while dpin = 1 : wend cpin = 0 : shiftin dpin,cpin,0,[result.byte1\8] : dpin = 0 : cpin = 1 pause 1 : cpin = 0 : shiftin dpin,cpin,0,[result.byte0\8] : dpin = 0 cpin = 1 : pause 1 : cpin = 0 : shiftin dpin,cpin,0,[crc\8] : dpin = 1 cpin = 1 : pause 1 : cpin = 0 : input dpin : input cpin : return TransferStart: cpin = 1 : pause 1 : dpin = 0 : pause 1 : cpin = 0 pause 1 : cpin = 1 : pause 1 : dpin = 1 : pause 1 : cpin = 0 : pause 1 return WaitSensor: result = 4096 Loop: result = result - 1 : if PORTD.0 & result.bit11 then Loop return END
Looks like the mode might've been wrong in a couple of places.
The END wasn't at the bottom...not that it makes much of a difference.
And it's been COLON-IZED!![]()
Last edited by skimask; - 19th June 2008 at 06:02.
Cheers Ski, looks more compact, and forgot about those colon type things to seperate code. AM I missing something else, because now I get this
ERROR: Macro AND?TTL not found in macro file.
I recall some kind of Math Macro...........
Last edited by gringobomba14; - 19th June 2008 at 07:54. Reason: bad grammer, misspelled words, and it was raining
Bookmarks