unsigned char sin_table[64]={0, 6, 12, 18, 24, 30, 36, 42, 48, 53, 59, 64, 69, 74, 79, 84, 88, 93, 97, 100, 104, 107, 110, 113, 115, 118, 120, 121, 123, 124, 124, 125, 125, 125, 124, 124, 123, 121, 120, 118, 115, 113, 110, 107, 104, 100, 97, 93, 88, 84, 79, 74, 69, 64, 59, 53, 48, 42, 36, 30, 24, 18, 12, 6}; //100% duty cycle
unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
unsigned int TBL_temp;
unsigned char DUTY_CYCLE;
unsigned int temp;
void interrupt(){
if (TMR2IF_bit == 1){
TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
if (TBL_POINTER_NEW < TBL_POINTER_OLD){
CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
}
TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10;
DUTY_CYCLE = TBL_POINTER_SHIFT;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
}
}
void Run_PWM(){
PORTC.F2 = 0;
PORTD.F5 = 0;
PORTD.F6 = 0;
PORTD.F7 = 0;
SET_FREQ = 1024;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
PR2 = 0b01111100; //124
CCP1CON = 0b01001100; // duty lowest bits + PWM mode
TMR2IF_bit = 0;
T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
while (TMR2IF_bit == 0);
TMR2IF_bit = 0;
TRISC = 0;
TMR2IE_bit = 1;
GIE_bit = 1;
PEIE_bit = 1;
}
void Stop_PWM(){
PORTC = 0;
PORTD = 0;
}
void main() {
ANSEL = 0; //Disable ADC
ANSELH = 0x00;
CM1CON0 = 0; //Disable Comparator
CM2CON0 = 0;
TRISD.F0 = 1;
TRISC.F2 = 0;
TRISD.F5 = 0;
TRISD.F6 = 0;
TRISD.F7 = 0;
TRISB.F0 = 0;
PORTC.F2 = 0;
PORTD.F5 = 0;
PORTD.F6 = 0;
PORTD.F7 = 0;
PORTB.F0 = 0;
PORTD.F0 = 0;
SET_FREQ = 1024;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
PR2 = 0b01111100; //124
CCP1CON = 0b01001100; // duty lowest bits + PWM mode
do{
if (PORTD.F0==0){
PORTB.F0 = 0;
Stop_PWM();
}
else {
Run_PWM();
PORTB.F0 = 1;
}
}while(1);
}