CR4 - The Engineer's Place for News and Discussion ®


Previous in Forum: Siemens Bus Bar Protection 7SS5..   Next in Forum: Current Unbalance in Motor When Connected to Siemens Masterdrive
Close
Close
Close
17 comments
Member

Join Date: Aug 2016
Posts: 8

AVR Interrupt Service Routine Not Executing as Fast as Expected

04/19/2017 2:48 AM

I'm developing a small logic analyzer with 7 inputs. My target device is an ATmega168 with a 20MHz clock rate. To detect logic changes I use pin change interrupts. Now I'm trying to find out the lowest sample rate I can detected these pin changes. I determined a value of minimum 5.6 µs (178.5 kHz). Every signal below this rate I can't capture properly.
My code is written in C (avr-gcc). My routine looks like:
Kod: ISR()
{
pinc = PINC; // char
timestamp_ll = TCNT1L; // char
timestamp_lh = TCNT1H; // char
timestamp_h = timerh; // 2 byte integer
stack_counter++;
}
My captured signal change is located at pinc. To localize it I have a 4 byte long timestamp value.
In the datasheet I read the interrupt service routine takes 5 clocks to jump in and 5 clocks to return to the main procedure. I'm assuming each command in my ISR() is taking 1 clock to be executed; So in sum there should be an overhead of 5 + 5 + 5 = 15clocks. The duration of one clock should be according to the clock rate of 20MHz 1/20000000 = 0.00000005 = 50 ns. The total overhead in seconds should be then: 15 * 50 ns = 750 ns = 0.75 µs. Now I don't understand why I can't capture anything below 5.6 µs. Can anyone explain what's going on?

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Comments rated to be "almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, rate them!
Guru

Join Date: Dec 2016
Posts: 2914
Good Answers: 115
#1

Re: AVR interrupt service routine not executing as fast as expected

04/19/2017 3:19 AM

Don't assume. Generate mixed C and assembly to see what the compiler is actually emitting. Not all instructions take one cycle, especially those accessing external data.

Register to Reply Score 1 for Good Answer
Commentator

Join Date: Feb 2010
Location: West Yorkshire, UK
Posts: 86
Good Answers: 8
#2

Re: AVR interrupt service routine not executing as fast as expected

04/19/2017 4:10 AM

Inputs are probably slugged to allow values to settle - like a low pass filter, hence the delay before you can see it. I am sure there is a technical term for this.

As pointed out above, don't mix codes, test it yourself (as you are doing) as that will be the best you can get.

Register to Reply
Commentator

Join Date: Feb 2010
Location: West Yorkshire, UK
Posts: 86
Good Answers: 8
#3
In reply to #2

Re: AVR interrupt service routine not executing as fast as expected

04/19/2017 4:19 AM

Propagation delay?

http://searchnetworking.techtarget.com/definition/propagation-delay

Register to Reply
Guru

Join Date: Apr 2010
Posts: 5185
Good Answers: 502
#4

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/19/2017 11:31 AM

I'm assuming each command in my ISR() is taking 1 clock to be executed;

No, each C instruction requires many clock cycles. The C compiler converts each C instruction to a small routine of machine instructions (or assembly code). Each machine instruction generally requires several clock cycles.

An example:

Example 1[edit]

For the multi-cycle MIPS, there are 5 types of instructions:

  • Load (5 cycles)
  • Store (4 cycles)
  • R-type (4 cycles)
  • Branch (3 cycles)
  • Jump (3 cycles)

https://en.wikipedia.org/wiki/Cycles_per_instruction

Register to Reply
Guru

Join Date: Dec 2016
Posts: 2914
Good Answers: 115
#5
In reply to #4

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/19/2017 12:08 PM

Yep, hence my advice to him/her to direct the compiler to output mixed assy & C and actually see what machine instructions are being emitted. One cannot assume a 1:1 mapping between C and assembly as it is almost never the case. Increment/decrement (++, --), for example, typically map directly to single-cycle instructions if the target is a register, but not if the target requires a memory access. The OP needs to see the machine instructions. There may also be instructions emitted 'behind the scenes' that do other things in support of the ISR and that do not correspond to an explicit C-language-level line in his code. There may also be optimisations silently performed that he's not aware of. As it is, until he looks at the actual machine code he's groping about in the dark.

