PDA

View Full Version : I really, really, really need help with this code!!



Ramius
- 22nd April 2012, 18:39
Hello Everyone!
I have been struggling, researching, and struggling trying to get this code to work. Nothing seems to work correctly. My sincere hope and wish is that someone's expert eyes will spot the errors. Okay, so I went to set all the inputs to digital using ADCON1 = 7. As soon as I did this the program stops right after this statement. I tried looking with an oscilloscope at signals such as the SCLK and nothing seems to be there! Hour after hour, day after day I have been banging my head against the wall (and not because it feels good when it stops hurting, Lol) and I must now admit I am completely stuck. Any and all input will be more than appreciated. Thanks, Ed



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"

#CONFIG
__CONFIG _CONFIG1H, _HS_OSC_1H & _FSCM_OFF_1H
__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32K_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 **************************
Dout VAR PortA.0 'data out from the MS5541
Din 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

ADCON1 = 7

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

'************** Intersema Variables *********************************
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 reading
' C1 to C6 are in EEROM as VAL.byte0, VAL.byte1 from 108 to 159

D1 var word ' raw pressure word from Intersema sensor
D2 var word ' raw temperature word
D1Flag var bit

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

dT var long ' intermediate calc value
dT1 var long ' intermediate calc value
dT2 var long
UT1 var word ' Calibration temperature
II var byte ' used ONLY within Intersema routines
IJ var byte
IK var byte
IL var byte
ILoop var word
CalFlag var bit ' EEROM 99. Used to reduce wear on EEROM
temp2 var word
ClkChk var word ' = 8192 if IntRC clock is exactly on frequency
TimeFlag var bit
TempRead var word
CntRem var word
CntPerC var word
d var BYTE[18]
IW var long
IX var long
IY var long
IZ var long
T2 var long ' Second order Temperature correction
P2 var long ' Second order Pressure correction
Offset var long
Senstvty var long
Pressure var long '
Celsius var long 'a LONG allows negative temperatures

' Set all variables to a known number
C1 =0
C2 =0
C3 =0
C4 =0
C5 =0
C6 =0
D1 =0
D2 =0
D1Flag =0
W1 =0
W2 =0
W3 =0
W4 =0
dT1 =0
dT2 =0
UT1 =0
II =0
IJ =0
IK =0
IL =0
ILoop =0
CalFlag =0
temp2 =0
ClkChk =0
TimeFlag =0
TempRead =0
CntRem =0
CntPerC =0
d =0
IW =0
IX =0
IY =0
IZ =0
T2 =0
P2 =0
Offset =0
Senstvty =0
Pressure =0
Celsius =0

goto endofsubroutines ' jump subroutines at startup

'*************************** Subroutines *****************************
ResetIntersema:
' need 32768 @ 50% for Intersema on CCP2.
hpwm 1, 127, 32767
pause 50
shiftout din, sclk, 0, [85, 85, 0\5] ' Sense of Din is IN to 5541
pauseus 100
'return
FetchWord:
shiftin dout, sclk, 2, [ij, ii, ik\1]
' IJ is hi byte, II is lo byte, IK is a dummy and discarded
'return

CalSensor:
read 99, calflag : if calflag = 1 then calsensordone
' debug 13, 10, "Fetch Intersema data", 13, 10
' 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 resetintersema
shiftout din, sclk, 0, [87, 1\5] ' Send W1 pattern to all sensors
gosub fetchword ' get reply
W1.byte0 = ii
W1.byte1 = ij
' W2
gosub resetintersema
shiftout din, sclk, 0, [215, 0\5] ' Send W2 pattern
gosub fetchword
W2.byte0 = ii
W2.byte1 = ij
' W3
gosub resetintersema
shiftout din, sclk, 0, [55, 1\5] ' Send W3 pattern
gosub fetchword
W3.byte0 = ii
W3.byte1 = ij
' W4
gosub resetintersema
shiftout din, 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

' high pgc : pause 1
' debug "Intersema Factory data ", $0D, $0A, "W1 = ,"_
' , #w1, ", W2 = ,", #w2, ", W3 = ,", #w3, ", W4 = ,", #w4, $0D, $0A
' debug "Derived coefficients", $0D, $0A, "C1 = ,", #C1, ", C2 = ,", #C2,_
' ", C3 = ,", #C3, ", C4 = ,", #c4, ", C5 = ,", #C5, ", C6 = ,", #C6.byte0,_
' $0D, $0A
' read 99, calflag : if calflag = 0 then write 99, 1
CalSensorDone:
return
ReadIntersema: '
' 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 resetintersema
' D1
shiftout din, sclk, 0, [47, 0\5] ' Select D1 pattern
While dout = 1
wend 'Convert5541Delay:
gosub fetchword
' This returns ii = lobyte, ij = hibyte from the sensor
D1.byte0 = ii
D1.byte1 = ij
' D2
shiftout din, sclk, 0, [79, 0\5] ' Select D2 pattern
While dout = 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

' formulae for DS5541 are NOT the same as for DS5540
ut1 = 8*c5 + 10000
dt = d2 - ut1
Celsius = 200 + dt*(c6 + 100)/2048
offset = c2 + ((c4 - 250)*dt)/4096 +10000
senstvty = c1/2 + ((c3 + 200)*dt)/8192 + 3000
pressure = (senstvty * (d1 - offset))/4096 + 1000
' 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
if calflag = 0 then
' high pgc : pause 1
' debug "MS5541 Celsius = ", sdec Celsius/10, ".", dec1 celsius//10, _
' ", Pressure = ", dec pressure, " mBar, recorded as ",_
' dec temp2, 13, 10
endif
' return

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

FetchDepth:
gosub readintersema
' debug "T2 = ", dec temp2, 13, 10
' debug dec (temp2 - 1000)/10, ".", dec1 (temp2 - 1000)//10, _
' ", ", #pressure, " mBar", 13, 10
' this returns Pressure & Celsius, both words.
gosub resetintersema

rsocor01
- 22nd April 2012, 18:53
Hi,

What PIC are you using?

Also, every subroutine should end with a RETURN statement, so you can return to the point where your code was at when calling the GOSUB. At the end of your program you need a GOTO FetchDepth.

Ramius
- 23rd April 2012, 00:13
Thank you! Thank you! Thank you! It is a 18F1320. For some reason (probably a brain cramp) I commented out the returns!

Ramius
- 23rd April 2012, 00:22
At the risk of sound more dumb than I already have, There are statements such as:
' D1
shiftout din, sclk, 0, [47, 0\5] ' Select D1 pattern
While dout = 1
wend 'Convert5541Delay:

and the program seems to stop at the wend??

mister_e
- 23rd April 2012, 00:25
Short that pin to GND and se what happen ;)

Ramius
- 23rd April 2012, 12:21
Okay and if I short it to gnd then the program continues to run. Comment out the WHILE and WEND statements things will run to a point where the program goes all the way back to the #config! Please note that I did not write this program, just trying to get it to function and there is a ton I simply do not understand. Thanks, Ed

Acetronics2
- 23rd April 2012, 13:16
Hi, Ramius,

Did you had a look to Intersema AN510 ???

Alain

Ramius
- 23rd April 2012, 20:00
Thank you Alain!
Yes the app note was very useful as it turned out the spec sheet for the MS5541 was translated from Chinese to English and some of the more necessary information was missing! The "AN510" filled in some of the blanks. It also turned out that this device is sensitive to timing and Intersema decided to use strange methodes of SPI communications such as sending 21 bits and not 24! I am very happy that there are people such as you as well as many others that are willing to assist and teach. Thank you all. Best, Ed