I am designing a PWM rectifier with IGBT switches ,here i am using Zero crossing detector for synchronization purpose for firing pulses . I have written a code to generate 5 pulses in one half cycle (50 Hz input Frequency) During testing for one switch i have have found some drift in gating pulse .I mean sometime there is 5 pulses inside the first half cycle (10 millisecond) and sometime there is 3 pulses , there is movement is gating pulses which is not desirable .
int flag1=1;
int flag2=1;
int ZCD = 2;// Input from zcd
void setup() {
pinMode(ZCD, INPUT);
pinMode(12, OUTPUT);
pinMode(9, OUTPUT);
//Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
int sinewave = digitalRead(ZCD); // ZCD output
if (sinewave==1 && flag1==1)
{
digitalWrite(12, LOW);
for(int i =0;i<5;i++)
{
digitalWrite(9,HIGH);
delay(1);
digitalWrite(9,LOW);
delay(1);
}
flag1=0;
flag2=1;
}
else if (sinewave==0 && flag2==1)
{
digitalWrite(9, LOW);
for(int i =0;i<5;i++)
{
digitalWrite(12,HIGH);
delay(1);
digitalWrite(12,LOW);
delay(1);
}
flag2=0;
flag1=1;
}
else
{
digitalWrite(9, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}
}
After detecting the AC Zero Cross,
the code may process for longer than 10 milliseconds
before checking for the next AC Zero Cross.
Is that acceptable?
First of all thanks for your replies .I have written a new code where i am using interrupts but still in this code i am facing the same problem.Even this time i am trying with four pulses to avoid the timing problem with ZCD (which is 10 ms for one half)
durgesh11oct:
First of all thanks for your replies .I have written a new code where i am using interrupts but still in this code i am facing the same problem.Even this time i am trying with four pulses to avoid the timing problem with ZCD (which is 10 ms for one half)
Yes, you used Interrupts but then in the LOOP() function you still POLLED the Interrupt Flag.
Maybe, you need to trigger "Hardware PWM" from inside the Interrupt instead of setting a FLAG?