djc
Advanced Member level 1
Hi,
I have written a code for PIC16F676 and mikroc where i have to drive an H bridge. Where one IGBT in upper half of the bridge will be ON for 10ms and corresponding diagonally opposite IGBT will get switched with PWM pulses to generate half cycle of sine wave. similarly for next 10ms of remaining sine wave another IGBT in upper half of another wing will be ON and corresponding diagonally opposite IGBT will be switched with PWM pulses. PWM is generated by Timer0 with 2Khz frequency and 10ms interrupt is generated by Timer1. This is all done. Now there is an external sync pulse of 50Hz on external interrupt pin i.e. RA2. I have configured it as rising edge interrupt. Whenever timer1 interrupt will occur timer0 interrupt starts and timer0 interrupt will stop when complete half cycle of sine wave is done. Code is as follows.
Now my problem is i want to switch off both the timers when complete sine wave is generated for both the wings and turn it ON back when external sync signal reappears, which is a zero crossing detection signal. If no external sync is there system should remain OFF. Here 'Sync' variable doesn't seems to be working. means when Sync is equal to 3 it means that a complete sine wave for both cycle has been created and shut all the interrupts off with INTE_bit = 1; enabled for external sync signal to be only active. Now when external sync is there turn all the interrupts on again.But in my code either sine wave gets distorted or external sync doesn't affect at all.Where is the actual mistake. Can anybody plz guide me.
I have written a code for PIC16F676 and mikroc where i have to drive an H bridge. Where one IGBT in upper half of the bridge will be ON for 10ms and corresponding diagonally opposite IGBT will get switched with PWM pulses to generate half cycle of sine wave. similarly for next 10ms of remaining sine wave another IGBT in upper half of another wing will be ON and corresponding diagonally opposite IGBT will be switched with PWM pulses. PWM is generated by Timer0 with 2Khz frequency and 10ms interrupt is generated by Timer1. This is all done. Now there is an external sync pulse of 50Hz on external interrupt pin i.e. RA2. I have configured it as rising edge interrupt. Whenever timer1 interrupt will occur timer0 interrupt starts and timer0 interrupt will stop when complete half cycle of sine wave is done. Code is as follows.
Code:
#define channel_1 PORTC.B0
#define channel_2 PORTC.B1
#define channel_3 PORTC.B2
#define channel_4 PORTC.B3
unsigned char OnPulse = 0;
unsigned char OffPulse = 0;
char TOG = 0,Flag=0,Tenms=0,Sync=0,valid=1;
unsigned int i = 0,j=0,i_1=0,n=0;
bit k,m,k_1,m_1;
const unsigned char sine2[22]={2,22,42,75,100,115,140,150,160,170,182,185,183,180,165,140,115,100,75,42,22,2};
const unsigned char sine3[22]={2,22,42,75,100,115,140,150,160,170,182,185,183,180,165,140,115,100,75,42,22,2};
Port_Control(){
CMCON = 0x07;
OSCCAL = 0b11111100;
ANSEL = 0;
TRISA = 0b00000100; //PortA as output
TRISC = 0x00; //PortC as output
PORTC = 0x00; //Initial value on PortC
INTE_bit = 1;
INTEDG_bit = 1;
}
Timer0_Control(){
T0IF_bit = 0;
TMR0 = 0;
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:2
PS1_bit = 0;
PS2_bit = 0;
}
Timer1_Control(){
INTCON = 0b11000000;
PIR1.TMR1IF = 0;
TMR1GE_bit = 0;
T1CON.T1CKPS0 = 0; //1:1 prescale value
T1CON.T1CKPS1 = 0; //1:1 prescale value
T1CON.TMR1CS = 0; //Internal clock Fosc/4
T1CON.TMR1ON = 0;
PIE1.TMR1IE = 1;
TMR1L = 0x00;
TMR1H = 0xc8;
T1CON.TMR1ON = 1;
}
void interrupt(){
if(INTF_bit){ //50hz square wave
INTF_bit = 0;
if(Sync==3){
INTCON = 0b11000000;
INTE_bit= 1;
Sync=0;
}
}
if(PIR1.TMR1IF){ //10ms on/off
PIR1.TMR1IF = 0;
Sync=Sync+1;
if(Sync==3){INTCON = 0b10000000;INTE_bit= 1;}
Tenms = Tenms+1;
if(Tenms ==1){
INTCON = 0b11100000; //timer0 on
T0CS_bit = 0;
channel_1 = 1;
channel_3 = 0;
channel_2 = 0;
channel_4 = 0;
k=1;
m=0;
}
else{
INTCON = 0b11100000;//timer0 on
T0CS_bit = 0;
//INTE_bit= 1;
Sync=0;
channel_1 = 0;
channel_3 = 1;
channel_2 = 0;
channel_4 = 0;
Tenms = 0;
k=0;
m=1;
}
TMR1L = 0x00;
TMR1H = 0xc8; //c8,e4
}
if(INTCON.T0IF){ //PWM Signal,2Khz
TMR0 = 0;
Flag=1;
//channel_1 = ~channel_1;
if(k==1){
if(TOG==1){
Tmr0 = OffPulse;
channel_2= 1;
Tog=0;
}
else{
Tmr0 = OnPulse;
channel_2 = 0;
Tog=1;
}
}
if(m==1){
if(TOG==1){
Tmr0 = OffPulse;
channel_4= 1;
Tog=0;
}
else{
Tmr0 = OnPulse;
channel_4 = 0;
Tog=1;
}
}
T0IF_bit = 0;
}
}
void main() {
Port_Control();
Timer0_Control();
Timer1_Control();
while(1){
if(Flag==1){
if(k==1){
OnPulse = sine2[j++];
OffPulse = 255-OnPulse;
Flag=0;
if(j>21){T0CS_bit = 1;/*INTCON = 0b11000000;*/ j =0;} //Timer0 off
}
if(m==1){
OnPulse = sine3[i++];
OffPulse = 255-OnPulse;
Flag=0;
if(i>18){T0CS_bit = 1;/*INTCON = 0b11000000;*/i =0;} //Timer0 off
}
}
}
}
Now my problem is i want to switch off both the timers when complete sine wave is generated for both the wings and turn it ON back when external sync signal reappears, which is a zero crossing detection signal. If no external sync is there system should remain OFF. Here 'Sync' variable doesn't seems to be working. means when Sync is equal to 3 it means that a complete sine wave for both cycle has been created and shut all the interrupts off with INTE_bit = 1; enabled for external sync signal to be only active. Now when external sync is there turn all the interrupts on again.But in my code either sine wave gets distorted or external sync doesn't affect at all.Where is the actual mistake. Can anybody plz guide me.