CPU定时器0模块初始化:
#include "DSP28_Device.h"
struct CPUTIMER_VARS CpuTimer0; //对用户开放的CPU定时器只有CpuTimer0,CpuTimer1
struct CPUTIMER_VARS CpuTimer1; //和CpuTimer2被保留用作实习操作系统OS(例如DSP
struct CPUTIMER_VARS CpuTimer2; //BIOS)
//初始化CpuTimer0。
void InitCpuTimers(void)
{
CpuTimer0.RegsAddr = &CpuTimer0Regs; //使得CpuTimer0.RegsAddr 指向定时器寄存器
CpuTimer0Regs.PRD.all = 0xFFFFFFFF; //初始化CpuTimer0的周期寄存器
CpuTimer0Regs.TPR.all = 0; //初始化定时器预定标计数器
CpuTimer0Regs.TPRH.all = 0;
CpuTimer0Regs.TCR.bit.TSS = 1; //停止定时器
CpuTimer0Regs.TCR.bit.TRB = 1; //将周期寄存器PRD中的值装入计数器寄存器TIM中
CpuTimer0.InterruptCount = 0; //初始化定时器中断计数器
}
//Timer(指定的定时器),Freq,Period
void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)
{
Uint32 temp;
Timer->CPUFreqInMHz = Freq;
Timer->PeriodInUSec = Period;
temp = (long) (Freq * Period);
Timer->RegsAddr->PRD.all = temp; //给定时器周期寄存器赋值
Timer->RegsAddr->TPR.all = 0; //给定时器预定标寄存器赋值
Timer->RegsAddr->TPRH.all = 0;
// 初始化定时器控制寄存器:
Timer->RegsAddr->TCR.bit.TIF=1; //清除中断标志位
Timer->RegsAddr->TCR.bit.TSS = 1; //停止定时器
Timer->RegsAddr->TCR.bit.TRB = 1; //定时器重装,将定时器周期寄存器的值装入定时器计数器寄存器
Timer->RegsAddr->TCR.bit.SOFT = 1;
Timer->RegsAddr->TCR.bit.FREE = 1;
Timer->RegsAddr->TCR.bit.TIE = 1; //使能定时器中断
Timer->InterruptCount = 0; //初始化定时器中断计数器
}
主函数模块:
#include "DSP28_Device.h"
void main()
{
InitSysCtrl();
DINT;
IER = 0x0000;
IFR = 0x0000;
InitPieCtrl();
InitPieVectTable();
InitPeripherals();
InitGpio();
PieCtrl.PIEIER1.bit.INTx7 = 1;
IER |= M_INT1;
EINT;
ERTM;
ConfigCpuTimer(&CpuTimer0, 150, 1000000);
StartCpuTimer0();
for(;;)
{
}
}
CPU定时器0周期中断函数:
interrupt void TINT0_ISR(void) // CPU-Timer0中断函数
{
CpuTimer0.InterruptCount++;
if(CpuTimer0.InterruptCount==1)
{
GpioDataRegs.GPFCLEAR.bit.GPIOF14=1; //XF引脚低电平,D3亮
}
if(CpuTimer0.InterruptCount==2)
{
GpioDataRegs.GPFSET.bit.GPIOF14=1; //XF引脚高电平,D3灭
CpuTimer0.InterruptCount=0;
}
CpuTimer0Regs.TCR.bit.TIF=1; //清除定时器中断标志位
PieCtrl.PIEACK.bit.ACK1=1; //响应同组其他中断
EINT; //开全局中断
}