rocketman
- 1st October 2004, 14:30
Problem: 16F877a begins sending a infinite series of "1" as output to the RS232 conveter.
From the best I can tell the error originates in my "If Command=Q" statement where I read the TEMP2 on a DS18S20 sensor. I assume I am screwing up the stack somewhere, but I can not tell where.
Here's the code, can someone please tell me where I went wrong:
include "modedefs.bas"
DEFINE OSC 20
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 2 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 100 ' Set sampling time in uS
DEFINE HSER_TXSTA 24H
DEFINE HSER_RCSTA 90H
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR
'LCD SETTINGS
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
COMMAND VAR BYTE[2]
COUNTR VAR BYTE
COUNTC VAR BYTE
DS18S20F VAR WORD
DS18S20 VAR WORD
TEMP1 VAR PORTB.7
TEMP2 VAR PORTB.6
'LIGHTS
PUMP1 VAR PORTC.1
LIGHT VAR PORTE.2
'I/O CONFIGURATION
TRISA=%00011111
TRISB=%00000000
TRISC=%11001011
TRISD=%11110001
TRISE=%00000000
'USART SETTINGS
PIE1=%01110000
RCSTA=%10010000 'ENABLE SERIAL PORT AND RECEPTION
TXSTA=%00100100
'Turn everything off at startup
LOW LIGHT
LOW PUMP1
LCDOUT $FE,1, "RESET"
HSEROUT ["MARINETIX ZEOSC 1.0"]
PAUSE 1000
HSEROUT ["KX"] 'power up responce
MAIN:
If PIR1.5=1 then 'if there is something in the UART buffer read it in
HSERIN ,2000, TIMEOUT, [STR COMMAND\2] 'IF THERE IS SOMETHING IN THE RECIEVE REGISTER STORE IT IN COMMAND
GOSUB EXECUTE
TIMEOUT: 'if it times out, just start over at main
ENDIF
GoTo MAIN
'***** EXECUTE THE INCOMING COMMAND BASED ON THE CHARACTERS RECIEVED ****
EXECUTE:
IF COMMAND(0) = "K" THEN
HSEROUT ["KX"]
ENDIF
IF COMMAND(0)="T" THEN
LOW TEMP1 'initialize DS18S20
PAUSEUS 500 'wait for return pulse
TRISB.7=1 'set pin as input to get temp reading
PAUSEUS 100
IF TEMP1=1 THEN
DS18S20F=999 'VB error code signaling no sensor connected
HSEROUT ["T", DEC (DS18S20F),"X"]
RETURN
ENDIF
PAUSEUS 400
'START TEMP READING
OWOUT TEMP1, 1, [$CC, $44] ' $CC-SKIP SEARCHING FOR MULTIPLE DS18S20, $44-START
WAITFORCONVERSION:
OWIN TEMP1, 4, [COUNTR] 'CHECK FOR STILL BUSY CONVERTING SIGNAL
IF COUNTR=0 THEN WAITFORCONVERSION
OWOUT TEMP1, 1, [$CC, $BE] 'READ THE TEMPERATURE
OWIN TEMP1, 0, [DS18S20.LOWBYTE, DS18S20.HIGHBYTE, SKIP 4, COUNTR, COUNTC]
DS18S20=((DS18S20>>1*100)-25)+(((COUNTC-COUNTR)*100)/COUNTC)
DS18S20F=(DS18S20 */ 461)+3200 'CALCULATE TEMP IN DEGREES F, NOT VALID FOR NEGATIVE TEMP
HSEROUT ["T", DEC (DS18S20F/100), ".", DEC1 (DS18S20F),"X"]
RETURN
ENDIF
IF COMMAND(0)="Q" THEN
LOW TEMP2 'initialize DS18S20
PAUSEUS 500 'wait for return pulse
TRISB.6=1 'set pin as input to get temp reading
PAUSEUS 100
IF TEMP2=1 THEN
DS18S20F=999 'VB error code signaling no sensor connected
HSEROUT ["Q", DEC (DS18S20F),"X"]
RETURN
ENDIF
PAUSEUS 400
'START TEMP READING
OWOUT TEMP2, 1, [$CC, $44] ' $CC-SKIP SEARCHING FOR MULTIPLE DS18S20, $44-START
WAITFORTEMP2:
OWIN TEMP2, 4, [COUNTR] 'CHECK FOR STILL BUSY CONVERTING SIGNAL
IF COUNTR=0 THEN WAITFORTEMP2
OWOUT TEMP2, 1, [$CC, $BE] 'READ THE TEMPERATURE
OWIN TEMP2, 0, [DS18S20.LOWBYTE, DS18S20.HIGHBYTE, SKIP 4, COUNTR, COUNTC]
DS18S20=((DS18S20>>1*100)-25)+(((COUNTC-COUNTR)*100)/COUNTC)
DS18S20F=(DS18S20 */ 461)+3200 'CALCULATE TEMP IN DEGREES F, NOT VALID FOR NEGATIVE TEMP
HSEROUT ["Q", DEC (DS18S20F/100), ".", DEC1 (DS18S20F),"X"]
ENDIF
RETURN
'END OF CODE
From the best I can tell the error originates in my "If Command=Q" statement where I read the TEMP2 on a DS18S20 sensor. I assume I am screwing up the stack somewhere, but I can not tell where.
Here's the code, can someone please tell me where I went wrong:
include "modedefs.bas"
DEFINE OSC 20
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 2 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 100 ' Set sampling time in uS
DEFINE HSER_TXSTA 24H
DEFINE HSER_RCSTA 90H
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR
'LCD SETTINGS
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
COMMAND VAR BYTE[2]
COUNTR VAR BYTE
COUNTC VAR BYTE
DS18S20F VAR WORD
DS18S20 VAR WORD
TEMP1 VAR PORTB.7
TEMP2 VAR PORTB.6
'LIGHTS
PUMP1 VAR PORTC.1
LIGHT VAR PORTE.2
'I/O CONFIGURATION
TRISA=%00011111
TRISB=%00000000
TRISC=%11001011
TRISD=%11110001
TRISE=%00000000
'USART SETTINGS
PIE1=%01110000
RCSTA=%10010000 'ENABLE SERIAL PORT AND RECEPTION
TXSTA=%00100100
'Turn everything off at startup
LOW LIGHT
LOW PUMP1
LCDOUT $FE,1, "RESET"
HSEROUT ["MARINETIX ZEOSC 1.0"]
PAUSE 1000
HSEROUT ["KX"] 'power up responce
MAIN:
If PIR1.5=1 then 'if there is something in the UART buffer read it in
HSERIN ,2000, TIMEOUT, [STR COMMAND\2] 'IF THERE IS SOMETHING IN THE RECIEVE REGISTER STORE IT IN COMMAND
GOSUB EXECUTE
TIMEOUT: 'if it times out, just start over at main
ENDIF
GoTo MAIN
'***** EXECUTE THE INCOMING COMMAND BASED ON THE CHARACTERS RECIEVED ****
EXECUTE:
IF COMMAND(0) = "K" THEN
HSEROUT ["KX"]
ENDIF
IF COMMAND(0)="T" THEN
LOW TEMP1 'initialize DS18S20
PAUSEUS 500 'wait for return pulse
TRISB.7=1 'set pin as input to get temp reading
PAUSEUS 100
IF TEMP1=1 THEN
DS18S20F=999 'VB error code signaling no sensor connected
HSEROUT ["T", DEC (DS18S20F),"X"]
RETURN
ENDIF
PAUSEUS 400
'START TEMP READING
OWOUT TEMP1, 1, [$CC, $44] ' $CC-SKIP SEARCHING FOR MULTIPLE DS18S20, $44-START
WAITFORCONVERSION:
OWIN TEMP1, 4, [COUNTR] 'CHECK FOR STILL BUSY CONVERTING SIGNAL
IF COUNTR=0 THEN WAITFORCONVERSION
OWOUT TEMP1, 1, [$CC, $BE] 'READ THE TEMPERATURE
OWIN TEMP1, 0, [DS18S20.LOWBYTE, DS18S20.HIGHBYTE, SKIP 4, COUNTR, COUNTC]
DS18S20=((DS18S20>>1*100)-25)+(((COUNTC-COUNTR)*100)/COUNTC)
DS18S20F=(DS18S20 */ 461)+3200 'CALCULATE TEMP IN DEGREES F, NOT VALID FOR NEGATIVE TEMP
HSEROUT ["T", DEC (DS18S20F/100), ".", DEC1 (DS18S20F),"X"]
RETURN
ENDIF
IF COMMAND(0)="Q" THEN
LOW TEMP2 'initialize DS18S20
PAUSEUS 500 'wait for return pulse
TRISB.6=1 'set pin as input to get temp reading
PAUSEUS 100
IF TEMP2=1 THEN
DS18S20F=999 'VB error code signaling no sensor connected
HSEROUT ["Q", DEC (DS18S20F),"X"]
RETURN
ENDIF
PAUSEUS 400
'START TEMP READING
OWOUT TEMP2, 1, [$CC, $44] ' $CC-SKIP SEARCHING FOR MULTIPLE DS18S20, $44-START
WAITFORTEMP2:
OWIN TEMP2, 4, [COUNTR] 'CHECK FOR STILL BUSY CONVERTING SIGNAL
IF COUNTR=0 THEN WAITFORTEMP2
OWOUT TEMP2, 1, [$CC, $BE] 'READ THE TEMPERATURE
OWIN TEMP2, 0, [DS18S20.LOWBYTE, DS18S20.HIGHBYTE, SKIP 4, COUNTR, COUNTC]
DS18S20=((DS18S20>>1*100)-25)+(((COUNTC-COUNTR)*100)/COUNTC)
DS18S20F=(DS18S20 */ 461)+3200 'CALCULATE TEMP IN DEGREES F, NOT VALID FOR NEGATIVE TEMP
HSEROUT ["Q", DEC (DS18S20F/100), ".", DEC1 (DS18S20F),"X"]
ENDIF
RETURN
'END OF CODE