`新建工程的详情见: 使用Haawking IDE 新建一个LED闪烁的工程 - 中科昊芯 DSP - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com),即上一个帖子。
一、编写程序
第一步:定时器初始化函数
//---------------------------------------------------------------------------
// InitCputimers:
//---------------------------------------------------------------------------
// This function initializes all three CPU timers to a known state.
//
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;
// 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;
// CpuTimer 1 and CpuTimer2 are reserved for DSP BIOS & other RTOS
// Do not use these two timers if you ever plan on integrating
// DSP-BIOS or another realtime OS.
//
// Initialize address pointers to respective timer registers:
CpuTimer1.RegsAddr = &CpuTimer1Regs;
CpuTimer2.RegsAddr = &CpuTimer2Regs;
// Initialize timer period to maximum:
CpuTimer1Regs.PRD.all = 0xFFFFFFFF;
CpuTimer2Regs.PRD.all = 0xFFFFFFFF;
// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
CpuTimer1Regs.TPR.all = 0;
CpuTimer2Regs.TPR.all = 0;
// Make sure timers are stopped:
CpuTimer1Regs.TCR.bit.TSS = 1;
CpuTimer2Regs.TCR.bit.TSS = 1;
// Reload all counter register with period value:
CpuTimer1Regs.TCR.bit.TRB = 1;
CpuTimer2Regs.TCR.bit.TRB = 1;
// Reset interrupt counters:
CpuTimer1.InterruptCount = 0;
CpuTimer2.InterruptCount = 0;
}
//---------------------------------------------------------------------------
// ConfigCpuTimer:
//---------------------------------------------------------------------------
// This function initializes the selected timer to the period specified
// by the "Freq" and "Period" parameters. The "Freq" is entered as "MHz"
// and the period in "uSeconds". The timer is held in the stopped state
// after configuration.
//
void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)
{
Uint32 temp;
//===========================================================================
// End of file.
//===========================================================================
第二步:初始化中断函数、调用定时器初始化,当计数减到0的时候,控制灯闪烁
int main(void)
{
// volatile unsigned int i;
InitSysCtrl(); //配置系统时钟是60MHz
LEDInit();
BeepInit();
DINT; //Clear all interrupts and initialize PIE vector table
CpuTimer1Regs.TCR.bit.TSS = 0;// To start or restart the CPU-timer, set TSS to 0
ConfigCpuTimer(&CpuTimer2, 60,100000 );//定时器2初始化,第二个参数为CPU SYSCLK = 60M, 第三个参数为设定的周期,us单位
CpuTimer2Regs.TCR.bit.TSS = 0;// To start or restart the CPU-timer, set TSS to 0