Files
SyncHome/trunk/workspace/gcb/Servo_Demo_16F1829.gcb
2023-03-09 12:51:54 +00:00

97 lines
2.6 KiB
Plaintext

' ########### start ###########
' pic:PIC16F1829
' ########### end ###########
'''An Example Program for Great Cow Basic.
'''Control 4 Servos Using 4 Pots and TIMER1
'''using a Midrange PIC (16F1829)
'''@author Willam Roth
'''@date 01.04.2015
'''********************************************************************************
#chip 16F1829, 16
; *** This code only works at 16 Mhz ***
'Servo Pins
#Define Servo1 PORTB.4
#Define Servo2 PORTB.5
#Define Servo3 PORTB.6
#Define Servo4 PORTB.7
'ADC Pins
#Define POT1 AN3
#Define POT2 AN7
#Define POT3 AN8
#Define POT4 AN9
'Variables
DIM SERVOPOS As WORD
DIM SERVO1POS AS WORD
DIM SERVO2POS AS WORD
DIM SERVO3POS AS WORD
DIM SERVO4POS AS WORD
'Pin Directions
DIR Servo1 OUT
DIR Servo2 OUT
DIR Servo3 OUT
DIR Servo4 OUT
DIR PORTA.4 IN 'AN3
DIR PORTC.3 IN 'AN7
DIR PORTC.6 IN 'AN8
DIR PORTC.7 IN 'AN9
'Setup and Start Timer1
InitTimer1 Osc, PS1_2
TMR1H = 100: TMR1L = 0 'Timer Preload value for 20ms Period
On Interrupt timer1overflow call ISR1
Starttimer 1
DO
READ_POTS
'Instead of reading POTs we could
'read serial, I2C or HSPI data
'for Servo control data
LOOP
SUB ISR1
' This routine takes ~10 ms to complete with all servos
' at maximim pulse width.
TMR1H = 100: TMR1L = 0 ' timer preload for 20 ms period
set Servo1 on: wait SERVO1POS us: set Servo1 off
set Servo2 on: wait SERVO2POS us: set Servo2 off
set Servo3 on: wait SERVO3POS us: set Servo3 off
set Servo4 on: wait SERVO4POS us: set Servo4 off
END SUB
SUB READ_POTS
SERVOPOS = READAD10(POT1)
SERVOPOS = (SERVOPOS * 25 / 16 + 797) '// Map 800 to 2400
if servopos < (SERVO1POS - 1) or Servopos > (SERVO1POS + 1) then
SERVO1POS = SERVOPOS '// Update & save value
end if
SERVOPOS = READAD10(POT2)
SERVOPOS = (SERVOPOS * 25 / 16 + 797) '// Map 800 to 2400
if servopos < (SERVO2POS - 1) or Servopos > (SERVO2POS + 1) then
SERVO2POS = SERVOPOS '// Update & save value
end if
SERVOPOS = READAD10(POT3)
SERVOPOS = (SERVOPOS * 25 / 16 + 797) '// Map 800 to 2400
if servopos < (SERVO3POS - 1) or Servopos > (SERVO3POS + 1) then
SERVO3POS = SERVOPOS '// Update & save value
end if
SERVOPOS = READAD10(POT4)
SERVOPOS = (SERVOPOS * 25 / 16 + 797) '// Map 800 to 2400
if servopos < (SERVO4POS - 1) or Servopos > (SERVO4POS + 1) then
SERVO4POS = SERVOPOS '// Update & save value
end if
END SUB