main loop-isr bidirectional control issue

Skip to first unread message


Apr 3, 2022, 1:07:51 PMApr 3
Hi all,
I'm trying to have a fast bidirectional control between the ISR and main loop without much success. Everything goes ok if in the main loop I use the IE registers flags to stop or start things in the ISR. It seems to not work ok if I try to control things in the main loop by reading registers which are changed in the ISR.

In the ISR I'm toggling one bit on and off each time the tmr1 is rollowing. This is generating a 50% duty cycle signal which I need to control two power solid state switches A and B. The max toggling frequency is around 8KHz.  There is a delay of 2us between switching, both switches have less than 1us latency between ON-OFF and OFF-ON states on the hardware side, the hardware is running ok.

In the main loop I'm reading a voltage and a current controlled by the A and B switches by reading first the ISR toggled bit and then initiating an ADC measurement. Even the Tcy speed is enough (48MHz clock), sometimes the ADC current and voltage values correspond to a wrong switch A and B position...and this is annoying. I did not read the ADC in the ISR because I need a perfect 50% duty cycle and a lot of other things are happening in the ISR adding a jitter....but perhaps this is the only way?

Any good ideas?  

BTW, ( this is another story) during USB serial communication ( interrupt or no interrupt driven)  the other fast processes have to be stopped...and the GIE flag can not be turned off for a short time in the main loop because connection is lost...

Oliver Seitz

Apr 4, 2022, 3:44:33 AMApr 4

perfect 50.000000% is not reachable, in no way. You will come nearest to that number by starting the A/D conversion very early in the only ISR, doing the jittering stuff after the conversion is startet. You will not get less jitter by moving the conversion to the main program, as the main program is suspended while the ISR is running. Remember that it doesn't matter at what time you're reading the ADC, it only matters at what time you're starting the conversion.

Or, depending on the chip you're using: e.g. the 18f27k42 has an "Auto Conversion Trigger", which can start a conversion on a selected event, like timer rolling over or PWM output. No interrupt needs to be fired here to start a conversion, so that is the way to get really, really close to "perfect 50%".


Am Sonntag, 3. April 2022, 19:07:52 MESZ hat vsurducan <> Folgendes geschrieben:

You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit


Apr 4, 2022, 9:31:58 AMApr 4
Don't be child Kiste, :) of course anything  can't be perfect,  at this time I have a 50% duty cycle below 1% error at highest frequency and somewhere below 0.2% at lower frequency which is "perfect". Jitter depends on many factors, mostly of what is happening in the ISR where a zero error RTC is also running and from time to time the USB serial library "as it wants" as well. Of course reading ADC register is not essential but the sampling time. The problem is that syncronisation of ADC sampling outside the ISR seems it did not work, I'll try to fire it within the interrupts.

Reply all
Reply to author
0 new messages