ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

ME Labs, Inc. | 1-719-520-5323 | Example Program - SERVOX.pbp

PICBASIC PRO program to move RC servo 1 using keypad buttons. Button 1 moves servo left, 2 centers servo, 3 moves servo right.
' Name        : SERVOX.pbp
' Compiler    : PICBASIC PRO Compiler 2.60
' Assembler   : PM or MPASM
' Target PIC  : 40-pin 16F887
' Hardware    : LAB-X1 Experimenter Board
' Oscillator  : 4MHz external crystal
' Keywords    : KEYPAD, SERVO CONTROL
' Description : PICBASIC PRO program to move RC servo 1 using keypad
' buttons. Button 1 moves servo left, 2 centers servo, 3 moves servo
' right.
'

' Define LOADER_USED to allow use of the boot loader.
' This will not affect normal program operation.
Define LOADER_USED 1

Define LCD_DREG  PORTD   ' Define LCD connections
Define LCD_DBIT  4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG  PORTE
Define LCD_EBIT  1

pos    Var Word     ' Servo position
servo1 Var PORTC.1  ' Alias servo pin

   ANSEL = %00000000 ' Make PORTA and PORTE digital
   ANSELH= %00000000 ' Make PORTA and PORTE digital
   Low PORTE.2      ' LCD R/W low = write
   Pause 100        ' Wait for LCD to startup

   OPTION_REG = $7f ' Enable PORTB pullups
   Low servo1       ' Servo output low
   Gosub center     ' Center servo

' Main program loop
mainloop:
   PORTB = 0        ' PORTB lines low to read buttons
   TRISB = $fe      ' Enable first button row

   ' Check for button press to move servo
   If PORTB.4 = 0 Then  ' SW1 on LAB-X1 keypad
      Gosub left
   Endif
   If PORTB.5 = 0 Then  ' SW2 on LAB-X1 keypad
      Gosub center
   Endif
   If PORTB.6 = 0 Then  ' SW3 on LAB-X1 keypad
      Gosub right
   Endif

   Lcdout $fe, 1, "Position = ", #pos
   servo1 = 1      ' Start servo pulse
   Pauseus 1000 + pos
   servo1 = 0      ' End servo pulse
   Pause 16        ' Servo update rate about 60Hz
   Goto mainloop   ' Do it all forever

' Move servo left
left:
   If pos < 1000 Then
      pos = pos + 1
   Endif
   Return

' Move servo right
right:
   If pos != 0 Then
      pos = pos - 1
   Endif
   Return

' Center servo
center:
   pos = 500
   Return

   End

           

Download the program file.