Up one Timers example. by Lee Davison Up to top

Introduction.

In this example the SuprDupr is used to read a joystick on an analog input and generate a servo PWM signal using two of the timers.

The joystick input gave a value of 90 to 165, the servo needs a pulse width of 0.5mS to 2.5mS which, with the prescale value used, means TIMERX values will range from 50 to 250

The values for S and M, the scaling and minimum offset values used in the code, were calculated as follows..

S = timer range / input range
  = (250-50) / (165-90)
  = 2.67
.. and ..
M = minimum timer - minimum scaled value
  = 50 - (90*S)
  = -190
The code.

10 REM generates timing pulses for a servo
20 REM control is via analog input 0
30 REM output is via timer X
40 REM timer 1 is used to generate the 20mS frame
50 REM
100 TIMERX STOP : REM stop output
110 PRES12 49 : TIMER1 199 :  REM set TIMER 1 for 20mS timebase
120 M = -190 : REM set minimum pulse width offset
130 PS = 4 : REM set timer X prescale value
140 S = 2.67 : REM set scale value
150 PRESX PS : REM set timer X prescaler
160 X = 0 : REM set input for timer X
180 TIMERX PULSE : REM timer X to output pulse
190 GOSUB 1010 : REM calculate initial X value
200 ON TIMERX 1000 : REM stop timer and calculate next
210 ON TIMER1 2000 : REM set and start TIMERX
220 DO : LOOP : REM just loop forever
997 REM
998 REM TIMER X routine
999 REM
1000 TIMERX STOP
1010 LX = A2D(X) : REM read port
1020 LX = LX*S : REM scale value
1030 LX = LX+M : REM add minimum width offset
1040 RETURN
1997 REM
1998 REM TIMER 1 routine
1999 REM
2000 TIMERX LX : REM set timer X
2010 TIMERX START : REM start timer
2020 RETURN

Last page update: 9th July, 2003. e-mail me e-mail