chipKIT® Development Platform

Inspired by Arduino™

What timer for 2MHz interrupt?

Created Wed, 23 May 2012 12:28:12 +0000 by jcobreros


jcobreros

Wed, 23 May 2012 12:28:12 +0000

Hi, I'm trying to have the max32 toggle some bits (Using LATx) at 2MHz. For this, I want to create an interrupt that fires every 500ns. I tried using attachCoreTimerService but I don't have the latest MPIDE release installed (where can I get it?) and would like to try something more low level.

What timer should I use if I just want to retain Serial port capabilities? I don't care about delay() or millis().

How can I set this timer so the ISR has as small overhead as possible? Could you point me to a good resource to learn about the PIC32 timers within the MPIDE? Thank you!


jcobreros

Thu, 24 May 2012 10:54:12 +0000

Ok, I figured it out (Have to read more and have more patience). I'm using ConfigIntTimer and OpenTimer. Looking into the timer.h helped a lot.

The weird thing is that the interrupt function seems to be firing at half the rate that it should. I've made sure my multiplier is set to 1. So, if I have an 80MHz and want a 1MHz interrupt, i would set the timer period to 80. But this way I only get 500KHz. Another problem is I've seem to hit a roof at 1MHz. My ISR function is very simple, only a LATxINV and clearIntFlag. Where is all the overhead coming from? This is my program. Any ideas how to speed it up? Thanks!!

PS: If I just put a LATxINV in the Loop() function, I can get it to toggle at around 5MHz, and if I put the LATxINV inside a while(1) inside the loop(), then it runs at 20MHz.

#include <plib.h>
#include <Time.h>


void setup()
{
    pinMode(47, OUTPUT);
    pinMode(39, OUTPUT);
  
   ConfigIntTimer2(T2_ON | T2_INT_PRIOR_3);
   OpenTimer2(T2_ON | T2_PS_1_1, 40);
}

void loop()
{
  //do nothing
}

extern "C"
{

    void __ISR(_TIMER_2_VECTOR,ipl3) playSam(void)
     {
       LATDINV = 0b1000000;
       mT2ClearIntFlag();  // Clear interrupt flag
     }
  

} // end extern "C"

EmbeddedMan

Thu, 24 May 2012 20:48:44 +0000

The core timer runs at 1/2 the main clock rate, so it's running at 40MHz. Does that help?

*Brian


Ryan K

Thu, 24 May 2012 21:39:23 +0000

Hello,

Brian is right, the timers are based off the peripheral bus clock by default and not the system clock. The peripheral bus clock is set to run at 1/2 the speed (this can be changed by changing some of pragma configs) but anyways in terms of things to read about learning how to use the Max32 at the low level, look at the PIC32MX datasheets it tells you what each bit of each config register does.

At the bottom of this page there are pdfs. http://www.microchip.com/wwwproducts/devices.aspx?ddocname=en545660

ww1.microchip.com/downloads/en/DeviceDoc/61156G.pdf

Best Regards, Ryan K Digilent


jcobreros

Fri, 25 May 2012 07:33:26 +0000

Thank you both! That explains the frequency issue. It feels good to realize I wasn't making a mistake somewhere. I've been reading the pic32 datasheet, and the detailed "by topic" manuals. It is very good (once you start getting used to it. It can be crazy at the beginning :) and I feel I have a good grasp now on timer and bit registers (it's fantastic). But still a lot to learn. I'm still hoping to find a way to make my ISR run faster. It only has to toggle some bits, and Im planning on using the timers as counters for some of my arrays, to save speed. But I'm still discouraged by it only running at 1MHz when it's practically empty. Any ideas on how to remove some overhead? Would it run faster in this case if I switch to MPLAB? (I know there's some overhead on the loop() function of Arduinos, but that shouldn't be a problem now with timers, right?

Thanks!


EmbeddedMan

Fri, 25 May 2012 13:31:33 +0000

Ryan,

I thought Gene had set up the peripheral clock to run at 1x the processor clock. Is that not true?

I don't believe the core timer is derived from the peripheral clock. I think it's always 1/2 the core clock. But I'm not 100% certain about this.

*Brian