【DSP学习】DSP28335定时器
0 参考资料
DSP自学视频(顾老师) 从入门到编写(C2000)——定时器
1 原理
1.1 CPU定时器的工作原理
中断信号何时发出:先设置 PRDH,再把 PRDH 的值装载到 TIMH 中;此后每经历一个 TIMCLK,计数器就减 1,直到减到 0 时发出中断信号。若还需继续计数,则再次把 PRDH 的值装载到 TIMH 中,重复上述过程。
TIMCLK 信号何时发出:先设置分频器 TDDRH,再把该值装载到 PSCH 中;此后每来一个系统时钟信号,计数器就减 1,直到减到 0 时输出一个 TIMCLK 信号。
1.2 定时时间计算
TIMCLK 的时间 =(1 + TDR 分频值)× 每个系统时钟信号的时间(即 1/150M s)
定时器时间 =(1 + PRD)× TIMCLK =(1 + PRD)×(1/150M)## 1.3 定时器中断
其中 INT13、INT14 这两个中断的优先级相对较低。
2 定时器跑马灯例子
#include"DSP2833x_Device.h"// DSP2833x Headerfile Include File#include"DSP2833x_Examples.h"// DSP2833x Examples Include FileinterruptvoidISRTimer0(void);//定时器0的周期中断函数voidmain(void){// Step 1. Initialize System Control:// PLL, WatchDog, enable Peripheral Clocks// This example function is found in the DSP2833x_SysCtrl.c file.InitSysCtrl();//初始化// Step 2. Initialize GPIO初始化GPIO// This example function is found in the DSP2833x_Gpio.c file and// illustrates how to set the GPIO to it's default state.InitGpio();//初始化GPIOInitCpuTimers();// 初始化定时器 For this example, only initialize the Cpu Timers// Step 3. Clear all interrupts and initialize PIE vector table:// Disable CPU interruptsDINT;// Initialize the PIE control registers to their default state.// The default state is all PIE interrupts disabled and flags// are cleared.// This function is found in the DSP2833x_PieCtrl.c file.InitPieCtrl();//初始化PIE中断// Disable CPU interrupts and clear all CPU interrupt flags:IER=0x0000;IFR=0x0000;// Initialize the PIE vector table with pointers to the shell Interrupt// Service Routines (ISR).// This will populate the entire table, even if the interrupt// is not used in this example. This is useful for debug purposes.// The shell ISR routines are found in DSP2833x_DefaultIsr.c.// This function is found in DSP2833x_PieVect.c.InitPieVectTable();// Interrupts that are used in this example are re-mapped to// ISR functions found within this file.EALLOW;// This is needed to write to EALLOW protected registersPieVectTable.TINT0=&ISRTimer0;//把定时器 0 中断向量重定向到我们自己写的`ISRTimer0` 也可以不用写这个,直接去默认的中断函数里面写,默认是函数在DSP2833x_Defaultsr.c中的interrupt void TINT0_ISR(void)EDIS;// This is needed to disable write to EALLOW protected registers// Enable CPU int1 which is connected to CPU-Timer 0IER|=M_INT1;//使能CPU中断1// Enable TINT0 in the PIE: Group 1 interrupt 7PieCtrlRegs.PIEIER1.bit.INTx7=1;// Enable global Interrupts and higher priority real-time debug events:EINT;// Enable Global interrupt INTMERTM;// Enable Global realtime interrupt DBGM// Configure CPU-Timer 0, 1, and 2 to interrupt every second:// 150MHz CPU Freq, 1 second Period (in uSeconds)ConfigCpuTimer(&CpuTimer0,150,1000000);//用的哪个中断,时间频率150M,每次定时要的时间单位是微秒StartCpuTimer0();//启动定时器函数for(;;){}}接下来编写中断服务函数:
interruptvoidISRTimer0(void){if(status==0){led1();}elseif(status==1){led2();}elseif(status==2){led3();}elseif(status==3){led4();}elseif(status==4){led5();}elseif(status==5){led6();}status++;if(status>5)status=0;// Acknowledge this interrupt to receive more interrupts from group 1PieCtrlRegs.PIEACK.all=PIEACK_GROUP1;CpuTimer0Regs.TCR.bit.TIF=1;CpuTimer0Regs.TCR.bit.TRB=1;}voidled1(void){GpioDataRegs.GPADAT.bit.GPIO0=0;GpioDataRegs.GPADAT.bit.GPIO1=1;GpioDataRegs.GPADAT.bit.GPIO2=1;GpioDataRegs.GPADAT.bit.GPIO3=1;GpioDataRegs.GPADAT.bit.GPIO4=1;GpioDataRegs.GPADAT.bit.GPIO5=1;}...//写void led1(void)~void led6(void)