本帖最后由 阳阳阳阳阳阳阳阳 于 2021-8-24 14:00 编辑
/******************************************************************
编 译 器:Haawking IDE V0.5.0
D S P:dsc28027勇士板
电 脑 配 置:Windows10 专业版; 64 位操作系统
仿 真 器:HX100V2
使 用 库:无
作 用:GPIO输入输出例程
说 明:D401来自定时器0中断服务函数
D400亮由Cputimer1控制,灭由CpuTimer2控制 版 本:V0.0.1
******************************************************************/
1. HaawkingIDE新建工程
2. 初始化LED:
/******************************************************************
函数名:void InitLED()
参 数:无
返回值:无
作 用:初始化LED
说 明:
******************************************************************/
- void InitLED()
- {
- EALLOW;
- GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0; //D400MUX = 0;
- GpioCtrlRegs.GPADIR.bit.GPIO7 = 1; //D400DIR = 1;
- GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; //D401MUX = 0;
- GpioCtrlRegs.GPADIR.bit.GPIO6 = 1; //D401DIR = 1;
- EDIS;
- }
初始化定时器0:
/******************************************************************
函数名:Timer0_init()
参 数:无
返回值:无
作 用:定时器0初始化-1ms 周期
说 明:
******************************************************************/
- void Timer0_init()
- {
- InitCpuTimers();
- //中断配置步骤-----1,开启模块中断使能,位于 Timer->RegsAddr->TCR.bit.TIE = 1;
- ConfigCpuTimer(&CpuTimer0, 120, 60000); //12MHz,1000us ,即为 1ms中断周期
- CpuTimer0Regs.TPR.bit.TDDR = 10000;
- CpuTimer0Regs.TCR.bit.TIE = 1; //使能中断
- CpuTimer0Regs.TCR.bit.TSS = 0; // To start or restart the CPU-timer, set TSS to 0
- //中断配置步骤-----2,重映射中断服务函数
- // Interrupts that are used in this example are re-mapped to
- // ISR functions found within this file.
- EALLOW;
- PieVectTable.TINT0 = &cpu_timer0_isr;
- EDIS;
- //中断配置步骤-----3,连接CPU中断Y
- IER_ENABLE(M_INT1);
- //中断配置步骤-----4,连接Y中断里的第几位
- PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
- }
定时器1/2初始化:
- 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;
- // 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;
- }
定时器1/2配置如下:
- ConfigCpuTimer(&CpuTimer1, 12, 500000);//定时器1配置,第二个参数为CPU SYSCLK = 12M, 第三个参数为设定的周期,us单位
- CpuTimer1Regs.TCR.bit.TSS = 0; // To start or restart the CPU-timer, set TSS to 0
- ConfigCpuTimer(&CpuTimer2, 12, 250000);//定时器2配置,第二个参数为CPU SYSCLK = 12M, 第三个参数为设定的周期,us单位
- CpuTimer2Regs.TCR.bit.TSS = 0; // To start or restart the CPU-timer, set TSS to 0
CpuTimer0 作为1ms周期,使D401闪烁;CpuTimer1 作为500ms周期,采用查询方式,控制D400亮;CpuTimer2 作为250ms周期,采用查询方式,控制D400灭:
- while(1)
- {
- if(timer0Base.msCounter>1000) //0.1s
- {
- timer0Base.msCounter=0;
- GpioDataRegs.GPATOGGLE.bit.GPIO6 = 1; //D401闪烁,指示定时器0中断周期
- }
- if(CpuTimer1Regs.TCR.bit.TIF == 1) //定时器1查询周期
- {
- CpuTimer1Regs.TCR.bit.TIF = 1; //标志位写1清0 clear flag
- CpuTimer2Regs.TCR.bit.TSS = 0; //启动定时器2
- GpioDataRegs.GPACLEAR.bit.GPIO7 =1; //D400亮
- }
- if(CpuTimer2Regs.TCR.bit.TIF == 1) //定时器2查询周期
- {
- CpuTimer2Regs.TCR.bit.TIF = 1; //标志位写1清0 clear flag
- CpuTimer2Regs.TCR.bit.TSS = 1; //关闭定时器2
- GpioDataRegs.GPASET.bit.GPIO7 =1; //D400灭
- }
- }
点击编译、编译完成后点击下载:
实验现象:
程序全速运行后,LED灯D400和D401保持闪烁。