disputedip.com

Tag: microchip

Timering with DSPic’s

by Martyn on Mar.22, 2009, under Microcontrollers, Robots

Timers on DSPics are easy - and after helping a few people out with timers at uni I thought I would post a really quick how-to on setting up a timer to interrupt at a set frequency. First lets start with a few bits you need to know:

Fosc = 40000000;

That’s it :D Just make sure that this value is the number in Hz on your oscillator (or a multiple of it if using PLL)

Next its important to remember there are different types of timers on a pic, do some research and rtfm before doing any pic / embedded work - will save you hours, I am using timer 1 - as its the first one in the manual :p

Next bits:

  1.  
  2.  
  3. T1CONbits.TCKPS = 0×02; // Select the PRESCALER
  4. TMR1 = 0×00; // Make sure the timer is starting from zero
  5. PR1 = 600; // How long the timer should run before an interrupt (in timer ticks)
  6. _T1IF = 0; // Clear the interrupt flag
  7. _T1IE = 1; // Enable the interrupt
  8. T1CONbits.TON = 1; // Turn the interrupt on
  9.  
  10. void __attribute__((__interrupt__)) __attribute__((no_auto_psv)) _T1Interrupt(void)
  11. {
  12. // Funky interrupt code goes
  13. }
  14.  
  15.  

To explain, the prescale waits x many ticks before incrementing the timer counter and is usually a set number like 1,8,32 or 64. To calculate the number for PR1 you then juse a very simple formula:

Fcy = Fosc/2

PR1 = (Freq^-1)/(Fcy^-1)

Where Freq is your desired interrupt frequency. Then just round the value of PR1 to the nearest integer if it is not one already. Simple!

As a side note - more information to why the interrupt function has a no_auto_psv attribute can be found at FlyingPic24.com

Hope this has been useful - please comment if you have any corrections/questions/comments.

Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...