Cortex-M3定时器复习

目录

5.1定时器概述

5.2 用TIM1产生1s定时 

5.3使用通用定时器TIM3生成四路不同的PWM波形

5.4 TIMx具有输入模式,可以用来捕获外部触发 


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波,但是过于复杂,此处略过,后续有可能补上。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No_one-_-2022

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值