PDA

View Full Version : 16F877, DS18S20 and Serial Comm Problem



YellowTang
- 24th April 2004, 05:05
I have a very simple PIC program that reads the temperature on a DS18S20 one-wire temperature sensor then send that reading through the USART via serial comm to a VB GUI.
From there VB collects the data via the OnComm event and processes it to the operator.

Problem is, eventually the PIC flips out ans starts sending bad data

Here's the PIC code to collect the temp and send it to the VB GUI:

include "modedefs.bas"

DEFINE OSC 20
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

DEFINE HSER_TXSTA 24H
DEFINE HSER_RCSTA 90H
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR

COMMAND VAR BYTE[2]
RESPONSE VAR BYTE[2]

COUNTR VAR BYTE
COUNTC VAR BYTE
DS18S20F VAR WORD
DS18S20 VAR WORD

TEMP1 VAR PORTB.7

'I/O CONFIGURATION

ADCON0 = %10000001
ADCON1 = %11000100 '%11000000
TRISA=%10011111
TRISB=%00000000
TRISC=%11001011
TRISD=%11110001 '11110001
TRISE=%00000000

'USART SETTINGS
PIE1=%01110000
RCSTA=%10010000 'ENABLE SERIAL PORT AND RECEPTION
TXSTA=%00100100

MAIN:

GOSUB CommandIN

GoTo MAIN

CommandIN:

If PIR1.5=1 then

HSERIN ,2000, TIMEOUT, [STR COMMAND\2] 'IF THERE IS SOMETHING IN THE RECIEVE REGISTER STORE IT IN "COMMAND"

GOSUB EXECUTE
TIMEOUT:'HSEROUT ["TIMEOUT"]
ENDIF

RETURN

'***** EXECUTE THE INCOMING COMMAND BASED ON THE CHARACTERS RECIEVED ****

EXECUTE:

IF COMMAND(0)="T" THEN
GOSUB READTEMP

RESPONSE(0)=COMMAND(0)
'HSEROUT [RESPONSE(0), DEC (DS18S20F/100), ".", DEC1 (DS18S20F),"X"]
HSEROUT [RESPONSE(0), DEC (DS18S20F),"X"]

ENDIF
RETURN


READTEMP:
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
RETURN
ENDIF
PAUSEUS 400

OWOUT TEMP1, 1, [$CC, $44] ' $CC-SKIP SEARCHING FOR MULTIPLE DS18S20, $44-START

TEMPERATURE CONVERSION

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

RETURN
'*****************************************

Now, the problem is that after about 60 minutes of collecting temp readings and sending them to VB the PIC or temp sensor flips out and starts sending the same digit over and over. Here is a sampling of the data received from the pic (VB adds the date/time stamp):

T7228X 4/21/2004 9:55:06 AM
T744X 4/21/2004 10:00:06 AM
T741X 4/21/2004 10:05:06 AM
T735X 4/21/2004 10:10:07 AM
T742X 4/21/2004 10:15:07 AM
T763X 4/21/2004 10:20:07 AM
T729X 4/21/2004 10:25:07 AM
T728X 4/21/2004 10:30:07 AM
T724X 4/21/2004 10:35:07 AM
T769X 4/21/2004 10:40:07 AM
T762X 4/21/2004 10:45:07 AM
T60X 4/21/2004 10:50:13 AM "**********This is where things go wrong******************
T7228X80 4/21/2004 10:55:13 AM
008X 4/21/2004 11:00:13 AM
008X.888 4/21/2004 11:05:13 AM
008X.888.888 4/21/2004 11:10:13 AM
008X.888.888.888 4/21/2004 11:15:13 AM
008X.888.888.888.888 4/21/2004 11:20:13 AM
008X.888.888.888.888.888 4/21/2004 11:25:13 AM '*VB clears the buffer and it starts over
.888 4/21/2004 11:30:13 AM
.888.888 4/21/2004 11:35:13 AM
.888.888.888 4/21/2004 11:40:13 AM

and so on, and so forth.....

Any idea what is going on? Where do I start trouble shooting?

Melanie
- 26th April 2004, 10:36
Have you determined whether your problem is with the PIC or with VB's comms handling?

There is a limit to VB's string handling capability where when the limit is reached it with either hang or corrupt incomming data. If this is the problem, the solution is simply to terminate each transmission sequence from the PIC with a Return/Line-Feed combination.

Also, as a note, jumping into the middle of IF/ENDIF statements is bad programming, and whilst I cannot say if PBP will tollerate this, I do know many compilers that will behave unexpectedly... so this...

CommandIN:
If PIR1.5=1 then
HSERIN ,2000, TIMEOUT, [STR COMMAND\2] etc
GOSUB EXECUTE
TIMEOUT:
ENDIF
RETURN

... is better written like so...

CommandIN:
If PIR1.5=1 then
HSERIN ,2000, TIMEOUT, [STR COMMAND\2] etc
GOSUB EXECUTE
ENDIF
TIMEOUT:
RETURN

The result is the same.

Melanie