1,使能定时器模块的时钟(必须先使能时钟,再操作定时器模块寄存器)
在void InitPeripheralClocks(void)函数中:
SysCtrlRegs.PCLKCR3.bit.CPUTIMER0ENCLK = 1; // CPU Timer 0
SysCtrlRegs.PCLKCR3.bit.CPUTIMER1ENCLK = 1; // CPU Timer 1
SysCtrlRegs.PCLKCR3.bit.CPUTIMER2ENCLK = 1; // CPU Timer 2
2,初始化定时器模块寄存器及使能PIE级中断
2.1,在InitCpuTimer0函数中,
ConfigCpuTimer(&CpuTimer0Regs, Freq,Period);// 100us
IER |= M_INT1;//enable group interrupt
PieCtrlRegs.PIEIER1.bit.INTx7= 1;// Enable TINT0 in the PIE: Group 1 interrupt 7
StartCpuTimer0();
2.2,在InitCpuTimer1函数中,
ConfigCpuTimer(&CpuTimer1Regs, Freq,Period);
IER |= M_INT13;//enable group interrupt
//timer1 has no PIE level interrupt control
StartCpuTimer1();
注意:Timer1与Timer0使能中断部分稍有不同,Timer0所在中断组中,有8个中断共用一个CPU级中断,所以,除了使能CPU级中断外,还要使能PIE级中断。而Timer1只需要使能组中断即可,这些区别可从中断向量表中找到。
3,中断函数的确定
3.1,TI在DSP2833x_DefaultIsr.c中,对于每一个中断都定义了中断服务函数,我们可以找到相应的函数直接在里面编写代码就可以。但鉴于里面函数众多,不方便代码的查看,这里单独建立isr.c的文件,用于存放中断服务函数,函数的名字与DSP2833x_DefaultIsr.c中的一致。这样就要注释掉DSP2833x_DefaultIsr.c的相应函数,否则,编译器会报重定义的错误。
我采用宏定义的方式,注释相应函数。不使能相应中断时,将宏定义为0.使用则定义为1.
#define TINT0_ISR_ENABLE 1 //TIMER0 interrupt
#define INT13_ISR_ENABLE 1 //TIMER1 interrupt
#define INT14_ISR_ENABLE 0 //TIMER2 interrupt
#if (INT13_ISR_ENABLE == 0)
// Connected to INT13 of CPU (use MINT13 mask):
// Note CPU-Timer1 is reserved for TI use, however XINT13
// ISR can be used by the user.
interrupt void INT13_ISR(void) // INT13 or CPU-Timer1
{
// Insert ISR Code here
// Next two lines for debug only to halt the processor here
// Remove after inserting ISR Code
asm (" ESTOP0");
for(;;);
}
#endif
#if (INT14_ISR_ENABLE == 0)
// Note CPU-Timer2 is reserved for TI use.
interrupt void INT14_ISR(void) // CPU-Timer2
{
// Insert ISR Code here
// Next two lines for debug only to halt the processor here
// Remove after inserting ISR Code
asm (" ESTOP0");
for(;;);
}
#endif
3.2,在isr.c中重新定义中断服务函数
interrupt void TINT0_ISR(void) // CPU-Timer 0
{
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //Writing 1 to clear flag,must be needed
CpuTimer0Regs.TCR.bit.TIF=1; //Writing 1 to this bit clears the flag
LED1_TOG;
}
interrupt void INT13_ISR(void) // INT13 or CPU-Timer1
{
CpuTimer1Regs.TCR.bit.TIF=1; //Writing 1 to this bit clears the flag
1,使能定时器模块的时钟(必须先使能时钟,再操作定时器模块寄存器)
在void InitPeripheralClocks(void)函数中:
SysCtrlRegs.PCLKCR3.bit.CPUTIMER0ENCLK = 1; // CPU Timer 0
SysCtrlRegs.PCLKCR3.bit.CPUTIMER1ENCLK = 1; // CPU Timer 1
SysCtrlRegs.PCLKCR3.bit.CPUTIMER2ENCLK = 1; // CPU Timer 2
2,初始化定时器模块寄存器及使能PIE级中断
2.1,在InitCpuTimer0函数中,
ConfigCpuTimer(&CpuTimer0Regs, Freq,Period);// 100us
IER |= M_INT1;//enable group interrupt
PieCtrlRegs.PIEIER1.bit.INTx7= 1;// Enable TINT0 in the PIE: Group 1 interrupt 7
StartCpuTimer0();
2.2,在InitCpuTimer1函数中,
ConfigCpuTimer(&CpuTimer1Regs, Freq,Period);
IER |= M_INT13;//enable group interrupt
//timer1 has no PIE level interrupt control
StartCpuTimer1();
注意:Timer1与Timer0使能中断部分稍有不同,Timer0所在中断组中,有8个中断共用一个CPU级中断,所以,除了使能CPU级中断外,还要使能PIE级中断。而Timer1只需要使能组中断即可,这些区别可从中断向量表中找到。
3,中断函数的确定
3.1,TI在DSP2833x_DefaultIsr.c中,对于每一个中断都定义了中断服务函数,我们可以找到相应的函数直接在里面编写代码就可以。但鉴于里面函数众多,不方便代码的查看,这里单独建立isr.c的文件,用于存放中断服务函数,函数的名字与DSP2833x_DefaultIsr.c中的一致。这样就要注释掉DSP2833x_DefaultIsr.c的相应函数,否则,编译器会报重定义的错误。
我采用宏定义的方式,注释相应函数。不使能相应中断时,将宏定义为0.使用则定义为1.
#define TINT0_ISR_ENABLE 1 //TIMER0 interrupt
#define INT13_ISR_ENABLE 1 //TIMER1 interrupt
#define INT14_ISR_ENABLE 0 //TIMER2 interrupt
#if (INT13_ISR_ENABLE == 0)
// Connected to INT13 of CPU (use MINT13 mask):
// Note CPU-Timer1 is reserved for TI use, however XINT13
// ISR can be used by the user.
interrupt void INT13_ISR(void) // INT13 or CPU-Timer1
{
// Insert ISR Code here
// Next two lines for debug only to halt the processor here
// Remove after inserting ISR Code
asm (" ESTOP0");
for(;;);
}
#endif
#if (INT14_ISR_ENABLE == 0)
// Note CPU-Timer2 is reserved for TI use.
interrupt void INT14_ISR(void) // CPU-Timer2
{
// Insert ISR Code here
// Next two lines for debug only to halt the processor here
// Remove after inserting ISR Code
asm (" ESTOP0");
for(;;);
}
#endif
3.2,在isr.c中重新定义中断服务函数
interrupt void TINT0_ISR(void) // CPU-Timer 0
{
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //Writing 1 to clear flag,must be needed
CpuTimer0Regs.TCR.bit.TIF=1; //Writing 1 to this bit clears the flag
LED1_TOG;
}
interrupt void INT13_ISR(void) // INT13 or CPU-Timer1
{
CpuTimer1Regs.TCR.bit.TIF=1; //Writing 1 to this bit clears the flag