SPice10214: Programming
Once installed on a SPLat controller, the SPice10211 appears as several regular analog and digital I/O points. The following table shows the mappings for all the I/O on the board. Numbers in parenthesis are the numeric analog channel numbers used with fAnIn.
| Cnctr pin | SLXXX | MMiXXX | MS12 | |
|---|---|---|---|---|
| P1 (Onboard) | 7 | AnInF (5) | AnInH (7) | AnInF (5) |
| P2 | 5 | AnInD (3) | AnInF (5) | AnInD (3) |
| P3 | 6 | AnInE (4) | AnInG (6) | AnInE (4) |
| P4 | 4 | AnInC (2) | AnInE (4) | AnInC (2) |
initializing the SPice connector
Because the pins of the SPice connector, to which the SPice10214 is connected, can be configured for different functions (analog/digital, input/output), you need to have some code in your program that configures them to the correct settings for the SPice12014.
The following section of code should be run by your program just once, at the beginning, before your program attempts to do anything else with the SPice10214.
ClrU
setu 0,3 ;set spice connector pin 4 as analogue input
setu 1,3 ;set spice connector pin 5 as analogue input
setu 2,3 ;set spice connector pin 6 as analogue input
setu 3,3 ;set spice connector pin 7 as analogue input
SpiceConfigU
Once that is done programming is a simple matter of executing an analog input instruction against the input of interest.
The readings (values) obtained will depend on the potentiometer setting, and nominally range from 0 to 255. In practice you should allow for a few missing codes at each end of the scale due to potentiometer end resistances and inevitable errors in the analog inputs of the SPLat.
Do not rely on obtaining a reading of 0 or 255 for something mission critical to happen in your controller.
The best strategy is to work on an active range of 5 minimum and 245 maximum (but still tolerate 0 to 255 without doing something silly).
Example program
The following program illustrates how you can use the SPice10214 to create a variable timer.
(Click here for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help (.chm) files)
Example program:
;vartime.spt - SPice10214 demo program for MS12
;This program illustrates the use of a potentiometer to create
;a variable timer. It does the following:
; 1 Reads potentiometer on P3
; 2 Limits the range to 5-245
; 3 Shifts it to 0-240 and displays.
; 4 Scales to 1 to 5 seconds and displays
; 5 Generates an output pulse of 1 to 5 seconds
; when a button is pressed.
ON 20 ;LCD backlight
;Initialize the SPice connector for SPice10214
ClrU
setu 0,3 ;set spice connector pin 4 as analogue input
setu 1,3 ;set spice connector pin 5 as analogue input
setu 2,3 ;set spice connector pin 6 as analogue input
setu 3,3 ;set spice connector pin 7 as analogue input
SpiceConfigU
;----- Main loop ------
Loop:
AnInE ;Read the pot
GoSub Fixup ;Limit range to 5-245, shift to 0-240
OBLCD_SetCur 1,0
Push
OBLCD_UDecDispXFW ;Display the semi-processed reading
float ;Convert to floating point
fLoadQ 0.016667 ;Scale 0-240 to 0-4
fMul
fLoadQ 1 ;1s offset, giving the range 1 to 5
fAdd
OBLCD_fDispW 6,1 ;Display
InputK 16
GoSubIfT PulseOut ;Time duration is already in W
GoTo Loop ;Repeat forever
;-------- Generate an output pulse equal to the value in W, in seconds.
PulseOut:
fLoadQ 10
fMul ; now in units of 100mS
fSetTimer 0 ;Start timing
On 8 ;Turn on the output
PO1:
Test 0 ;Timed out
GoIfT PO1 ;g/ not yet
Off 8 ;Turnoff the output
Return ;All done
;=================================================
;Bound X within range 5 to 245, offset to 0 to 240
;I've made this a subroutine because it can then be re-used for all
;4 channels of the SPice10214.
Fixup:
Push ;Make a copy
GoIfXGE 5,L1 ;Test if < 5
LoadX 5 ;Anything < 5 becomes 5
L1:
Push ;Make a copy
GoIfXLE 245,L2 ;Test if > 245
LoadX 245 ;Anything > 245 becomes 245
L2:
DecX ;Knock off 5 counts ...
DecX ; ... reducing range...
DecX ; ... 5 - 245 ...
DecX ; ... to ...
DecX ; ... 0 - 240
Return