Register to Reply Score 1 for Good Answer
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9444
Good Answers: 447
#6

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/19/2017 1:10 PM

1. Your terminology is muddled. You are (presumably) striving for the highest sampling rate not the lowest

2. To get the shortest possible interrupt service routine, you almost certainly need to write it in assembler. Or machine code if you're up to it.

.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
Popular Science - Weaponology - New Member Safety - ESD - New Member Hobbies - Fishing - New Member

Join Date: Sep 2006
Location: Near Frankfurt am Main, Germany. 50.390866N, 8.884827E
Posts: 17877
Good Answers: 194
#9
In reply to #6

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/20/2017 5:37 AM

Hi John, I like your post, it smells strongly of common sense....

In such situations for the OP, I would in his position, search for chips/circuits, that could improve the speed by each one handling a separate interrupt.

Maybe fast PICs suitably programmed, just to handle a single input and pass the interrupt along to the actual processor.

Or a purpose built, interrupt controller, mentioned here in this lesson:-

Interrupts!!

I myself have never done such high speed work myself, so I am a bad source of infos (sadly!)

See here also which should be a cheap and easy way to speed up the handling of interrupts:-

Programmable_Interrupt_Controller

But there are certain chips, families of chips, that are exceedingly fast (at least some were years ago, they may not be considered so fast nowadays!) are ECL chips for instance.

Other "versions" taken from this webpage:-

Emitter Coupled Logic

are:-

1) The equivalent of emitter-coupled logic made out of FETs is called source-coupled logic (SCFL).

The following may also be of particular interest due to the differential aspect.

2) A variation of ECL in which all signal paths and gate inputs are differential is known as differential current switch (DCS) logic.

Sadly, I have been away too long from such work, to be able to make any good suggestions.

But hopefully, these might help you with further thinking...

__________________
"What others say about you reveals more about them, than it does you." Anon.
Register to Reply
Guru

Join Date: Apr 2010
Posts: 5185
Good Answers: 502
#7

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/19/2017 1:16 PM

Try compiling with an -O2 option, if your compiler supports it. The optimizes the code.

Coding your ISR in assembly code might (or might not) shorten the execution time, depending on how good your compiler is.

Look at your code to see if you are doing anything in the ISR that doesn't need to be done.

Are PINC, TCNT1L, and TCNT1H, #defines (constants)? If so, can you make the variables timestamp_ll, timestamp_lh, and pinc global so that they won't have to be loaded in the ISR?

If you have to load the "char" variables each time, it would likely be faster if they could be combined together into a larger 16 (or 32) bit word so only one instruction is necessary.

There are probably other tweaks that you could do to streamline your ISR.

Register to Reply
Guru

Join Date: Mar 2011
Location: Lubbock, Texas
Posts: 10484
Good Answers: 128
#8

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/19/2017 4:05 PM

I suspect there is more code to it than that, such as setup code, and repetitive code.

If you do any analog reads, that will vastly increase cycle time.

__________________
If it ain't broke, don't fix it. Just build a better one.
Register to Reply
Member

Join Date: Mar 2017
Posts: 8
Good Answers: 2
#10

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/20/2017 7:18 AM

The AVR is not a good candidate for any sort of mini Logic Analyzer. Arduino instructions are typically much more than one clock cycle.

Why even bother doing a product like this when you can buy one that would be so much better for so cheap? The following is based on the PIC: https://www.aliexpress.com/item/1sets-New-Arrival-USB-Logic-Analyze-24M-8CH-MCU-ARM-FPGA-DSP-debug-tool/32246802326.html

It works with free desktop software to display/analyze the triggered capture results: https://www.saleae.com/downloads

Register to Reply
Guru
Popular Science - Weaponology - New Member Safety - ESD - New Member Hobbies - Fishing - New Member

