PDA

View Full Version : Stopping at 3 minutes!!!



Ramius
- 17th August 2013, 19:20
Okay guys here is a "strange one" I do not understand and do not know how to correct! The program runs fine for exactly 3 minutes and then the value of "FromSensor" stays at a 1 and never changes to a 0! I am running the program with PicBasic Pro Version 5.0.0.5, Complier Version PBPX 3.0.6.4, MPLAB IDE v8.91, and using the ICD in PBP to watch the program run. I have timed when the program stops running and it is consistently at exactly 3 minutes! Stop and Run again and it is like a 3 minute timer starts again! Any ideas as to what is going on and how to solve the problem? Thanks!

'************************************************* ***************
'* Name : Depth.bas *
'* Author : Ed Cannady (Original written by BrianT) *
'* Notice : Copyright (c) 2011 *
'* : All Rights Reserved *
'* Date : 4/4/2011 *
'* Version : 1.0 *
'* Notes : For a 18F1320 and MS-5541 *
'* *
'************************************************* ***************

DEFINE OSC 20 'Define crystal frequency

DEFINE CCP1_REG PORTB ' Use CCP1 for 32768 master clock PORTB.3
DEFINE CCP1_BIT 3 ' Use CCP1 for 32768 master clock PORTB.3

Include "Modedefs.bas"
include "hpwm10L.pbp"
Enable Debug
;18F1320 EXT OSC RUNNING AT 20MHZ TO 40MHZ MCLR AS INPUT AND OSC PINS AS INPUTS

#CONFIG
__CONFIG _CONFIG1H, _HS_OSC_1H & _FSCM_OFF_1H
__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_8K_2H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _BORV_27_2L
__CONFIG _CONFIG3H, _MCLRE_OFF_3H
__CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_ON_4L & _STVR_ON_4L
__CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
__CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
__CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
__CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
__CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
__CONFIG _CONFIG7H, _EBTRB_OFF_7H
#ENDCONFIG

'********************* Declaired Pins **************************

FromSensor VAR PortA.0 'data out from the MS5541
ToSensor var PortA.2 'data in to the MS5541
Sclk var PortA.3 'MS5541 32.768 khz clock
Mclk var PortB.3 'MS5541 Master clock for the MS5541
Reset VAR PortB.0 'Detect when we have surfaced

ADCON1 = 255 ' Make everything digital (no analog)

TRISA.0 = 1 ' Make A.0 an input YEL
TRISA.2 = 0 ' Make A.2 an output GREEN
TRISA.3 = 0 ' Make A.3 an output (Sclk) BLUE
TRISB.3 = 0 ' Make B.3 an output (CCP2) VIOLET
TRISB.0 = 1 ' Make B.0 an input (Reset when we have surfaced)

'************** Intersema Variables *********************************
' C1 to C6 are in EEROM as VAL.byte0, VAL.byte1 from 108 to 159
C1 var word ' 5540 Pressure sensitivity
C2 var word ' 5540 Pressure Offset
C3 var word ' 5540 Temp Coef of pressure sensitivity
C4 var word ' 5540 Temp Coef of Pressure Offset
C5 var word ' 5540 Reference temperature
C6 var word ' 5540 temp coef of Temp reaToSensorg

D1 VAR word ' raw pressure word from Intersema sensor
D2 var word ' raw temperature word

' W1 to W4 are NOT stored in EEROM - they are not needed again.
W1 var word 'coefficient from 5540
W2 var word 'coefficient from 5540
W3 var word 'coefficient from 5540
W4 var word 'coefficient from 5540

Chan VAR BYTE : Chan = 1
Duty VAR WORD : Duty = 511
Freq VAR WORD : Freq = 32768

dT var long ' intermediate calc value
UT1 var word ' Calibration temperature
II var byte ' used ONLY within Intersema routines
IJ var byte
IK var byte
IL var byte
CalFlag var bit ' calibration data read and processed
RefPress VAR BIT
temp2 var word
d var BYTE[18]
Offset var long
Senstvty var long
Pressure var long
SetPressure VAR long
Celsius var long 'a LONG allows negative temperatures
DegreesF VAR LONG ' Our temperature in Fahrenheit
Depth VAR word ' Our depth in inches

