Connect with us

Code execution in ISR too slow.

Discussion in 'Microcontrollers, Programming and IoT' started by indeedisuper, Oct 7, 2015.

Scroll to continue with content
  1. indeedisuper

    indeedisuper

    5
    0
    Oct 7, 2015
    Hello,

    I have code inside an ISR that evaluates the pulse info of a PWM pulse from one of the digital I/O pins. I checked the time it takes for the ISR to execute which turned out to be 10us, but I need to be able to sample the PWM input at a rate of less than 1 us.

    Is there anything I can do to solve this? I've skimmed down the code within the ISR as much as possible. Do I have to go look for a faster controller?
     
  2. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    Hello
    Can you run the micro at a higher frequency. What frequency are you currently using?
    Adam
     
  3. indeedisuper

    indeedisuper

    5
    0
    Oct 7, 2015
    I am using Pololu's Orangutan X2 and I believe the frequency of the MCU is set. More insight on the story is I am using US Digital's MA3 P10 encoder:
    http://www.usdigital.com/assets/datasheets/MA3_datasheet.pdf?k=635798396611514737

    All of the code evaluating the pulses were originally in the ISR and everything works (positioning etc) with the exception of being able to sample the pulses when they get smaller than 10us.

    I have taken most of the code out of the ISR with the exception of reading in the rise and fall times of the pulses and the ISR clocks in at 1.5us...which is good but not good enough...:(
     
  4. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    Can you stretch the timings of the incoming signal and then scale it back in software?
    Adam
     
  5. indeedisuper

    indeedisuper

    5
    0
    Oct 7, 2015
    That wouldn't be possible, unfortunately.
     
  6. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    Looking at the circuit for the dev board you are using one of the micros has a 20Mhz clock, this should easily achieve less than 1 microsecond instruction cycle. See if there is a pre-scalar you can change.
     
  7. indeedisuper

    indeedisuper

    5
    0
    Oct 7, 2015
    Correct me if I'm wrong but isn't pre-scalar typically used to slow down a system clock because the ATmega should be running at its max clock speed?
     
  8. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    Yes correct, but is it running at full speed internally. If you do a test with a pin setting it high and then low what timings do you get? This will give you an indication of the clock it's using.
     
  9. hevans1944

    hevans1944 Hop - AC8NS

    4,595
    2,149
    Jun 21, 2012
    Looking at the MA3 datasheet, it appears you need to measure pulse widths with one microsecond (or better) resolution. And for best accuracy you need to measure both the "on" time and the "off" time and compute the angular position based on those two measurements. Have you considered using another dedicated microprocessor, perhaps a Microchip PIC, to off-load this chore from your main controller?

    You could use the PIC to make the measurements and send the result either as serial data or parallel data at fixed intervals, say every two to five (or more) milliseconds, depending on encoder resolution. A position update requires either 1,024 μs or 4,096 μs depending on resolution, so there would be plenty of time to send the conversion data over a serial link as a binary number between conversions. You could even encode it as BCD characters and send it in ASCII and still have plenty of time left over. I am not suggesting you do that, but it could be an aid to troubleshooting. And you could revert back to binary for the final code with little additional effort.

    It will be a little tricky measuring the pulse duration and sending the results in between measurements (perhaps every other measurment?) but doable IMHO.
     
  10. Harald Kapp

    Harald Kapp Moderator Moderator

    11,310
    2,589
    Nov 17, 2011
    What about using the timer/counter hardware to do the hard work of measuring the pulse duration and using the ISR only to forward the measured interval from the timer/counter's register to the software for further handling?
     
  11. indeedisuper

    indeedisuper

    5
    0
    Oct 7, 2015
    Oooh, I like the idea that Harald just suggested. I think I'll give that a go first...but in order to use the timer and counter hardware to measure the pulse width, doesn't it still need an ISR to trigger a pin to differentiate between the rising edge and falling edge?

    @Arouse1973 , so it seems that the MCU is going at 20Mhz

    @hevans1944 , I would prefer not to have to purchase another device. I think it's okay to disregard for instance, pulses less than 2us? I would be giving up some accuracy but on the grand scheme of this it shouldn't make too much of a difference?

    Also, some updates on the problem are:
    - The encoder readings come out fine when it's being turned forward and backwards with the only errors occurring when it's going past the 0 point clockwise or counter clockwise...
     
    Last edited: Oct 8, 2015
  12. JWHassler

    JWHassler

    80
    16
    Dec 22, 2014
    If your microprocessor has two Input capture units, set them to capture opposite edges.
    No interrupt is necessary: do the calculation whenever the second capture-unit sets its flag.
     
  13. Colin Mitchell

    Colin Mitchell

    1,417
    313
    Aug 31, 2014
    You have to talk about things in the form of instruction-cycles.
    We need to know the clock speed because it is divided by 4, so 20MHz will allow 5 instruction cycles of 1uS.
    The way I did it was to pole an input on a regular basis and take the result for processing.
    You can't do much with 5uS.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-