You have no items in your shopping cart.

Subtotal:Rs.0.00
DSPIC Boards

An Easyway to dsPIC30F4011 Timer Automatic Prescalar Configurations

an-easyway-to-dspic30f4011-timer-automatic-prescalar-configurations

Timer can be configured by selecting the prescalar, postscalar, FCY and FOSC frequency. For a 16 bit timer, the time period is calculated by (65535*TOSC), TOSC=1/FOSC.

Example:

 XTAL=10MHz, PLL=8, FOSC=80MHz, FCY=FOSC/4=20MHz

For 20MHz FCY the TCY=1/20MHz=0.05us (micro seconds).

So a 16bit Timer can produce 65535*0.05us=3276.75us ie 3.275ms

Suppose the user wish to change the delay period, then the above calculations should be performed repeatedly.

Many compilers such as MIKROC for PIC, dsPIC etc, provides inbuilt libraries. So the user can do the timings easily. But their source codes are available as dll or encodes where the user cannot go through.

Here are some simple examples managed to write in MPLAB C30 for dsPIC30F4011. This Example requires only TWO Parameters one is delay, and the other one is interrupt Enable. Delay in terms of milliseconds.

Both 16bit Timer and 32 timer functionality Explained. The C Code can produce upto 838ms (16 bit Timer) and 214s (32 Timer) at FCY of 20MHz.

 

Main.c

 

#include <p30f4011.h>

#include “Timers.h”

 

_FOSC(CSW_FSCM_OFF & XT_PLL8);             //10MHz-XTAL * 8PLL=80MHz

_FWDT(WDT_OFF);

_FBORPOR(PBOR_ON & MCLR_EN);

_FGS(CODE_PROT_OFF);

 

unsigned char FLG=0;

 

void __attribute__((interrupt__,no_auto_psv)) _T1Interrupt(void)

{

    IFS0bits.T1IF=0;

    LATF0 ^=1;                                                               

    TMR1   = 0;                                                                                                                              

}

 

void __attribute__((interrupt__,no_auto_psv)) _T5Interrupt(void)

{

    IFS1bits.T5IF=0;

    LATF5 ^= 1;                                                              

    TMR4    = 0;                                                                            

    TMR5    = 0;                                                                                     

}

 

int main(void)

{

                        TRISF = 0x0000;

                        FLG = Timer1_Init(500,1);            //500ms

                        if(FLG) _LATF2 = 1;

                        else _LATF4 = 1;

                        TMR1_On();

 

                        Timer45_32_Init(10000,1);         //10seconds

                        Timer45_On();

                        while(1);

}


 

Timers.h

 

 

#ifndef TIMER_H

#define TIMER_H

 

#ifndef FOSC

#define FOSC                 20000000UL      //Crystal * PLL/4 = 10MHz*8/4=20Mhz

#endif

 

#define MHz_1            1000000UL

#define TOSC              (1.0/FOSC)

 

#define Count_1us  (FOSC/MHz_1)

#define Count_1ms  (Count_1us*1000)

 

unsigned int Timer1_Init(float time, unsigned char Interrupt) //time in ms: Max Possible 838ms@20MhzFCY

{

         float PERIOD=0;

        float PRESCALE[4] = {1.0,8.0,64.0,256.0};

        unsigned int i=0,ERROR=0;

                        if(time==0)                                                                                                                                                                                                                                                                                              {

         T1CON = 0x0000;                                                                                                                               

         PR1   = 0;

        TMR1  = 0;

                        }

                        else

                        {

                            do

                         {

                           PERIOD = (((Count_1ms * time)/PRESCALE[i])-1);        //1ms=20000 @20Mhz FOSC

                            i++;

                            }

                        while((PERIOD>65535) && (i<4));

                                               

                          if((i==4) && (PERIOD>65535))

                            {

                                  ERROR = 1;

                                  PR1   = 0;

                               }

                       else

                             {

                                     ERROR = 0;

                                     PR1   = PERIOD;    //Set the Time Period

                                     T1CON = ((T1CON & 0xffcf) | ((i-1)<<4));//Switch OFF Timer-1 & select Pre-scaler

                                      TMR1  = 0;   //Initialize the Timer-1 Register

                                   }

                        }

                        if(Interrupt==1) _T1IE = 1;

                        return ERROR;

}

 

void TMR1_On(void)

{

                        T1CONbits.TON=1;

}

 

 

 

 

 

 

 

 

unsigned int Timer45_32_Init(unsigned int time, unsigned char Interrupt)

{

                        unsigned long PERIOD=0;

                        unsigned int ERROR=1;

                        if(time==0)                                                                                                                                                                                                                                                                     

          {

             T5CON = 0x0008;

             T4CON = 0x0000;                                                                                                                               

             PR4   = 0;

             PR5   = 0;

            TMR4  = 0;

            TMR5   = 0;

             }

             else

             {

               PERIOD = (Count_1ms * time)-1;    //1ms=20000 @20Mhz FOSC, 2power32-1us ie 214s possible

               PR4    = PERIOD & 0xffff;     //Set the Time Period

               PR5    = PERIOD>>16 & 0xffff;

                T4CON  = 0x0008;          //Set 32 bit timer

                TMR4   = 0;                 //Clear Timer count Registers

                 TMR5   = 0;

               }

                     if(Interrupt==1) _T5IE = 1;

                     return ERROR;

}

 

void Timer45_On(void)

{

                        T4CONbits.TON=1;

}

 

#endif         /*TIMER_H*/

 

dsPIC30F_Timer

 

Tags : dspic source codedspic timerdsPIC30F4011 Timer Automatic Prescalar Configurations
sukumar

The author sukumar