On another thread someone asked about C18 servo code and I wrote a simple version. It occurred to me that it wouldn't be too difficult to make this work on the Junebug and so here it is. This code reads the potentiometer on AN1 (VR1) and sets the position of a servo on RB3 to match it.
Switches 1,2,3 & 8 on.
Have fun.
Mike.
Project file attached.
Switches 1,2,3 & 8 on.
Code:
#include <p18f1320.h>
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO2
#define ServoPin LATBbits.LATB3
#define ServoTris TRISBbits.TRISB3
void main(void){
int ServoPos;
OSCCON=0x70; //Osc=8MHz
ADCON0=0b00000101; //A2D on and select AN1
ADCON1=0x7d; //A1 = analogue
ADCON2=0b10110101; //Right justify - Fosc/16
ServoTris=0; //make servo pin output
ServoPin=0; //Servo output off
CCP1CON=0b00001011; //Special event trigger
T1CON=0b10010001; //Timer 1 on with Pre=2
ServoPos=1500; //set servo to mid position
CCPR1=ServoPos; //set CCP initial value
while(1){
while(!PIR1bits.CCP1IF); //wait for CCP interrupt bit
ServoPin=0; //end pulse
CCPR1=20000-ServoPos; //Off time = 20mS - Servo Time
PIR1bits.CCP1IF=0; //clear int flag
ADCON0bits.GO=1; //start conversion
while(ADCON0bits.GO); //Wait for it to complete
ServoPos=ADRES+1000; //Pos will be 1mS to 2.023mS
while(!PIR1bits.CCP1IF); //wait for int flag
ServoPin=1; //start pulse
CCPR1=ServoPos; //Servo time in uS
PIR1bits.CCP1IF=0; //clear int flag
}
}
Have fun.
Mike.
Project file attached.
Last edited: