目录
5.1定时器概述
STM32F107有7个通用功能的定时器(还有几个专用的,例如看门狗,RTC):高级定时器TIM1,通用定时器TIM2~5,基本定时器TIM6、7。
5.2 用TIM1产生1s定时
void TIM1_config(){
/*定义一个TIM_TimeBaseInitTypeDef类型的结构体,该类型在stm32f10x_tim.h中定义*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*使能TIM1时钟源*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
/*系统时钟为72MHz,产生1s一次中断,有多种划分方法,本例为7200*10 000*/
//自动装载系数
TIM_TimeBaseStructure.TIM_Period=(10 000-1);
//预分频系数
TIM_TimeBaseStructure.TIM_Prescaler=(7200-1);
//向上计数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_UP;
//重复次数0
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
//时钟分割,用不到
TIM_TimeBaseStructure.TIM_ClockDivision=0;
//根据设定参数设置TIM1
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
//计数器使能
TIM_Cmd(TIM1,ENABLE);
}
在固件库文件中可以找到TIM_TimeBaseStructure的定义
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 */
uint16_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.
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;
5.3使用通用定时器TIM3生成四路不同的PWM波形
首先我们介绍TIM_OCInitTypeDef结构体
typedef struct
{
uint16_t TIM_OCMode; /*!< Specifies the TIM mode.定时器输出状态*/
uint16_t TIM_OutputState; /*!< Specifies the TIM Output Compare state.输出使能*/
uint16_t TIM_OutputNState; /*!< Specifies the TIM complementary Output Compare state.
This parameter can be a value of @ref TIM_Output_Compare_N_state
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_Pulse; /*!< Specifies the pulse value to be loaded into the Capture Compare Register.
This parameter can be a number between 0x0000 and 0xFFFF */
uint16_t TIM_OCPolarity; /*!< Specifies the output polarity.
This parameter can be a value of @ref TIM_Output_Compare_Polarity */
uint16_t TIM_OCNPolarity; /*!< Specifies the complementary output polarity.
This parameter can be a value of @ref TIM_Output_Compare_N_Polarity
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCNIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_N_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
} TIM_OCInitTypeDef;
我们需要掌握的PWM模式非常的简单,只有模式1和模式2.“CCR<=CNT"(模式1),“CCR>=CNT"(模式2)。一旦满足要求,OCXREF置低,引发中断。
void TIM3_Config(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM3,ENABLE);
TIM_TimeBaseStructure.TIM_Period=(10 000-1);
TIM_TimeBaseStructure.TIM_Prescaler=(7200-1);
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_UP;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_Cmd(TIM3,ENABLE);
/*通道1*/
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
/*调用通道1初始化程序,使能预装载寄存器*/
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
/*通道2*/
TIM_OCInitStructure.TIM_Pulse=CCR2_Val;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
/*通道3、4忽略*/
}
typedef struct
{
uint16_t TIM_OCMode; /*!< Specifies the TIM mode.定时器输出状态*/
uint16_t TIM_OutputState; /*!< Specifies the TIM Output Compare state.输出使能*/
uint16_t TIM_OutputNState; /*!< Specifies the TIM complementary Output Compare state.
This parameter can be a value of @ref TIM_Output_Compare_N_state
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_Pulse; /*!< Specifies the pulse value to be loaded into the Capture Compare Register.
This parameter can be a number between 0x0000 and 0xFFFF */
uint16_t TIM_OCPolarity; /*!< Specifies the output polarity.
This parameter can be a value of @ref TIM_Output_Compare_Polarity */
uint16_t TIM_OCNPolarity; /*!< Specifies the complementary output polarity.
This parameter can be a value of @ref TIM_Output_Compare_N_Polarity
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCNIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_N_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
} TIM_OCInitTypeDef;
5.4 TIMx具有输入模式,可以用来捕获外部触发
可以来获得外部的PWM波,但是过于复杂,此处略过,后续有可能补上。