Maker Pro
Maker Pro

Code execution in ISR too slow.

indeedisuper

Oct 7, 2015
5
Joined
Oct 7, 2015
Messages
5
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?
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
Hello
Can you run the micro at a higher frequency. What frequency are you currently using?
Adam
 

indeedisuper

Oct 7, 2015
5
Joined
Oct 7, 2015
Messages
5
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...:(
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
Can you stretch the timings of the incoming signal and then scale it back in software?
Adam
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
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.
 

indeedisuper

Oct 7, 2015
5
Joined
Oct 7, 2015
Messages
5
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?
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
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.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
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.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
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?
 

indeedisuper

Oct 7, 2015
5
Joined
Oct 7, 2015
Messages
5
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:

JWHassler

Dec 22, 2014
86
Joined
Dec 22, 2014
Messages
86
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.
 

Colin Mitchell

Aug 31, 2014
1,416
Joined
Aug 31, 2014
Messages
1,416
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.
 
Top