chipKIT® Development Platform

Inspired by Arduino™

How to use a Timer to generate a clock signal on a pin?

Created Thu, 30 May 2013 14:20:20 +0000 by OldfordCJ


OldfordCJ

Thu, 30 May 2013 14:20:20 +0000

Hi,

I recently purchased a ChipKIT uC32 board. I would like to use one of the internal timers to generate a clock signal on one of the uC32 output pins. Eventually, I want the software to be able to vary the frequency (25kHz to 40kHz). This signal will be used to clock data to another device.

I am looking at the T1CON, TMR1, PR1, and T1CONSET registers to start up the timer. How do I route the timer's output to one of the uC32 output pins so I can see it on an oscilloscope?

I was able to do this fairly simply on an Arduino Uno R3 but that board is just too slow for the rest of my application.

Thanks in advance, Curt


majenko

Thu, 30 May 2013 16:18:40 +0000

There's 2 ways you can do this.

  1. Use the timer and OC modules to generate a PWM signal with 50% duty cycle

  2. Use the timer to trigger an interrupt to toggle an IO pin.

With option 1 you are limited on which IO pins you can use - check the data sheet for the chip on your board and the manual for the board to find which OC module routes to which pin(s). The second option is more processor intensive, but allows you to toggle any IO pin or combination of pins you like.


OldfordCJ

Fri, 31 May 2013 14:55:07 +0000

Hi majenko,

Thanks for pointing me in the right direction. I was able to use the Output Compare module and get various signals to appear on one of the output pins.

Now I will try to figure out how to have the timer generate an interrupt. I suppose I could just wire my output pin to an interrupt input pin and treat it like an external interrupt. However, I think I should be able to connect the timer internally to the interrupt module and not use any pins. Does that sound right?

Thanks, Curt


majenko

Fri, 31 May 2013 16:26:34 +0000

Yep, that sounds right :)

You need to enable the timer's interupt (see the TxIE flag in the IECx SFR), and create an ISR (see other libraries for the __ISR examples). Don't forget to clear the IFSx.TxIF flag in the interrupt... (note "x" is a number, and varies depending on which timer you are using - read the data sheet).

You will probably need to wrap your __ISR in a C scoping:

extern "C" {
  #include <sys/attribs.h>
  void __ISR(_TIMER_3_VECTOR, _T3_IPL_ISR) T3_IntHandler (void)
  {
    // Your code here
  }
}

The best place to see the ISR code is in the servo library in Servo/utility/int.c


OldfordCJ

Mon, 03 Jun 2013 17:00:32 +0000

Hi majenko,

Thanks... By the way, where do I find the servo library in Servo/utility/int.c you mentioned? Is it in the mpide directory structure installed on my local drive?

Curt


majenko

Mon, 03 Jun 2013 17:37:27 +0000

Yes. It's in the hardware/libraries folder.