CPU有3个32位定时器0/1/2。CPU定时器0和1可以给用户使用,CPU定时器2留给实时操作系统(DSP/BIOS),如果不使用DSP/BIOS,那么CPU定时器2可以在应用程序中使用。
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
void InitCpuTimers(void);
void ISRCpuTimer0(void);
void ISRCpuTimer1(void);
struct CPUTIMER_VARS CpuTimer0;
struct CPUTIMER_VARS CpuTimer1;
int count_timer0=0;
int count_timer1=0;
void main(void)
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl();
DINT;
IER=0x0000;
IFR=0x0000;
InitPieCtrl();
InitPieVectTable();
InitCpuTimers();
//第三个参数指定了定时间 定时时间
ConfigCpuTimer(&CpuTimer0, 150, 1000000);
ConfigCpuTimer(&CpuTimer1, 150, 1000000);
EALLOW;
PieVectTable.TINT0=&ISRCpuTimer0;
PieVectTable.XINT13=&ISRCpuTimer1;
EDIS;
//使能PIE模块
PieCtrlRegs.PIECTRL.bit.ENPIE=1;
//PIE通道使能
PieCtrlRegs.PIEIER1.bit.INTx7=1;
//CPU级 中断允许位
IER|=0x0001;
IER|=0x1000;
EINT;
EALLOW;
//这个寄存器是外设的控制中断寄存器,这里选择定时器1作为输入
XIntruptRegs.XNMICR.bit.SELECT=0;
EDIS;
// CpuTimer0Regs.TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0
// CpuTimer1Regs.TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0
CpuTimer0Regs.TCR.bit.TSS = 0; // Use write-only instruction to set TSS bit = 0
CpuTimer1Regs.TCR.bit.TSS = 0; // Use write-only instruction to set TSS bit = 0
while(1);
}
interrupt void ISRCpuTimer0(void)
{
count_timer0++;
PieCtrlRegs.PIEACK.bit.ACK1=1;
}
interrupt void ISRCpuTimer1(void)
{
count_timer1++;
}
void InitCpuTimers(void)
{
// CPU Timer 0
// Initialize address pointers to respective timer registers:
CpuTimer0.RegsAddr = &CpuTimer0Regs;
// Initialize timer period to maximum:
CpuTimer0Regs.PRD.all = 0xFFFFFFFF;
// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
CpuTimer0Regs.TPR.all = 0;
CpuTimer0Regs.TPRH.all = 0;
// Make sure timer is stopped:
CpuTimer0Regs.TCR.bit.TSS = 1;
// Reload all counter register with period value:
CpuTimer0Regs.TCR.bit.TRB = 1;
// Reset interrupt counters:
CpuTimer0.InterruptCount = 0;
// CPU Timer 1
// Initialize address pointers to respective timer registers:
CpuTimer1.RegsAddr = &CpuTimer1Regs;
// Initialize timer period to maximum:
CpuTimer1Regs.PRD.all = 0xFFFFFFFF;
// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
CpuTimer1Regs.TPR.all = 0;
CpuTimer1Regs.TPRH.all = 0;
// Make sure timer is stopped:
CpuTimer1Regs.TCR.bit.TSS = 1;
// Reload all counter register with period value:
CpuTimer1Regs.TCR.bit.TRB = 1;
// Reset interrupt counters:
CpuTimer1.InterruptCount = 0;
}
void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)
{
Uint32 temp;
// Initialize timer period:
Timer->CPUFreqInMHz = Freq;
Timer->PeriodInUSec = Period;
temp = (long) (Freq * Period);
Timer->RegsAddr->PRD.all = temp;
// Set pre-scale counter to divide by 1 (SYSCLKOUT):
Timer->RegsAddr->TPR.all = 0;
Timer->RegsAddr->TPRH.all = 0;
// Initialize timer control register:
Timer->RegsAddr->TCR.bit.TSS = 1; // 1 = Stop timer, 0 = Start/Restart Timer
Timer->RegsAddr->TCR.bit.TRB = 1; // 1 = reload timer
Timer->RegsAddr->TCR.bit.SOFT = 0;
Timer->RegsAddr->TCR.bit.FREE = 0; // Timer Free Run Disabled
//开启时钟 中断使能
Timer->RegsAddr->TCR.bit.TIE = 1; // 0 = Disable/ 1 = Enable Timer Interrupt
// Reset interrupt counter:
Timer->InterruptCount = 0;
}
//===========================================================================
// No more.
//===========================================================================
ConfigCpuTimer第三个参数指定了定时时间
|