(SX48/52 only) Syntax

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' Demonstates the SX48/52 TIMER1 in PWM mode to control a servo.
'
' Notes:
' -- 4 MHz / 32 = 125,000 Hz -> 8 uS per cycle (0.000008 s)
' -- 20 ms / 8 uS = 2500  (0.020 / 0.000008 = 2500)
' --  1 ms / 8 uS =  125  (0.001 / 0.000008 =  125)
' --  2 ms / 8 uS =  250  (0.002 / 0.000008 =  250)


' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX48, OSCXT1
FREQ            4_000_000
ID              "TIMER"

' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

Servo           VAR     RB.6                    ' T1 output pin 

' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

ServoCycle      CON     2_500                   ' 20 ms @ 4 MHz, 1:32
ServoMin        CON     125
ServoMax        CON     250

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

position        VAR     Byte                    ' servo position
tmpW1           VAR     Word                    ' for subroutines

' =========================================================================
  PROGRAM Start
' =========================================================================

' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------

DELAY           SUB     1, 2                    ' delay in milliseconds

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
  OUTPUT Servo                                  ' enable output pin
  TIMER1 PRESCALE, 5                            ' set prescaler to 1:32
  DO
    FOR position = ServoMin TO ServoMax STEP 5
      TIMER1 PWM, position, ServoCycle
      DELAY 200                                 ' slow sweep
    NEXT
    FOR position = ServoMax TO ServoMin STEP -5
      TIMER1 PWM, position, ServoCycle
      DELAY 20                                  ' faster sweep
    NEXT
  LOOP

' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------

' Use: DELAY ms
' -- 'ms' is delay in milliseconds, 1 - 65535

DELAY:
  IF __PARAMCNT = 1 THEN
    tmpW1 = __PARAM1                            ' save byte value
  ELSE
    tmpW1 = __WPARAM12                          ' save word value
  ENDIF
  PAUSE tmpW1
  RETURN