DSP

F28335 定时器0-Timer0中断配置(完整)源代码+注释

2019-07-13 12:21发布

本定时器实现的功能就是在实现一个闪烁的LED灯和PWM波,LED亮灭周期和PWM周期均为200ms。 #include "DSP2833x_Device.h" // DSP2833x Headerfile Include File #include "DSP2833x_Examples.h" // DSP2833x Examples Include File #define LED11 GpioDataRegs.GPADAT.bit.GPIO11 interrupt void ISRTimer0(void); Uint16 i,led=0; void main(void) { InitSysCtrl(); InitXintf16Gpio(); DINT; InitPieCtrl(); IER = 0x0000; IFR = 0x0000; InitPieVectTable(); //基本初始化 EALLOW; PieVectTable.TINT0=&ISRTimer0; //将中断地址赋给你要写的中断 EDIS; InitCpuTimers(); //初始化系统时钟,这个TI的DSP2833X_CpuTimes.c文件自带 #if (CPU_FRQ_150MHZ) //这个也是TI的DSP2833X_CpuTimes.c文件自带 // Configure CPU-Timer 0 to interrupt every 500 milliseconds: // 150MHz CPU Freq, 50 millisecond Period (in uSeconds) ConfigCpuTimer(&CpuTimer0, 150, 100000); #endif #if (CPU_FRQ_100MHZ) // Configure CPU-Timer 0 to interrupt every 500 milliseconds: // 100MHz CPU Freq, 50 millisecond Period (in uSeconds) ConfigCpuTimer(&CpuTimer0, 100, 500000); #endif CpuTimer0Regs.TCR.bit.TSS=0; //写0打开定时器 写1就是停止定时器了 //在设置完时钟之后,一定要记得打开定时器 EALLOW; GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 0; //IO口的初始化 GpioCtrlRegs.GPADIR.bit.GPIO26=1; EDIS; GpioDataRegs.GPADAT.bit.GPIO26=0; EALLOW; GpioCtrlRegs.GPAMUX1.bit.GPIO11= 0; GpioCtrlRegs.GPADIR.bit.GPIO11=1; EDIS; EALLOW; GpioCtrlRegs.GPAMUX1.bit.GPIO0= 0; GpioCtrlRegs.GPADIR.bit.GPIO0=1; EDIS; IER|=M_INT1; //全局使能INT1 PieCtrlRegs.PIEIER1.bit.INTx7=1; //使能INT1.7即定时器0中断 EINT; ERTM; for(; ;) {}; } interrupt void ISRTimer0(void) { led++; CpuTimer0.InterruptCount++; PieCtrlRegs.PIEACK.all=PIEACK_GROUP1; //使能CPU接受第一组中断 GpioDataRegs.GPATOGGLE.bit.GPIO11=1; //I/O口翻转 GpioDataRegs.GPATOGGLE.bit.GPIO0=1; CpuTimer0Regs.TCR.bit.TIF=1; //清除中断标志位 CpuTimer0Regs.TCR.bit.TRB=1; //使能重载 } //=========================================================================== // No more. //=========================================================================== 这下面具体介绍一下,TI的DSP2833X_CpuTimes.c文件自带的两个函数第一个:就是中断时间设定函数,这个函数有3个参数
  • 哪个定时器
  • 定时器频率
  • 定时器周期
void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period) { Uint32 temp; // 初始化时钟,其中Freq是定时器频率,Period是定时器中断周期 //这里说一下他的计算过程,在150M的情况下,temp=150*500000=75M,而一个时钟周期是Tbclk=1/150M s , // temp*Tbclk=0.5s //所以这个是一个0.5秒闪烁的灯。 Timer->CPUFreqInMHz = Freq; Timer->PeriodInUSec = Period; temp = (long) (Freq * Period); Timer->RegsAddr->PRD.all = temp; Timer->RegsAddr->TPR.all = 0; //预定标寄存器给0 Timer->RegsAddr->TPRH.all = 0; // Initialize timer control register: Timer->RegsAddr->TCR.bit.TSS = 1; // 停止定时器 Timer->RegsAddr->TCR.bit.TRB = 1; // 重新装载定时器 Timer->RegsAddr->TCR.bit.SOFT = 0; Timer->RegsAddr->TCR.bit.FREE = 0; // 仿真模式关闭 Timer->RegsAddr->TCR.bit.TIE = 1; // 使能定时器中断 // Reset interrupt counter: Timer->InterruptCount = 0; } 第二个:CPU初始化函数 void InitCpuTimers(void) { // 指向定时器0地址 CpuTimer0.RegsAddr = &CpuTimer0Regs; // 初始化定时器周期寄存器值 CpuTimer0Regs.PRD.all = 0xFFFFFFFF; // 设定预定标数 CpuTimer0Regs.TPR.all = 0; CpuTimer0Regs.TPRH.all = 0; // 确保定时器在停止状态 CpuTimer0Regs.TCR.bit.TSS = 1; // 使能重新装载 CpuTimer0Regs.TCR.bit.TRB = 1; // 重置定时器计数值 CpuTimer0.InterruptCount = 0; // 定时器1和2和定时器0类似 // 指向定时器1和2地址 CpuTimer1.RegsAddr = &CpuTimer1Regs; CpuTimer2.RegsAddr = &CpuTimer2Regs; // 初始化定时器周期寄存器值 CpuTimer1Regs.PRD.all = 0xFFFFFFFF; CpuTimer2Regs.PRD.all = 0xFFFFFFFF; // 设定预定标数 CpuTimer1Regs.TPR.all = 0; CpuTimer1Regs.TPRH.all = 0; CpuTimer2Regs.TPR.all = 0; CpuTimer2Regs.TPRH.all = 0; // 确保定时器在停止状态: CpuTimer1Regs.TCR.bit.TSS = 1; CpuTimer2Regs.TCR.bit.TSS = 1; // 使能重新装载 CpuTimer1Regs.TCR.bit.TRB = 1; CpuTimer2Regs.TCR.bit.TRB = 1; // 重置定时器计数值 CpuTimer1.InterruptCount = 0; CpuTimer2.InterruptCount = 0; }