Join Date: Sep 2006
Location: Near Frankfurt am Main, Germany. 50.390866N, 8.884827E
Posts: 17877
Good Answers: 194
#12
In reply to #10

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/20/2017 12:14 PM

That is brilliant, well found toilet busy light.

It also appears to be far faster than the OP needs (guessing slightly!) and cheaper it could not be!!!

I am sure that others here will be interested too. Sad, I have no usage for one at this time! DAMN!!

(I have slightly corrected the English!)

Price: US $4.74

1. 8 channels input;

2. Logical light indication: a small light indicate CHO electrical level;

3. Input feature:5PF,100K Ohm;

4. USB power supply;

5. Sampling rate can be set as below:

24MHz. 16MHz, 12MHz, 8MHz, 4MHz, 2MHz, 1MHz, 500KHz, 250KHz, 200KHz, 100KHz, 50KHz, 25KHz;

6. Data upload real time,the max of depth of collection is 10 Terrabytes, far more from a practical standpoint than most people will ever need.

__________________
"What others say about you reveals more about them, than it does you." Anon.
Register to Reply
Guru

Join Date: Mar 2011
Location: Lubbock, Texas
Posts: 10484
Good Answers: 128
#13
In reply to #10

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/20/2017 1:41 PM

What about this higher priced one with 16 channels? Worth it?

16 channel USB logic analyzer

__________________
If it ain't broke, don't fix it. Just build a better one.
Register to Reply
Guru
Popular Science - Weaponology - New Member Safety - ESD - New Member Hobbies - Fishing - New Member

Join Date: Sep 2006
Location: Near Frankfurt am Main, Germany. 50.390866N, 8.884827E
Posts: 17877
Good Answers: 194
#14
In reply to #13

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/20/2017 2:20 PM

....and that price is for THREE of them!!!

Find two friends.........

__________________
"What others say about you reveals more about them, than it does you." Anon.
Register to Reply
Guru

Join Date: Mar 2011
Location: Lubbock, Texas
Posts: 10484
Good Answers: 128
#15
In reply to #14

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/20/2017 2:26 PM

...and you will end up with one.

__________________
If it ain't broke, don't fix it. Just build a better one.
Register to Reply
Guru
Popular Science - Weaponology - New Member Safety - ESD - New Member Hobbies - Fishing - New Member

Join Date: Sep 2006
Location: Near Frankfurt am Main, Germany. 50.390866N, 8.884827E
Posts: 17877
Good Answers: 194
#16
In reply to #15

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/21/2017 4:43 AM

Funny and completely true!!!

__________________
"What others say about you reveals more about them, than it does you." Anon.
Register to Reply
Guru

Join Date: Mar 2011
Location: Lubbock, Texas
Posts: 10484
Good Answers: 128
#17
In reply to #16

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/21/2017 9:38 AM

Cheers, mate.

__________________
If it ain't broke, don't fix it. Just build a better one.
Register to Reply
Guru
Australia - Member - New Member

Join Date: Feb 2008
Location: Australia
Posts: 1712
Good Answers: 200
#11

Re: AVR Interrupt Service Routine Not Executing as Fast as Expected

04/20/2017 7:37 AM

It is also possible that your input module has inherent delay.

Years ago was building an A/D converter for video frequency. At 5MHz, the propagation time for the OpAmps limited me to 4 bit resolution and even wire/track lengths had to be considered.

If you have any signal conditioning, then this may be affecting your signal, rather than the capability of your actual sensor.

Get a CRO or similar device where you can "see" the input signal to confirm that your faster signal is actually arriving at the device.

__________________
Just an Engineer from the land down under.
Register to Reply
Register to Reply 17 comments
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Comments rated to be "almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, rate them!
Copy to Clipboard

Users who posted comments:

Andrew Westman (2); Andy Germany (4); James Stewart (4); JohnDG (1); Just an Engineer (1); Rixter (2); rogerggbr (2); toilet busy light (1)

Previous in Forum: Siemens Bus Bar Protection 7SS5..   Next in Forum: Current Unbalance in Motor When Connected to Siemens Masterdrive

Advertisement