#define _XTAL_FREQ 8000000
#define SERVO_PIN RA2
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
void main(){
OSCCONbits.IRCF = 0b1110;//8MHz
TRISAbits.TRISA2 = 0;//RA2 is Output
ANSELAbits.ANSA2 = 0;//RA2 is Digital I/O
APFCONbits.CCP1SEL = 1;//CCP1/P1A function is on RA5
unsigned char i;
__delay_ms(1000);
while(1){
unsigned int i;
for(i=0; i<50; i++){//+60 Deg
SERVO_PIN = 1;
__delay_us(800);
SERVO_PIN = 0;
__delay_us(19200);
}
__delay_ms(100);
for(i=0; i<50; i++){//0 Deg
SERVO_PIN = 1;
__delay_us(1400);
SERVO_PIN = 0;
__delay_us(18600);
}
__delay_ms(100);
for(i=0; i<50; i++) {//-60 Deg
SERVO_PIN = 1;
__delay_us(2200);
SERVO_PIN = 0;
__delay_us(17800);
}
__delay_ms(100);
for(i=0; i<50; i++){//0 Deg
SERVO_PIN = 1;
__delay_us(1400);
SERVO_PIN = 0;
__delay_us(18600);
}
__delay_ms(100);
}
}