chipKIT® Development Platform

Inspired by Arduino™

Timer interrupts and millis()

Created Sun, 16 Dec 2012 02:26:00 +0000 by helpme


helpme

Sun, 16 Dec 2012 02:26:00 +0000

With Timer Interrupts defined in setup():

T3CON    = 0x00;      // Stop the Timer and Reset Control register
  T3CON    = 0x70;      // Set prescaler at 1:256, internal clock source
  TMR3     = 0x0;        // Clear timer register
  PR3      = 0xFFFF;      // Load period register
  IPC3SET  = 0x000C;  // Set priority level=3
  IPC3SET  = 0x0001;  // Set subpriority level=1
  IFS0CLR  = 0x00001000;  // Clear Timer interrupt status flag
  IEC0SET  = 0x00001000;  // Enable Timer interrupts
  T3CONSET = 0x8000; // Start Time

The interrupt routine gets called and functions as expect, Albeit the Alarm.delay(100) routine call in loop() hangs. relevent line of code from Alarm.delay():

unsigned long start = millis();
while( millis() - start <= ms) serviceAlarms();

although, if I comment out the aforementioned Timer Interrupt setup code lines, the Alarm.delay works as expected.

My feeble understanding says that these two items should not be interacting, although it seems as though something is going on that I don't understand. Please enlighten me...

The Timer Interrupt routine reads some analog pins and updates some arrays, and the Alarm.delay causes reads from a OneWire device and updates a different arrays.

Thanks,

Fred


helpme

Sun, 16 Dec 2012 06:07:38 +0000

One additional detail: In the ISR if I do any of the following the interrupts cease and the Alarm.delay works

IFS0CLR  = 0x00001000;  // Clear Timer interrupt status flag
IFS0CLR  = 0x00001000;  // Clear Timer interrupt status flag
  IEC0SET  = 0x00001000;  // Enable Timer interrupts
mT3ClearIntFlag(); // Clear interrupt flag

What does clearing the interrupt status supposed to do? The PIC32MX3XX/4XX family data sheet only states that this should be done but not why. Also, how is one supposed to setup for periodic interrupts, it seems as though the data sheet is describing this but clearing the Interrupt Status flag seems to disabling periodic interrupts.

Thanks,

Fred


majenko

Sun, 16 Dec 2012 11:50:30 +0000

What does clearing the interrupt status supposed to do? The PIC32MX3XX/4XX family data sheet only states that this should be done but not why.

When an interrupt is triggered the associated "IF" flag is set. This both indicates to you, and to the interrupt system, that the interrupt has fired. While this flag is set it is assumed that the user is handling the interrupt. The interrupt won't fire again until after the "IF" flag is cleared. This prevents interrupt race conditions where an interrupt triggers again while it's still being handled from the previous triggering.


helpme

Sun, 16 Dec 2012 18:48:57 +0000

Thanks, this confirms my understanding as to what should happen.

Does anyone want to venture a guess as to why I am seeing the opposite happen, clearing the IF bit disables future instances of this interrupt?

Thanks,

Fred