chipKIT® Development Platform

Inspired by Arduino™

32 bit timer?

Created Mon, 27 Aug 2012 18:52:14 +0000 by educa


Mon, 27 Aug 2012 18:52:14 +0000


I read in the datasheet of the pic32 that timer1 = 16 bit but that you could combine timer2+3 to have a 32 bit timer.

I am looking for the following.

I'm looking for a counter which counts as fast as possible (80Mhz, 40Mhz ??) and I must be capable of doing the following things with it

  1. start that timer
  2. stop that timer
  3. reset that 32 bit counter/timer to 0
  4. read the actual value of the timer.

So I don't need to do pwm or interrupts, just use it as a counter in the background.

Could somebody please help me a little by telling how to define such a counter in MPIDE ? I know how to use arduino, but thats 16bit counter

Kind regards and much thank in advance,



Mon, 27 Aug 2012 20:34:14 +0000

I believe you can do all of the things you want to do with the Core Timer. I think you'll find the core timer references in section 02 of the PIC32 reference manual (CPU). Also, you can find some calls to the core timer routines in wiring.c. Note that there are already MPIDE core routines (like the millis and microsecond timers, as well as the task scheduler) that use the core timer, so you may need to disable those before you can use it.



Mon, 27 Aug 2012 20:40:25 +0000


I allready read about core timer and fact that it runs 40Mhz (hence the reason I reply so fast, I didn't have to check it)

I am aware of millis which is probably using the 25ns core timer to update, but I guess that somehow I can disable ALL interupts on chipkit ? If I do that the millis() won't work anymore, but my program won't be interrupted by it either.

So can I somehow disable all interrupts on chipkit max32 ?

And can you reset a core timer to 0? I read it overflows after about 107sec, but if I could reset it to 0, it wouldn't overflow for me in the code.

Kind regards; Bart


Tue, 28 Aug 2012 02:34:16 +0000

Yes, you can disable all interrupts. You can not disable exceptions, I think.

You can not change the value of the core timer. But you can change the core timer match register, so that you can trigger when your interrupt occurs.



Wed, 29 Aug 2012 00:27:41 +0000

Looking very nice.

I also discovered that there seems to be a function readcoretimer() which might be usefull.

Did I read well that ISRs on Pic32 don't have to push registers at start en pop them again on reti because the pic32 has 2 sets of registers?

Can you then tell me aprox how many cpu cycles are needed between the time an ISR is called and the effectively execution of the 1st command in the ISR? In an AVR MCU you first push all registers onto stack and that is even done at for example 16MHz (lot slower then pic32).

So is the PIC32 then accessing the ISR directly (no overhead from pushing registers)? That should be very speedy at 80Mhz :)

I also have 1 other question. The thing I want to make is a stepper motor controller to control a cnc lasercutter. The data has to be read from a SD card, but reading from SD card takes some time.

I could of course let the main code loop read from SD card and fill up some kind of byte/word buffer, thereby reading extra data if buffer gets low on characters + I could do all the step generation in the ISR of the coretimer.

But, that would mean that if I read SD in the main loop, coretimer CAN and WILL interrupt my main loop (while reading from SD) and execute its commands.

Are there risks involved with this where on a reti the SD commands won't resume?

I do think that normally this should work as long as I don't touch any of the pins used by the SD card insite the coretimer ISR , but maybe you know a good reason NOT to do it?

I can certainly not take the risk to read from SD and block the cpu with it, because lasercutter can need around 50Khz signals at max speed and thats 0.00002s or 20µS between each step. I really doubt that I can read anything from SD card in 20µS.

On an arduino reading 512 bytes takes around 2ms, but I need to test with chipkit because that might be faster.

Any ideas? Hints? Tips?