hello,
I need very high frequency PWM then time chaining two PWM outputs to drive high power MOSFET & power electronics inverters.
#define NOP __asm__("nop\n\t")
int outputPin1 = 22; // PORTA - bit #1
int outputPin2 = 7; // PORTH - bit #4
int i, charge_on, charge_off, extract_on, extract_off;
int duty_charge, duty_blank, duty_extract;
long freq;
void setup()
{
Serial.begin(9600);
pinMode(outputPin1, OUTPUT);
pinMode(outputPin2, OUTPUT);
freq=23000; // Hz
duty_charge=55; // %
duty_blank=5; // %
duty_extract=35; // %
frame();
}
void loop()
{
// Turns ON coil charging opto-coupler #1
PORTH |= B10000;
for(i=0;i<charge_on;i++) NOP;
// Turns OFF coil charging opto-coupler #1
PORTH &= B11101111;
for(i=0;i<charge_off;i++) NOP;
// Turns ON coil FE extracting opto-coupler #2
PORTA |= B1;
for(i=0;i<extract_on;i++) NOP;
// Turns OFF coil FE extracting opto-coupler #2
PORTA &= B11111110;
for(i=0;i<extract_off;i++) NOP;
}
void frame()
{
float fperiod, fduty_charge, fduty_blank, fduty_extract;
int period;
fperiod=2272727.0 / ((float) freq);
period=(int) (fperiod+0.5);
fduty_charge=fperiod*((float) duty_charge)/100.0;
fduty_blank=fperiod*((float) duty_blank)/100.0;
fduty_extract=fperiod*((float) duty_extract)/100.0;
charge_on=(int) (fduty_charge+0.5);
charge_off=(int) (fduty_blank+0.5);
extract_on=(int) (fduty_extract+0.5);
extract_off=period-charge_on-charge_off-extract_on;
}
Once frequency gets high and duty cycle low, it is not precise anymore due to overhead time of for(i=0;i<N;i++).
For big values of N, I get average 699ns run time for each NOP if LONG I or average 444ns run time each NOP loop if INT i so very far from theoretical 62,5ns.
When N is small, this number gets much higher due to internal overhead control management of for(i=0;i>...)
Can you teach me or show me a code much faster (guess in ASM but i'm new to AVR) where my need is to SW by a variable control how much NOP have to be called.
Thank you, Albert