SX10501: Programming
The following tables show how the Xwire data block needs to appear in memory. Because the SX10501 can be used with or without an optional TN9301 peripheral board there are 2 sets of Xwire data blocks.
If the SX10501 is used only for the Ultrasonic distance measurement the Transmit data block will consist of 1 byte and the Receive data block will consist of 5 bytes.
However if the SX10501 is also using a TN9301 then the Transmit data block would consist of 2 bytes and the Receive data block would consist of 13 bytes.
Xwire transmit data block:
SX10501 only
| Memory | Description | Format |
|---|---|---|
| 0 | Master sequence number | Byte |
Memory location 0 in the Xwire transmit data block corresponds to the ‘Master sequence number’. The Master sequence number consists of a single byte in the range of 0 – 255. Whenever the controller software changes this number, the ultrasonic sensor will take a single reading. This allows several ultrasonic ranging modules to be used on the same controller and be sequenced to avoid interaction between them.
SX10501 with TN9301 attached
| Memory | Description | Format |
|---|---|---|
| 0 | Master sequence number | Byte |
| 1 | Master sequence number | Byte |
To enable the use of a TN9301 peripheral board the ‘Master sequence number’ length is equated as 2 bytes size in the Xwire transmit data block. This second byte does not contain any data and the Master sequence number performs exactly as described above in the “SX10501 only” transmit data block. The 2nd byte invokes the TN9301 temperature measurement option.
Xwire receive data block:
SX10501 only
| Memory | Description | Format |
|---|---|---|
| 0 – 3 | Floating point number returned in the range of 30cm – 600cm | |
| 4 | Returned sequence number | Byte returned in the range of 0 – 255 |
*Note that this data block does not need to start at memory location 0 and 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.
Memory locations 0-3 in the Xwire receive data block is the ‘Distance’ read back from the SX10501. The distance is returned in 4 bytes as a floating point number in the range of 30cm to 600cm. If the Ultrasonic sensor is operated outside of this range erroneous readings will result.
Memory location 4 in the Xwire receive data block is the ‘Returned sequence number’ from the SX10501. When the ‘Returned sequence number’ becomes the same value that is inside the ‘Master sequence number’ the distance measurement is complete and the data is ready to be taken.
SX10501 with TN9301 attached
| Memory | Description | Format |
|---|---|---|
| 0 – 3 | Floating point number returned in the range of 30cm – 600cm | |
| 4 | Returned sequence number | Byte returned in the range of 0 – 255 |
| 5 – 8 | Target temperature from the TN9301 | Floating point number returned in Degrees Celsius. |
| 9 – 12 | Ambient temperature from the TN9301 | Floating point number returned in Degrees Celsius. |
*Note that this data block does not need to start at memory location 0 and 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.
Memory locations 5-8 in the Xwire receive data block is the remote Target temperature from the TN9301. This is the temperature of an object in front of the sensor. The temperature is returned as a floating point number in degrees Celsius. The temperature is updated at a rate of 1.4Hz.
Memory locations 9-12 in the Xwire receive data block is the Ambient temperature from the TN9301. The temperature is returned as a floating point number in degrees Celsius. The temperature is updated at a rate of 1.4Hz.
If the TN9301 is removed from the SX10501 or damaged an error value of NaN is returned from the SX10501 for the affected function.
Example Program 1
Interfaces with an SX10501 without TN9301 attachment.
Displays Distance in Centimetres and Inches updating 5 times a second.
********************* Xwire ****************************
DistMastSeqNum: mEQU 218,1 ;Define Xwire Tx Ram for slave
DistMastSeqNumLen: EQU 1 ;length of Tx Ram *alternatively set to 2 if using TN9301 attachment
DistReadyFlag: mEQU 224,1 ;where the return sequence flag from the slave is stored
DistData: mEQU 220,4 ;Define Xwire Rx Ram for slave * Distance
DistDataLen: EQU 5 ;length of Rx Ram *alternatively set to 13 if using TN9301 attachment
DistAddress: EQU 0 ;Xwire Address of slave
;Initialisation:
on 20 ;turn on MS120 backlight
XwireMaster XwireTab ;start xwire going
gosub DSP_DO_Init ;initialise the Display handler
gosub XW_DO_Init ;Initialise the XWire handler
runtasksforever
;*************** Display OBJECT **********************
;***************** Description ************************
;Short name: DSP_
;
;Purpose Display handler task.
;
;Behavior *Initially displays a splash screen for 4 seconds.
;
; *Displays Distance in Centimetres and Inches updating-
; 5 times a second.
******************* Entry points ************************
********************* Methods ***************************
* Entry point to initialise this task *
DSP_DO_Init:
launchtask DSP_Task ;initialise the task
return
*********************************************************
****************** Display Task ***********************
*********************************************************
DSP_Task:
DSP_Set0:
;0123456789012345
Oblcd_Cls
Oblcd_SetCur 0,0
Oblcd_Text " SX10501 Demo "
DSP_0:
pause 400
DSP_Set1:
DSP_1:
yieldtask
gosub XW_GetDistance
Oblcd_SetCur 0,0 ;set curser to home position on line 1
;0123456789012345 ;space available on lcd
Oblcd_Text "Distance "
Oblcd_SetCur 1,0 ;set curser to home position on line 1
;0123456789012345 ;space available on lcd
Oblcd_fDispW 3,0 ;display the distance
Oblcd_Text "cm "
gosub DSP_ConvDist ;Convert Centimetres to Inches
Oblcd_fDispW 3,0 ;display the distance
Oblcd_Text "in "
goto DSP_1
DSP_ConvDist: ;will convert Centimetres to inches and return it in W
floadQ 2.54
fswap
fdiv ;distance in cm / 2.54 = inches. A -> W
Return
;***************** XWire Handler ***********************
;***************** Description ************************
;Short name: XW_
;
;Purpose Maintains and handles communications over XWire.
;
;Behavior
;
; * The following task increments 'DistMastSeqNum' to request
; the current distance measurement from the slave.
; * The slave responds when the measurement is ready to take
; by setting 'DistReadyFlag' to the same value in 'DistMastSeqNum'.
********************* Defines ***************************
;Define Memory
sXWireChange: defsem ;XWire index change semaphore
XWireIdx: defbyte ;index number of data to obtain
XW_Distance: defbyte 4 ;where distance data is stored in floating point
;Define Outputs
oXW_XWireError: oequ 9 ;LED turns on when there is an Xwire communication error.
******************* Entry points ************************
********************* Methods ***************************
* Entry point to initialise this task *
XW_DO_Init: launchtask XW_Task ;put the XWire handler task in the task queue
return
****************** Property Gets ************************
* Entry point to check Distance Ready *
XW_ChkDistRdy:
Recalls sXWireChange ;returns with data in X, if 0 then data ready for use
return
* Entry point to retrieve Distance data *
XW_GetDistance:
fRecallW XW_Distance ;returns with data in W
return
*********************************************************
****************** XWire Task *************************
*********************************************************
XW_Task:
* XWire Distance data recovery *
XW_0:
pause 20 ;only update distance 5 times a second
incm DistMastSeqNum ;incrementing this memory makes slave measure distance and send it back
XW_1: yieldtask ;wait looking at that status byte
xwiregeterrcount ;get the xwire error count
goiff XW_2
on oXW_XWireError ;turn on error LED if there are XWire errors
pause 1
goto XW_3
XW_2: off oXW_XWireError
XW_3: recall DistMastSeqNum ;recall counter number currently in transmit mem
recall DistReadyFlag ;recall status byte, if same as counter then data ready to take
compare
goifnz XW_1
fRecallw DistData ;recall the distance data returned via xwire
fstore XW_Distance ;move to storage memory location
clrs sXWireChange ;clear message changed semaphore so main knows to come get data
goto XW_0
*********************************************************
NVEM0
XwireTab:
NV0Byte DistAddress,DistMastSeqNum,DistMastSeqNumLen,DistData,DistDataLen
NV0Byte 255 ;end of table
Example Program 2
Interfaces with an SX10501 with TN9301 attachment.
Displays Ambient (A) and Target (T) temperature values in Celsius and Fahrenheit for 4 seconds.
Displays Distance in Centimetres and Inches for 4 seconds.
Repeats cycle.
********************* Xwire ****************************
DistMastSeqNum: mEQU 218,1 ;Define Xwire Tx Ram for slave
DistMastSeqNumLen: EQU 2 ;length of Tx Ram *alternatively set to 1 if ONLY using Ultrasonic distance measurement
DistReadyFlag: mEQU 224,1 ;where the return sequence flag from the slave is stored
DistData: mEQU 220,4 ;Define Xwire Rx Ram for slave * Distance
DistDataLen: EQU 13 ;length of Rx Ram *alternatively set to 5 if ONLY using Ultrasonic distance measurement
DistAddress: EQU 0 ;Xwire Address of slave
TempData: mEQU 225,8 ;Define Xwire Rx Ram for slave * Temperature (2 float, target/ambient temp)
;Initialisation:
on 20 ;turn on MS120 backlight
XwireMaster XwireTab ;start xwire going
gosub DSP_DO_Init ;initialise the Display handler
gosub XW_DO_Init ;Initialise the XWire handler
runtasksforever
;*************** Display OBJECT **********************
;***************** Description ************************
;Short name: DSP_
;
;Purpose Display handler task.
;
;Behavior *Initially displays a splash screen for 4 seconds.
;
; *Displays Ambient (A) and Target (T) temperature values in
; Celsius and Fahrenheit for 4 seconds.
; *Displays Distance in Centimetres and Inches for 4 seconds.
; *Repeats cycle.
******************* Entry points ************************
********************* Methods ***************************
* Entry point to initialise this task *
DSP_DO_Init:
launchtask DSP_Task ;initialise the task
return
*********************************************************
****************** Display Task ***********************
*********************************************************
DSP_Task:
DSP_Set0:
;0123456789012345
Oblcd_Cls
Oblcd_SetCur 0,0
Oblcd_Text " SX10501 Demo "
Oblcd_SetCur 1,0
Oblcd_Text " with TN9301 "
DSP_0:
pause 400
DSP_Set1:
marktime
DSP_1:
yieldtask
gosub XW_GetAmbientT ;get the ambient temperature
Oblcd_SetCur 0,0
Oblcd_Text "A"
Oblcd_fDispW 3,0 ;display the temperature
Oblcd_SpclChar 223 ;display degree sign
Oblcd_Text "C "
gosub XW_GetTargetT ;get the target temperature
Oblcd_Text "T"
Oblcd_fDispW 3,0 ;display the temperature
Oblcd_SpclChar 223 ;display degree sign
Oblcd_Text "C "
gosub XW_GetAmbientT ;get the ambient temperature
gosub DSP_ConvTemp ;convert Celsius to Fahrenheit
Oblcd_SetCur 1,0
Oblcd_Text "A"
Oblcd_fDispW 3,0 ;display the temperature
Oblcd_SpclChar 223 ;display degree sign
Oblcd_Text "F "
gosub XW_GetTargetT ;get the target temperature
gosub DSP_ConvTemp ;convert Celsius to Fahrenheit
Oblcd_Text "T"
Oblcd_fDispW 3,0 ;display the temperature
Oblcd_SpclChar 223 ;display degree sign
Oblcd_Text "F "
loopiftiming 400,DSP_1 ;Display temperatures for 4 seconds.
DSP_Set2:
marktime
DSP_2:
yieldtask
gosub XW_GetDistance
Oblcd_SetCur 0,0 ;set curser to home position on line 1
;0123456789012345 ;space available on lcd
Oblcd_Text "Distance "
Oblcd_SetCur 1,0 ;set curser to home position on line 1
;0123456789012345 ;space available on lcd
Oblcd_fDispW 3,0 ;display the distance
Oblcd_Text "cm "
gosub DSP_ConvDist ;Convert Centimetres to Inches
Oblcd_fDispW 3,0 ;display the distance
Oblcd_Text "in "
loopiftiming 400,DSP_2
goto DSP_Set1
DSP_ConvDist: ;will convert Centimetres to inches and return it in W
floadQ 2.54
fswap
fdiv ;distance in cm / 2.54 = inches. A -> W
Return
DSP_ConvTemp: ;will convert Celsius to fahrenheit and return it in W.
floadQ 1.8
fmul
floadQ 32
fadd ;temp in C x 1.8 + 32 = fahrenheit. A -> W
Return
;***************** XWire Handler ***********************
;***************** Description ************************
;Short name: XW_
;
;Purpose Maintains and handles communications over XWire.
;
;Behavior
;
; * The following task increments 'DistMastSeqNum' to request
; the current distance measurement from the slave.
; * The slave responds when the measurement is ready to take
; by setting 'DistReadyFlag' to the same value in 'DistMastSeqNum'.
;
; * The Ambient and Target temperature values are constantly updated.
********************* Defines ***************************
;Define Memory
sXWireChange: defsem ;XWire index change semaphore
XWireIdx: defbyte ;index number of data to obtain
XW_Distance: defbyte 4 ;where distance data is stored in floating point
;Define Outputs
oXW_XWireError: oequ 9 ;LED turns on when there is an Xwire communication error.
******************* Entry points ************************
********************* Methods ***************************
* Entry point to initialise this task *
XW_DO_Init: launchtask XW_Task ;put the XWire handler task in the task queue
return
****************** Property Gets ************************
* Entry point to check Distance Ready *
XW_ChkDistRdy:
Recalls sXWireChange ;returns with data in X, if 0 then data ready for use
return
* Entry point to retrieve Distance data *
XW_GetDistance:
fRecallW XW_Distance ;returns with data in W
return
* Entry point to retrieve Target Temperature data *
XW_GetTargetT:
fRecallW TempData ;returns with data in W
return
* Entry point to retrieve Ambient Temperature data *
XW_GetAmbientT:
loadi 1
ifRecallW TempData ;returns with data in W
return
*********************************************************
****************** XWire Task *************************
*********************************************************
XW_Task:
* XWire Distance data recovery *
XW_0:
pause 20 ;only update distance 5 times a second
incm DistMastSeqNum ;incrementing this memory makes slave measure distance and send it back
XW_1: yieldtask ;wait looking at that status byte
xwiregeterrcount ;get the xwire error count
goiff XW_2
on oXW_XWireError ;turn on error LED if there are XWire errors
pause 1
goto XW_3
XW_2: off oXW_XWireError
XW_3: recall DistMastSeqNum ;recall counter number currently in transmit mem
recall DistReadyFlag ;recall status byte, if same as counter then data ready to take
compare
goifnz XW_1
fRecallw DistData ;recall the distance data returned via xwire
fstore XW_Distance ;move to storage memory location
clrs sXWireChange ;clear message changed semaphore so main knows to come get data
goto XW_0
*********************************************************
NVEM0
XwireTab:
NV0Byte DistAddress,DistMastSeqNum,DistMastSeqNumLen,DistData,DistDataLen
NV0Byte 255 ;end of table