F28069M实时定时器例子

该代码示例展示了如何在F2806x微控制器上配置ePWM定时器以实现实时中断。ePWM1中断在周期匹配和比较A匹配时清零和设置,中断服务例程中更新计数器并控制LED状态。主要涉及实时中断、定时器配置和GPIO初始化。
//###########################################################################
//
// FILE:   Example_2806xEPwmRealTimeInt.c
//
// TITLE:  ePWM Real-Time Interrupt Example
//
//! \addtogroup f2806x_example_list
//! <h1>ePWM Real-Time Interrupt (epwm_real-time_interrupts)</h1>
//!
//! This example configures the ePWM1 Timer and increments
//! a counter each time an interrupt is taken. ePWM interrupt can
//! be configured as time critical to demonstrate real-time mode
//! functionality and real-time interrupt capability.
//! ControlCard LED2 (GPIO31) is toggled in main loop
//! ControlCard LED3 (GPIO34) is toggled in ePWM1 Timer Interrupt.
//! FREE_SOFT bits and DBBIER.INT3 bit must be set to enable ePWM1
//! interrupt to be time critical and operational in real time mode
//! after halt command.
//! In this example:
//!  - ePWM1 is initialized
//!  - ePWM1 is cleared at period match and set at Compare-A match
//!  - Compare A match occurs at half period
//!  - GPIOs for LED2 and LED3 are initialized
//!  - Free_Soft bits and DBGIER are cleared
//!  - An interrupt is taken on a zero event for the ePWM1 timer
//!
//! \b Watch \b Variables \n
//! - EPwm1TimerIntCount
//! - EPwm1Regs.TBCTL.bit.FREE_SOFT
//! - EPwm1Regs.TBCTR
//! - DBGIER.INT3
//
//###########################################################################
// $TI Release:  $
// $Release Date:  $
// $Copyright:
// Copyright (C) 2009-2022 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//   Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the
//   distribution.
//
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
### TI F28335 定时器配置与使用 #### 1. 初始化外设时钟 为了使能定时器功能,首先需要初始化并启用相应的外设时钟。这可以通过调用 `InitPeripheralClocks()` 函数来完成[^3]。 ```c // 启用所需外设时钟 InitPeripheralClocks(); ``` #### 2. 配置定时器参数 对于 TMS320F28335 的定时器配置,主要涉及以下几个方面: - **Prescaler (预分频)**:设置定时器的频率。 - **Period (周期)**:定义每次溢出的时间间隔。 - **Counting Mode (计数模式)**:可以选择递增、递减或中心对齐模式。 这些配置通常通过修改定时器寄存器实现,如 TIMx_PSC 和 TIMx_ARR 来调整预分频和周期值[^2]。 #### 3. 设置定时器中断 如果希望利用定时器触发某些事件,则需开启对应的中断服务例程(ISR)。下面是一个简单的例子展示如何注册一个按钮回调函数到 ISR 中去处理 LED 切换逻辑[^4]。 ```c #include "driverlib.h" void Timer_ISR(void){ // 清除定时器中断标志位 Timer_clearCaptureCompareInterrupt(TIMER_A_BASE, TIMER_CAPCMP_CHANNEL); // 执行具体操作... } ``` #### 4. 示例代码片段 这里给出一段完整的 C 语言代码用于演示 F28335 上的一个简单定时器应用实例。该程序会每隔一秒切换一次 GPIO 输出状态以点亮/熄灭连接在外围设备上的 LED 灯泡。 ```c #include "DSP2833x_Device.h" #include "DSP2833x_Examples.h" void main(void){ InitSysCtrl(); // 初始化系统控制模块 EALLOW; // 开启写保护访问权限 SysCtrlRegs.WDCR.bit.TMRST = 1; // 复位看门狗定时器 EDIS; InitPieCtrl(); // 初始化 PIE 控制器 IER = M_INT1; // 使能 CPU INT1 中断请求线 PieIER1.bit.INTx7 = 1; // 使能 TINT0 中断向量 InitTimer(); // 自定义函数:初始化定时器 while(1){ // 主循环体保持运行 __asm(" NOP "); } } interrupt void timer_isr(void){ GpioDataRegs.GPATOGGLE.all ^= 0x01; // 切换 GPADAT 寄存器第 0 位的状态 PieCtrlRegs.PIEACK.all |= PIEACK_GROUP1; // 发送应答信号给 PIE 控制器确认已响应此中断 } ``` 上述代码展示了如何在 TMS320F28335 微控制器上创建一个基于硬件定时器的服务程序,并将其关联至外部 IO 口的操作上来达到控制LED灯闪烁的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值