c++ - using delayMicroseconds inside interrupt -
i wondering if there better way use interrupt , delay. using 5 interrupts , not sure, if should way "wait" fireeing pin after 10us function need ketch half waives of 50hz ac power change speed of motor. each half waive 10ms long, want change speed in seting pin high time 100us x dimming factor*
void isr_0() //interrupt d2 {pulsecounter[0]++;} void isr_1() //interrupt d3 {pulsecounter[1]++;} void isr_2() //interrupt d21 {pulsecounter[2]++;} void isr_3() //interrupt d20 {pulsecounter[3]++;} void isr_4() //interrupt d19 {// firing angle calculation : 1 full 50hz wave =1/50=20ms // every zerocrossing thus: (50hz)-> 10ms (1/2 cycle) // 10ms=10000us // (10000us - 10us) / 100 = 100 (approx) int dimtime = (100*dimming); // 50hz =>100 when 0-100 delaymicroseconds(dimtime); // wait till firing triac digitalwrite(ac_load, high); // fire triac delaymicroseconds(10); // triac on propogation delay (for 60hz use 8.33) digitalwrite(ac_load, low); // no longer trigger triac (the next 0 crossing swith off) triac} void isr_5() //interrupt d18 {pulsecounter[5]++;}
the 1st answer add timer:
void isr_4() //interrupt d19 {// firing angle calculation : 1 full 50hz wave =1/50=20ms // every zerocrossing thus: (50hz)-> 10ms (1/2 cycle) // 10ms=10000us // (10000us - 10us) / 100 = 100 (approx) int dimtime = (10000-100*dimming); // 50hz =>100 when 0-100 if (micros() > dimtime) // wait till firing triac digitalwrite(ac_load, high); // fire triac if (micros() > dimtime+10) // triac on propogation delay digitalwrite(ac_load, low); // no longer trigger triac (the next 0 crossing swith off) triac} dimtime = 0; }
like this?
or timer in void loop
???
a delay should never used inside interrupt service routine.
in general, interrupts need processed fast possible allow other interrupts fire.
to change pin after 10µs, start timer.
chances using delay function inside interrupt service routine wouldn't work, because function using (timer) interrupt too.
update:
the function need ketch half waives of 50hz ac power change speed of motor. each half waive 10ms long, want change speed in setting pin high time 100us dimming factor
you need pulse-width modulation (pwm) this.
on arduino can done analogwrite()
. see pwm on arduino.
analogwrite()
takes care of outputting waves, need change motor speed call analogwrite()
once.
update (@peterm's comment):
in case think it's best start timer (and modify/update volatile variable delay time). in mean time, other interrupts can fired , handled. in timer interrupt, pin can set/reset.
also keep in mind arduino digitalwrite
function rather slow (it has map arduino pin numbers port/pin), in case better write port/pin directly. depending on clock speed, using digitalwrite
taking large part of 10µs delay.
some interesting info delaymicroseconds()
combined interrupts:
on arduino there timed interrupts configured unaware of. when interrupt received during execution of delaymicroseconds(), timing of
delaymicroseconds()
wrong. can of course stop interrupts before callingdelaymicroseconds()
, enable them afterwards, again impact timing accuracy duration of compiled code enabling/disabling.
Comments
Post a Comment