const unsigned char sine_table [73] = {0, 6, 12, 18, 24, 29, 31, 35, 41, 46, 52, 57, 63, 68, 73, 78, 83,
87, 92, 96, 100, 104, 108, 112, 115, 118, 121, 124, 126, 128, 130, 132, 133, 136, 136, 137,
137, 136, 135, 134, 133, 131, 129, 127, 125, 123, 120, 117, 114, 110, 107, 103, 99, 94, 90, 86, 81,
76, 71, 66, 61, 55, 50, 44, 38, 33, 27, 21, 15, 9, 3, 0};
int duty = 0;
bit P1M1_invert;
float feedback = 1.00; //max dutycycle.----->Duty cycle = 100%
int k = 0; unsigned int ac_volt = 0;
float vAD = 0.0;
void InitTimer1(){
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xFD;
TMR1L = 0xE8;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt () iv 0x0004 ics ICS_AUTO {
if(TMR1IF_bit) //timer1 overflow ---> 134usec.
{
TMR1IF_bit = 0;
TMR1H = 0xFD;
TMR1L = 0xE8;
duty++;
if(duty >= 73)
{
duty = 0;
P1M1_invert = ~P1M1_invert;
//CCPR1L = 0;
asm nop;
asm nop;
}
if(P1M1_invert)
{
RC0_bit = 1;
RC1_bit = 1;
RC3_bit = 0;
RC4_bit = 0;
PWM1_Set_Duty(sine_table[duty]);
}
else
{
RC0_bit = 0;
RC1_bit = 0;
RC3_bit = 1;
RC4_bit = 1;
PWM1_Set_Duty(sine_table[duty]);
}
}
}
void main()
{
//configure analog channle 4.
TRISC = 0;
PORTC = 0;
PWM1_Init(20000);//initialize at 20KHz
PWM1_Start();
InitTimer1();
//duty = 0;
while(1)
{
//feedback control code flow.
}
}
above mikroC code generate SPWM using only CCP1 with "7408 AND gate ic " making it to work as two CCP.
TMR1 is use to generate 50Hz setting XTAL = 16MHz with interrupt overflow 134usec. here the simulation show 49 or 50Hz fine with
sine_table of 73 values . here a problem of how to update SPWM duty cycle using a variable resistor at analog pin?