1、 基本原理
TMS320F28335的CPU Time有三個,分別為Timer0,Timer1,Timer2,其中Timer2是為操作系統DSP/BIOS保留的,當未移植操作系統時,可用來做普通的定時器。這三個定時器的中斷信號分別為TINT0, TINT1, TINT2,分別對應於中斷向量INT1,INT13,INT14。 下圖為定時器的結構框圖,圖中TIMH:TIM為計數寄存器,PRDH:PRD為周期寄存器。
詳細介紹在Tms320F2833x用戶指南的1.3.5節詳細介紹。
簡單說就是一個32bit 的減計數器,timer2留給操作系統作為系統節拍定時器用。timer0 和 timer1 給用戶使用,如果不使用操作系統tiemr2也可以使用。中斷系統如下圖所示:
2、 庫函數介紹
Ti給的庫函數 DSP2833x_CpuTimers.c 都把基本操作包含好了。用戶只需要調用函數即可。常用的函數就兩個如下所示:
InitCpuTimers(); //負責初始化
ConfigCpuTimer(&CpuTimer0, 150, 100000);//設置定時器頻率和周期
3、 實際應用
我們需要用定時器產生一個 ms 定時器,比如產生 500ms 定時去驅動LED指示燈。代碼如下所示:
__interrupt void cpu_timer0_isr(void) { CpuTimer0.InterruptCount++; PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;// Acknowledge this interrupt to receive more interrupts from group 1 //to do ... GpioDataRegs.GPATOGGLE.bit.GPIO18 = 1; } void User_CpuTimer0Init(void) { EALLOW; // This is needed to write to EALLOW protected registers PieVectTable.TINT0 = &cpu_timer0_isr; EDIS; // This is needed to disable write to EALLOW protected registers InitCpuTimers(); //初始化 CPU定時器 ConfigCpuTimer(&CpuTimer0, 150, 100000); //500000--500ms CpuTimer0Regs.TCR.all = 0x4000; // Use write-only instruction to set TSS bit = 0 IER |= M_INT1; // Enable CPU INT1 which is connected to CPU-Timer 0: PieCtrlRegs.PIEIER1.bit.INTx7 = 1; // Enable TINT0 in the PIE: Group 1 interrupt 7 // Enable global Interrupts and higher priority real-time debug events EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM }
主函數代碼如下所示:
void main(void) { InitSysCtrl(); DINT; //禁止全局中斷 InitPieCtrl(); // 初始化PIE模塊 IER = 0x0000; //清除 CPU寄存器 IFR = 0x0000; InitPieVectTable(); //初始化中斷服務表 EALLOW; GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 0; GpioCtrlRegs.GPADIR.bit.GPIO18 = 1; EDIS; User_CpuTimer0Init(); //CPU Timer0 初始化 EINT; // 使能全局中斷 ERTM; // Enable Global realtime interrupt DBGM while(1) { } }