Wait until finished then go into interrupt?

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

Is there a way in which I can run a segment of code or perhaps a function, finish that segment or function then go into the interrupt routine immediately after if the interrupt is triggered in the middle of the function?

I have several critical functions that run in the main loop which use values from sensors in which I read from the sensors only when PCINTx is triggered. However if the interrupt occurs while the processor is in the middle of a function, jumps out of the function and back in, it will not give me the desired value if the function running is using values from the sensor that triggerd the interrupt as they may change mid process. What I want is for the function to finish and then immediately enter the interrupt after completion if the interrupt occurs while running the function.

I have no idea how to do this. The only thing I know is that I can disable and enable interrupt service to ensure the function is completed without values being changed externally before its finished using cli(); an d sei();. But if I do that I may miss the new value to be read if the interrupt should occur during that time . Only thing I can think of is if there is an interrupt flag that I can read even if the interrupt is disabled to check after completion of each function and then enter the interrupt if the flag is set(or what ever value indicates an pin has changed value and the interrupt should have been triggered).

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

Quote:

Is there a way in which I can run a segment of code or perhaps a function, finish that segment or function then go into the interrupt routine immediately after if the interrupt is triggered in the middle of the function?

Just cli() and the beginning and sei() at the end. If in interrupt triggers in between its flag will be set and then serviced after the sei(). The danger is that TWO interrupts of the same type happen during the atomic block in which case only one instance will be triggered/serviced.

Cliff

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

yes, simply disable interrupts, and then re-enable them. As long as interrupts are disabled, no interrupt routine will execute. Once re-enabled, any pending interrupts will execute in priority order. (priority determined by the position of the vector in the vector table)

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

Ok, thanks for the help, I had considered that, but when I read through the data sheet, I didn't see any explicit explanation of whether or not a flag would be set and automatically executed once the interrupt was re-enabled. That makes things MUCH more simple for me since its automatically done. As for the pins enabling the ISR at the same time, mechanically that should not happen based on the timing of the gearbox and where the sensors are located, and I also have implemented a simple routine using flag bits to determine which pin on the port caused the interrupt, so that should not be an issue either. All of my sensory devices happen to be on PORTC, which was a necessary evil of the physical board design as it has to extremely compact.

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

Quote:

I didn't see any explicit explanation of whether or not a flag would be set and automatically executed once the interrupt was re-enabled.

Is it not the case that the datahseet shows that for every ??IE bit in a control register there is a correpsonding ??IF bit in a status regiser then? It's those ??IF flags that cause a jump via the associated vector table entry. Here's an example:
Quote:
• Bit 4 – ADIF: ADC Interrupt Flag
This bit is set when an ADC conversion completes and the Data Registers are updated.
The ADC Conversion Complete Interrupt is executed if the ADIE bit and the I-bit in
SREG are set. ADIF is cleared by hardware when executing the corresponding interrupt
handling vector. Alternatively, ADIF is cleared by writing a logical one to the flag.

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

Use buffers. In ISR take value from pins into buffers, in function copy buffers into variables on the start (without interrupts).