Sonsivri

Electronics => General Electronics => Topic started by: Yazi on December 29, 2005, 09:26:29 09:26



Title: Exemple on dspic with hitech dspic compiler
Post by: Yazi on December 29, 2005, 09:26:29 09:26
//this exemples are tested on the hard with dspic30f2010 it work fine
 
//using timer1
 
#include
#include
#define PLL_VALUE 8.0
#define CLOCK_TICKS 4000000
#define TCY_PER_SECOND (PLL_VALUE*CLOCK_TICKS/4.0)
#define PRE_SCALE 256.0
 
 
void setupTimer1(float timeInS)
{
unsigned long period;
 
period=(unsigned long)(timeInS*TCY_PER_SECOND/PRE_SCALE)-1;
SetPriorityIntT1(3);
EnableIntT1;
OpenTimer1(T1_ON & T1_GATE_OFF & T1_IDLE_STOP & T1_PS_1_256 & T1_SYNC_EXT_OFF & T1_SOURCE_INT,period);
}
 
void interrupt tc_int(void) @ T1_VCTR
{
T1IF=0;
WriteTimer1(0);
 
LATE++;
 
}
 
int main(void)
{
TRISE=0;
setupTimer1(1.0); // argument is interval (in seconds) between interrupts
 
while(1);
}
 
//e using timer2
#include
#include
#define PLL_VALUE 8.0
#define CLOCK_TICKS 4000000 //External 4Mhz cristal
#define TCY_PER_SECOND (PLL_VALUE*CLOCK_TICKS/4.0)
#define PRE_SCALE 256.0
 
void setupTimer2(float timeInS)
{
unsigned long period;
period=(unsigned long)(timeInS*TCY_PER_SECOND/PRE_SCALE)-1;
SetPriorityIntT2(1);
EnableIntT2;
OpenTimer2(T2_ON & T2_GATE_OFF & T2_IDLE_STOP & T2_PS_1_256 & T2_SOURCE_INT &T2_32BIT_MODE_OFF,period);
}
void interrupt tc_int(void) @ T2_VCTR
{
T2IF=0;
WriteTimer2(0);
LATE++;
 
}
int main(void)
{
TRISE=0;
setupTimer2(1.0); // argument is interval (in seconds) between interrupts
 
while(1);
}
 
//if you tray with Setuptimer2(3.0) you have only 0.902816 second as interval betwin interrupt
//because periode =((3*4000000*8)/4*256)-1 =16E35 the PR2 is only 16bits he take only 6E35=28213
//calcul interval betwin interrupt= (PLL_VALUE*CLOCK_TICKS/4.0*PRE_SCALE)/28213= 902.816ms
//Simulation under MPLAB give 902.942125 ms
 
// exemple 3 timer 2and 3 as 32 bits timer mode with dspic compiler
#include
#include
#define PLL_VALUE 8.0
#define CLOCK_TICKS 4000000
#define TCY_PER_SECOND (PLL_VALUE*CLOCK_TICKS/4.0)
#define PRE_SCALE 256.0
 
void setupTimer23(float timeInS)
{
unsigned long period;
period=(unsigned long)(timeInS*TCY_PER_SECOND/PRE_SCALE)-1;
SetPriorityIntT2(3);
EnableIntT3;
OpenTimer23(T2_ON & T2_GATE_OFF & T2_IDLE_STOP & T2_PS_1_256 & T2_SOURCE_INT &T2_32BIT_MODE_ON,period);
}
void interrupt tc_int(void) @ T3_VCTR
{
T3IF=0;
WriteTimer23(0);
 
LATE++;
 
}
int main(void)
{
TRISE=0;
setupTimer23(1); // argument is interval (in seconds) between interrupts
 
while(1);
}
//with 32bit timer the interval betwin interrupts can go to 137438.95 sec (near 38 hours);)