CalFlag = 0 ' We have not read the calibration data yet
Refpress = 0 ' We have not set the reference pressure yet
Reset = 0 ' Start with needing to recalibrate

goto endofsubroutines ' jump subroutines at startup
'*************************** Subroutines *****************************

ResetSensor:
' need 32768 @ 50% for Intersema on CCP1.
LOW PortB.3
@ HPWM10 _Chan, _Duty, _Freq
Pause 10
shiftout ToSensor, Sclk, 0, [85, 85, 0\5] ' Sense of ToSensor is IN to 5541
pauseus 100
return

FetchWord:
shiftin FromSensor, Sclk, 2, [ij, ii, ik\1]
' IJ is hi byte, II is lo byte, IK is a dummy and discarded
PAUSE 100 'Wait for data from sensor
return

CalSensor:
if calflag = 1 then calsensordone
' This fetches and unpacks the factory calibration coefficients
' from W1 ~ W4. W values need not be stored.
' These bitmaps are unpacked into the 6 working coefficients
' C1 to C6 which must be stored in EEROM for later use.

' W1
gosub ResetSensor
shiftout ToSensor, Sclk, 0, [87, 1\5] ' Send W1 pattern to all sensors
gosub fetchword ' get reply
W1.byte0 = ii
W1.byte1 = ij

' W2
gosub ResetSensor
shiftout ToSensor, sclk, 0, [215, 0\5] ' Send W2 pattern
gosub fetchword
W2.byte0 = ii
W2.byte1 = ij

' W3
gosub ResetSensor
shiftout ToSensor, sclk, 0, [55, 1\5] ' Send W3 pattern
gosub fetchword
W3.byte0 = ii
W3.byte1 = ij

' W4
gosub ResetSensor
shiftout ToSensor, sclk, 0, [183, 0\5] ' Send W4 pattern
gosub fetchword
W4.byte0 = ii
W4.byte1 = ij

' Unpack W1 ~ W4 into C1 ~ C6 coefficients

' C1
C1 = W1 >> 3
write 108, c1.byte0
write 109, c1.byte1
' C2
C2 = ((W1 & %0000000000000111) << 10) + (W2 >> 6)
write 110, c2.byte0
write 111, c2.byte1
' C3
C3 = W3 >> 6
write 112, c3.byte0
write 113, c3.byte1
' C4
C4 = W4 >> 7
write 114, c4.byte0
write 115, c4.byte1
' C5
C5 = ((W2 & %0000000000111111) << 6) + (W3 & %0000000000111111)
write 116, c5.byte0
write 117, c5.byte1
' C6
C6 = W4 & %0000000001111111
write 118, c6.byte0
write 119, c6.byte1

calflag = 1 'We are now Claibrated
CalSensorDone:

ReadSensor:
' D1 is Pressure - D2 is Temperature
' Uses LONG datatype to handle negative temperatures
' This routine reads the sensor and returns Pressure word
' and Celsius long
' A, II, IJ, W and X are all destroyed

gosub ResetSensor

' D1
shiftout ToSensor, Sclk, 0, [47, 0\5] ' Select D1 pattern
While FromSensor = 1
wend 'Convert5541Delay:
gosub fetchword

' This returns ii = lobyte, ij = hibyte from the sensor
D1.byte0 = ii
D1.byte1 = ij

' D2
shiftout ToSensor, Sclk, 0, [79, 0\5] ' Select D2 pattern
While FromSensor = 1
wend
gosub fetchword

D2.byte0 = ii
D2.byte1 = ij

CalcTempPress: ' 32 bit signed arithmetic version. Handles negatives.

read 108, c1.byte0
read 109, c1.byte1
read 110, c2.byte0
read 111, c2.byte1
read 112, c3.byte0
read 113, c3.byte1
read 114, c4.byte0
read 115, c4.byte1
read 116, c5.byte0
read 117, c5.byte1
read 118, c6.byte0

