here you go... (I've added comments for easy understanding)
Code:
/*
** Code for generation of PWM for motor esc
** PIC16F877A, HiTech PICC 9.81
** April, 2011
*/
#include <htc.h>
__CONFIG(CP_OFF & DEBUG_OFF & WRT_OFF &CPD_OFF & LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_LP) ;
//DEFINE CRYSTAL FREQ
#define _XTAL_FREQ 32768
//INITIALISE VARIABLES
//int i;
void main(void)
{ CMCON = 7;
PORTB = 0; //RESET PORTB PINS
TRISB = 0b11111100; //RB1:RB0 AS OUTPUTS
RB0 = 0;
RB1 = 1; //working LED On
PORTC = 0; //set RC2 for PWM
TRISC2 = 0;
PR2 = 0b10010100; //54.98Hz PWM @32.768kHz
T2CON = 0b00000100; //T2 on with 1:1 pre and post scaler
CCPR1L = 0b00001000; //1msec duty cycle
CCP1CON = 0b00001100; //set PWM and write 2-lsb of duty cycle
while(1)
{
if(0 == RB2)
{ CCPR1L = 0b00001100; //drive bldc
RB1 = 0;
}
else
{ if ((0 == RB2)&&(0 == RB3))
{ CCPR1L = 0b00001111; //drive hard!
RB1 = 0;
}
else
{ CCPR1L = 0b00001000; //stop bldc
RB1 = 1;
}
}
}
}