Interrupts trigger mid procedure

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Gents,

 

I am currently using a 16 bit number that increments event millisecond via an interrupt. Whenever I want to inspect this number I disable interrupts, copy it then reanable interrupts. I remember reading somewhere in the datasheet or on avrfreaks that interrupts do no trigger mid operation. Are they talking about assembly operation? Meaning a 16 bit number takes more than just one operation because of the 8 bit architecture?

 

What I mean to ask is. Could an interrupt trigger if I were adding two 16 bit ints together or simply assigning one to another? Or would it wait for the procedure to finish?

 

Simaily if I were adding to doubles together, could an interrupt trigger between them?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

An interrupt won't happen (that is, won't be recognized) in the middle of an AVR instruction, but it can certainly happen between instructions.  Most operations on 16-bit values involve multiple AVR instructions - look at the compiler listing if you're writing in a HLL.  So you are accessing the 16-bit number correctly, AKA atomically (or, in a critical section).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you use avr-gcc then see:

 

http://www.nongnu.org/avr-libc/u...

I remember reading somewhere in the datasheet or on avrfreaks that interrupts do no trigger mid operation. Are they talking about assembly operation? Meaning a 16 bit number takes more than just one operation because of the 8 bit architecture?

This is the whole point of atomicity. Consider:
 

uint8_t a,b,c;
...
 c = a+b;

When compiled this produces:

//==>   c = a + b;
        lds r24,b
        lds r25,a
        add r24,r25
        sts c,r24

The actual addition in this is a single opcode so it's not the case that the ADD can start then be interrupted part way through so in theory this is safe. While the 16 bit equivalent:

//==>   c = a + b;
        lds r24,b
        lds r25,b+1
        lds r18,a
        lds r19,a+1
        add r24,r18
        adc r25,r19
        sts c+1,r25
        sts c,r24

Is dangerous because the 16 bit add is done using two opcodes ADD then ADC so an interrupt that chanegs things might occur between them.