const unsigned char sin_table[576]={
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,
//0, 6, 11, 17, 23, 28, 34, 39, 44, 50, 55, 60, 64, 69, 74, 78, 82, 86, 90, 93, 96, 99, 102, 105, 107, 109, 111, 113, 114, 115, 115, 116, 116, 116, 115, 115, 114, 113, 111, 109, 107, 105, 102, 99, 96, 93, 90, 86, 82, 78, 74, 69, 64, 60, 55, 50, 44, 39, 34, 28, 23, 17, 11, 6, //92.8%
0, 5, 11, 16, 22, 27, 32, 37, 42, 47, 52, 57, 62, 66, 70, 75, 78, 82, 86, 89, 92, 95, 98, 100, 103, 105, 106, 108, 109, 110, 110, 111, 111, 111, 110, 110, 109, 108, 106, 105, 103, 100, 98, 95, 92, 89, 86, 82, 78, 75, 70, 66, 62, 57, 52, 47, 42, 37, 32, 27, 22, 16, 11, 5, //88.8%
0, 5, 10, 16, 21, 26, 31, 36, 41, 45, 50, 54, 59, 63, 67, 71, 75, 79, 82, 85, 88, 91, 93, 96, 98, 100, 101, 103, 104, 105, 105, 106, 106, 106, 105, 105, 104, 103, 101, 100, 98, 96, 93, 91, 88, 85, 82, 79, 75, 71, 67, 63, 59, 54, 50, 45, 41, 36, 31, 26, 21, 16, 10, 5, //84.8%
0, 5, 10, 15, 20, 25, 30, 34, 39, 44, 48, 52, 57, 61, 65, 68, 72, 76, 79, 82, 85, 87, 90, 92, 94, 96, 98, 99, 100, 101, 102, 102, 102, 102, 102, 101, 100, 99, 98, 96, 94, 92, 90, 87, 85, 82, 79, 76, 72, 68, 65, 61, 57, 52, 48, 44, 39, 34, 30, 25, 20, 15, 10, 5, //81.6%
0, 5, 10, 14, 19, 24, 28, 33, 38, 42, 46, 50, 54, 58, 62, 66, 69, 73, 76, 79, 81, 84, 86, 89, 91, 92, 94, 95, 96, 97, 98, 98, 98, 98, 98, 97, 96, 95, 94, 92, 91, 89, 86, 84, 81, 79, 76, 73, 69, 66, 62, 58, 54, 50, 46, 42, 38, 33, 28, 24, 19, 14, 10, 5, //78.4%
0, 5, 9, 14, 18, 23, 27, 32, 36, 40, 44, 48, 52, 56, 60, 63, 66, 70, 73, 76, 78, 81, 83, 85, 87, 89, 90, 91, 92, 93, 94, 94, 94, 94, 94, 93, 92, 91, 90, 89, 87, 85, 83, 81, 78, 76, 73, 70, 66, 63, 60, 56, 52, 48, 44, 40, 36, 32, 27, 23, 18, 14, 9, 5, //75.2%
0, 4, 9, 13, 18, 22, 26, 31, 35, 39, 43, 47, 51, 54, 58, 61, 64, 67, 70, 73, 76, 78, 80, 82, 84, 86, 87, 88, 89, 90, 91, 91, 91, 91, 91, 90, 89, 88, 87, 86, 84, 82, 80, 78, 76, 73, 70, 67, 64, 61, 58, 54, 51, 47, 43, 39, 35, 31, 26, 22, 18, 13, 9, 4, //72.8%
0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 41, 45, 49, 52, 56, 59, 62, 65, 68, 71, 73, 75, 78, 80, 81, 83, 84, 85, 86, 87, 88, 88, 88, 88, 88, 87, 86, 85, 84, 83, 81, 80, 78, 75, 73, 71, 68, 65, 62, 59, 56, 52, 49, 45, 41, 38, 34, 30, 26, 21, 17, 13, 9, 4, //70.4%
0, 4, 8, 12, 17, 21, 25, 29, 33, 36, 40, 44, 47, 51, 54, 57, 60, 63, 66, 68, 71, 73, 75, 77, 79, 80, 81, 82, 83, 84, 85, 85, 85, 85, 85, 84, 83, 82, 81, 80, 79, 77, 75, 73, 71, 68, 66, 63, 60, 57, 54, 51, 47, 44, 40, 36, 33, 29, 25, 21, 17, 12, 8, 4 //68%
};
unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ, adder,FB_Step;
unsigned int TBL_temp;
unsigned char DUTY_CYCLE;
unsigned int temp;
unsigned int battery_voltage;
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 + adder;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
}
}
void Run_PWM(){
PORTC = 0;
PORTD = 0;
SET_FREQ = 1024;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
adder = 0;
FB_Step = 0;
ECCPAS =0b11000000; //autoshutodwm on INT pin high
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; // Timer2 interrupt flag
TRISC = 0;
TMR2IE_bit = 1; //interrupt when TMR2 matches PR2
GIE_bit = 1; // Global interrupt enabled
PEIE_bit = 1; // peripheral interrupt enabled
PWM1CON = PWM1CON + 0b10000000 ; //Part of ECCPAS procedure
}
void main() {
ANSEL = 0x0F;
ANSELH = 0x00;
ADCON1 = 0x80;
TRISA=0b00000001;
CM1CON0 = 0; //Disable Comparator
CM2CON0 = 0;
TRISC = 0;
TRISD = 0;
TRISB = 1;
PORTC = 0;
PORTD = 0;
PORTB = 0;
ADC_Init();
battery_voltage = ADC_read(0);
FB_Step = floor((battery_voltage - 750)/35);
adder = FB_Step << 6; //2^6 =64
while( PORTB.F0 == 0);
Run_PWM();
while(1);
}