0
\$\begingroup\$

I am using PIC32MX795F512L. Using Peripheral library to generate timer interrupt. Following is the code I am using:

//PIC32 is running at 72MHZ
#define FCY 72000000UL
#define FPB (FCY/2)
#define BAUDRATE    9600

#define PB_DIV 2
#define PRESCALE 256
#define TICKS_PER_SEC 4
#define T1_TICK_RATE (FCY/PB_DIV/PRESCALE/TICKS_PER_SEC)

#pragma config POSCMOD=HS,FNOSC=PRIPLL //FNOSC=PRIPLL
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1
#pragma config FPBDIV=DIV_2, FWDTEN=OFF//, CP=OFF, BWP=OFF

#include <stdio.h>
#include <stdlib.h>
#include <plib.h>

int main(int argc, char** argv) 
{
TRISDbits.TRISD9 = 0;
OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK_RATE);
ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_3);
INTEnableSystemMultiVectoredInt();

while(1)
{

}
return (EXIT_SUCCESS);
}

void __ISR(_TIMER_1_VECTOR, ipl3) Timer1Handler(void)
{

mT1ClearIntFlag();
LATDbits.LATD9 = 1;
}

This timer is running at 250ms speed but I am not sure. As I am beginner in timers, I don't know how to calculate values. Lets say if I want to run timer at 100ms what values should I use for PRESCALE and TICKS_PER_SEC Please help thanks.!

\$\endgroup\$
1
\$\begingroup\$

TICKS_PER_SECOND = 10;

However, your resolution may be wrong, so you may want to prescale at T1_PS_1_128 with PRESCALE = 128, or lower if needed to use an integer number or full scale of timer bits.

Its all pretty basic math.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Eric has already given you some numbers to try. I'm always unsure whether I've got my timer settings right on various chips, with the various initializations of the timer peripherals plus the crystal frequency to consider, so I hook up an LED to one of my pins, and run some code something like this:

do_timer_initialization();

void delay_500ms
{
    int i;
    for (i=0; i<5; i++)
    {
         wait_one_tick();    // wait 100 ms
    }
}

while(1)
{
    set_pin(LED_PIN,1);
    delay_500ms();
    set_pin(LED_PIN,0);
    delay_500ms();    
}

where the for loop in delay_500ms is adjusted so the delay is 500 ms based on the tick count.

The LED should blink once per second, which is easy enough to check by counting the number of blinks in a minute, and comparing that to 60.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.