' formula for DS5541 are NOT the same as for DS5540
ut1 = 8*c5 + 10000
dt = d2 - ut1
Celsius = 200 + dt*(c6 + 100)/2048
dEGREESf = ((18 * Celsius) + 3200)/10
offset = c2 + ((c4 - 250)*dt)/4096 +10000
senstvty = c1/2 + ((c3 + 200)*dt)/8192 + 3000
pressure = (senstvty * (d1 - offset))/4096 + 1000

' if Reset = 0 then RefPress = 0 ' We have surfaced recalibrate
if Refpress = 1 then CalcDepth
SetPressure = Pressure
RefPress = 1

CalcDepth:
Depth = ((pressure*10) - (SetPressure*10))/2

' NOT applying second order temperature correction in this version.
' Note 2nd order correction formula ambiguity in datasheet.

if celsius < 0 then 'high bit of 32 bit number is set
temp2 = 1000 - celsius
else
temp2 = 1000 + celsius
d[17] = temp2.byte0
d[18] = temp2.byte1
endif

EndOfSubRoutines:
' *********************** Initialise *****************************

if calFlag = 0 then
goto CalSensor
else
gosub ReadSensor
endif

Normnet
- 17th August 2013, 22:35
Ramius

I don't see a "Return" at the end of the subroutines.


Norm

comwarrior
- 17th August 2013, 23:59
norm, i agree, no return for the gosubs...

Ramius
- 18th August 2013, 01:11
Hi Guys!
Thanks and to be honest I am not sure where a "return" or "returns" would or should go! :confused: Everything seems to run normally except for the program stopping and waiting for the "FromSensor" to go low so it can continue. The "FromSensor" does toggle from high to low to high to low consistently and the program runs completely for 3 minutes and then for some unknown reason at the D2 section "Wend" stops as "FromSensor" does not appear to go low!? Ed

Normnet
- 18th August 2013, 03:08
Ramius

A GoSub is a routine which can be reused and should include a "Return" at its end which takes the code point back to the GoSub where it was originally called.


Norm

Jerson
- 18th August 2013, 04:42
if calFlag = 0 then
goto CalSensor
else
gosub ReadSensor
endif

You could try changing this gosub to a goto ReadSensor.

rsocor01
- 18th August 2013, 07:02
Hi,

Without looking at the PIC's datasheet I think that the problem might be with the following line

ADCON1 = 255 ' Make everything digital (no analog)

Don't you need to define PORTA.0 as analog?

Robert

HenrikOlsson
- 18th August 2013, 11:38
Hi,
Just a thought and question:
Are you sure that it is the PIC that stops? I've never used the MCS Debugger but is it possible that IT is either stopping itself (so that you THINK the program has stopped) or perhaps IT is actually stopping the PIC? Have you tried running it without the ICD?

And, what others have said: ALL subroutines to which you jump with a GOSUB must, at some point, end with a RETURN or bad things will happen. I haven't looked that close at your code but perhaps you've just been "lucky" that it works and that after three minutes there's been exactly the amount of GOSUBS without RETURN to make the stack tip over. I don't know, just thinking out loud.

/Henrik.

Ramius
- 18th August 2013, 12:42
Hi All!
Again my sincere thanks to everyone! For Robert, the whole "system" is digital and there is no analog. The sensor only puts out digital information. Yes, it appears that the "Gosub" where Jerson located it was the error! Changed it to a "goto" and now everything runs longer than 3 minutes! FYI, the PIC never stopped running as Mclk would continue to change just the "FromSensor" would stop changing. So Henrik you were right (as usual!) I had just been lucky. By the way, I put everything on a small board, added a bunch of capacitor (probably overkill) so at the input and output of the TXB0101 there are a 470mf, 100mf tantalum, 01 ceramic, and on the bottom of the board, a 1mf tantalum on the input and output of the MCP1700-3002E voltage regulator! Best to all, Ed