SX10506: Programming
The following table shows how the Xwire data block needs to appear in memory. The Receive data block consists of 4 bytes.
Xwire receive data block:
| Memory | Description | Format |
|---|---|---|
| 3 | Pressure data | 24 bit floating point number normalised to 1.0000 |
*Note that this data block does not need to start at memory location 0. The data block can be moved anywhere in memory as long as it stays together, is in the order above and is defined in the NVEM Xwire table in the program code.
Example program demonstrating how to interface with a SX10506
* Program written for a MMI202a with a 2x16 LCD.
* The MMI202a communicates to the SX10506 over the XWire peripheral interface.
* MMI202a controller is the XWire master and the SX10506 is the XWire slave.
** Setup **
* The SX10502 is interfaced to the MMI202a over X-Wire protocol by setting the communications jumper to the X position.
* No board ID jumpers are installed as the X-Wire slave address for the SX10506 is address 0.
* The X-Wire cable is connected from the CN12 connector on the MMI to the X1 connector on the SX10506.
** Program operation **
* X-Wire is set up to return pressure data to the MMI202a.
* Pressure from the SX10506 is continually updated on the LCD.
* If at any time there are Xwire communications errors the front panel LED on output 15 will light.
* The user presses and holds iBTN_Calibrate for 10 seconds to enter calibration mode.
* At power on if the pressure calibration has not been previously set the program will enter calibration mode.
* Once calibrated, the calibration values are stored to shadow memory so they survive power off.
* XWire Equates *
XW_Address: EQU 0 ;Xwire Address of slave.
* XWire Receive data block *
* Data block of 4 bytes *
XW_RxRAMstart: EQU 100 ;The starting memory location of the Xwire receive data block.
XW_RxRAMlength: EQU 4 ;The size of the returned data in bytes.
XW_fPressure: mEQU 100,4 ;Location where the SX10506 Pressure reading is returned.
* XWire Transmit data block *
* Data block of 0 bytes *
XW_TxRAMstart: EQU 120 ;The starting memory location of the xwire transmit data block.
XW_TxRAMlength: EQU 0 ;There are no bytes to send to the SX10506.
* Input Equates *
iBTN_Calibrate: iEQU 12 ;User presses and holds for 10 seconds to enter calibration mode.
iBTN_Poffset: iEQU 11 ;When button is pressed the current pressure reading is saved as the offset. (Calibration only)
iBTN_Pspan: iEQU 10 ;When button is pressed the span is calculated and saved. (Calibration only)
* Output Equates *
oXW_XWireError: oEQU 15 ;LED turns on when there is an Xwire communication error.
* Pressure sensor calibration constants SHADOW memory *
CalFlag: EQU 'AA ;Value of the valid calibration flag.
ShadowLength: EQU 9 ;Length of shadow memory.
ShadowStart: EQU 200 ;Start of shadow memory buffer.
fPoffset: mEQU 200,4 ;Floating point pressure calibration offset value.
fPspan: mEQU 204,4 ;Floating point pressure scale factor.
bCalibratedFlag: mEQU 208 ;Byte that is set to 'AA when the pressure sensor is calibrated.
* defFOAT Directives *
fCalc: defFLOAT ;Where the current calculated water height is stored.
*******************
** Program start **
*******************
XwireMaster XwireTab ;Start xwire going.
LaunchTask MSQ_Start ;Start the Main Sequencer task running.
LaunchTask Xwire_Task ;Start the XWire error checking task running.
RunTasksForever
MSQ_Start:
ShadowRead ShadowStart,ShadowLength ;Recall calibration data (if present) from shadow memory.
GoIfMNE bCalibratedFlag,CalFlag,Calibrate ;If the calibration flag is 'AA then calibration is already set. Otherwise calibrate the pressure sensor.
MSQ_Display:
YieldTask
GoSub MSQ_Sub0_CalcHeight ;Calculate the current water level.
GoSub MSQ_Sub1_DispHeight ;Display current water level.
GoIfInOFF iBTN_Calibrate,MSQ_Display ;Test if the user wishes to enter Calibration mode.
MarkTime ;Start the timer running.
* Enter this state if the user has pressed and holds *
* the iBTN_Calibrate button *
MSQ_Display_T:
YieldTask
GoSub MSQ_Sub0_CalcHeight ;Calculate the current water level.
GoSub MSQ_Sub1_DispHeight ;Display current water level.
GoIfInOFF iBTN_Calibrate,MSQ_Display ;Is the user still holding the iBTN_Calibrate button ?
LoopIfTiming 1000,MSQ_Display_T
GoTo Calibrate ;If the user holds the iBTN_Calibrate button for 10 seconds enter Calibration Mode.
* Subroutines *
MSQ_Sub0_CalcHeight:
fRecallQ XW_fPressure ;Calculate water height. Water height = (PressureSensorReading - Poffset) / (Pspan)
fRecallW fPoffset
fSub
fRecallQ fPspan
fSwap
fDiv
fStore fCalc ;Store the calculated value temporarily.
Return
MSQ_Sub1_DispHeight:
fRecallW fCalc ;Recall value to display.
;0123456789012345
OBLCD_SetCur 0,0
OBLCD_Text " Water height "
OBLCD_SetCur 1,0 ;Display result on the LCD.
OBLCD_fDispW 7,2
OBLCD_Text "m "
Return
*******************************
* Pressure sensor calibration *
*******************************
* We are now in pressure calibration mode. We now wait for iBTN_Poffset button to be pressed before
* we store the low point value. Water depth is assumed to be at minimum.
Calibrate: ;0123456789012345
OBLCD_SetCur 0,0
OBLCD_Text "Water level = 0m"
OBLCD_SetCur 1,0 ;Display message.
OBLCD_Text "Press Poffset "
WaitOn iBTN_Poffset ;Has iBTN_Poffset button been pressed ?
fRecallW XW_fPressure ;Get current pressure sensor reading.
fStore fPoffset ;Store to Poffset.
* We are now waiting for the iBTN_Pcal button to be pressed before we calculate the span.
* Span is calculated as Pressure at 1m of water - fPoffset.
Calibrate_0: ;0123456789012345
OBLCD_SetCur 0,0
OBLCD_Text "Water level = 1m"
OBLCD_SetCur 1,0 ;Display message.
OBLCD_Text "Press Pspan "
WaitOn iBTN_Pspan ;Has iBTN_Pcal button been pressed ?
fRecallQ XW_fPressure ;Get current pressure sensor reading.
fRecallW fPoffset ;Calculate span.
fSub
fStore fPspan ;Store to Pspan.
SetMem bCalibratedFlag,CalFlag ;Set a flag to show at startup calibration has already been done.
ShadowWrite ShadowStart,ShadowLength ;Save calibration parameters and flag.
GoTo MSQ_Start
****************************
** Xwire Error Check Task **
****************************
Xwire_Task:
XW0:
YieldTask
XwireGetErrCount ;Get the xwire error count.
GoIfF XW1
On oXW_XWireError ;Turn on error LED if there are XWire errors.
Pause 1
GoTo XW0
XW1:
Off oXW_XWireError
GoTo XW0
NVEM0
XWireTab: ;Slave address, transmit data block, Tx bytes, receive data block, Rx bytes.
NV0Byte XW_Address,XW_TxRAMstart,XW_TxRAMlength,XW_RxRAMstart,XW_RxRAMlength
NV0Byte 255 ;End of table sentinel.