资深版主
- 积分
- 10541
- 金钱
- 10541
- 注册时间
- 2017-2-18
- 在线时间
- 1908 小时
|
http://www.openedv.com/forum.php?mod=viewthread&tid=282841&extra=
有网友反应stm32f4xx_tim.c文件的第288行TIM_TimeBaseInit(),该函数中使用了TIM_TimeBaseInitTypeDef的结构体,但是该结构体中明确写出“该结构体不能适用于TIM6和TIM7”
虽然该函数的注释写明不适合TIM6和TIM7使用
[mw_shl_code=c,true]/**
* @brief TIM Time Base Init structure definition
* @note This structure is used with all TIMx except for TIM6 and TIM7.
*/
typedef struct
{
uint16_t TIM_Prescaler; /*!< Specifies the prescaler value used to divide the TIM clock.
This parameter can be a number between 0x0000 and 0xFFFF */
uint16_t TIM_CounterMode; /*!< Specifies the counter mode.
This parameter can be a value of @ref TIM_Counter_Mode */
uint32_t TIM_Period; /*!< Specifies the period value to be loaded into the active
Auto-Reload Register at the next update event.
This parameter must be a number between 0x0000 and 0xFFFF. */
uint16_t TIM_ClockDivision; /*!< Specifies the clock division.
This parameter can be a value of @ref TIM_Clock_Division_CKD */
uint8_t TIM_RepetitionCounter; /*!< Specifies the repetition counter value. Each time the RCR downcounter
reaches zero, an update event is generated and counting restarts
from the RCR value (N).
This means in PWM mode that (N+1) corresponds to:
- the number of PWM periods in edge-aligned mode
- the number of half PWM period in center-aligned mode
This parameter must be a number between 0x00 and 0xFF.
@note This parameter is valid only for TIM1 and TIM8. */
} TIM_TimeBaseInitTypeDef; [/mw_shl_code]
但是但是当我们使用TIM6的更新中断时,所需要设置的TIM6_ARR、TIM6_PSC、计数模式和分频系数都是可以经过
结构体变量TIM_TimeBaseInitStructure来设置的
TIM6初始化函数如下
[mw_shl_code=c,true]//通用定时器6中断初始化
//这里时钟选择为APB1的2倍,而APB1为42M
//arr:自动重装值。
//psc:时钟预分频数
//定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us.
//Ft=定时器工作频率,单位:Mhz
void TIM6_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE); ///使能TIM6时钟
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE); //允许定时器6更新中断
TIM_Cmd(TIM6,ENABLE); //使能定时器6
NVIC_InitStructure.NVIC_IRQChannel=TIM6_DAC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}[/mw_shl_code]
TIM6的中断服务函数是TIM6_DAC_IRQHandler
我们在该中断服务函数中设置LED1电平翻转提示进入更新中断
[mw_shl_code=c,true]//定时器6中断服务程序
void TIM6_DAC_IRQHandler(void)
{
if(TIM_GetITStatus(TIM6,TIM_IT_Update)==SET) //溢出中断
{
LED1=!LED1;//DS1翻转
}
TIM_ClearITPendingBit(TIM6,TIM_IT_Update); //清除中断标志位
}[/mw_shl_code]
|
|