STM32 通用定时器的几种配置方式

STM32 通用定时器的几种配置方式

//------------------------------------------------------------------------------
// 1、普通定时使用

#include"stm32f10x.h"
#include"time.h"

static Time_NVIC_Config( void )
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 );
  NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );
  NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init( &NVIC_InitStructure );
}

static void Time_Config( void )
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE );
  //TIM_DeInit(TIM2);
  TIM_TimeBaseInitStructure.TIM_Prescaler = ( 10000 - 1 );      //时钟分频系数
  TIM_TimeBaseInitStructure.TIM_Period = ( 7200 - 1 );       //自动重装寄存器
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;    //向上计数模式
  TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;         //时钟分割,这里不涉及
#if 0
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter;
    //This parameter is valid only for TIM1 and TIM8
#endif
  TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure );        //配置参数
  TIM_ClearITPendingBit( TIMx, TIM_IT_Update );          //清除中断标示位
  TIM_ITConfig( TIMx, TIM_IT_Update, ENABLE );           //中断配置
  TIM_Cmd( TIMx, ENABLE );                //启动定时器
}

void Time_Init( void )
{
  Time_Config( );       //定时器配置
  Time_NVIC_Config( );      //嵌套中断向量初始化
}

//------------------------------------------------------------------------------
// 2、PWM输出

#include"stm32f10x.h"
#include"time.h"

#if 1

static Time_NVIC_Config( void )
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 );
  NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );
  NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init( &NVIC_InitStructure );
}
#endif

void Time_OUT_PWM_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( TIMx_OUT_GPIO_RCC, ENABLE );
  GPIO_InitStructure.GPIO_Pin = TIMx_OUT_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;               //复用推免式输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init( TIMx_OUT_Port, &GPIO_InitStructure );
}

static void Time_OUT_PWM_Config( void )
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure;
  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE );        //开启时钟
  TIM_DeInit( TIM2 );

  TIM_TimeBaseInitStructure.TIM_Prescaler = ( 100 - 1 );      //时钟分频系数
  TIM_TimeBaseInitStructure.TIM_Period = ( 1000 - 1 ); //自动重装寄存器    PWM频率:72M/100/1000=720Hz
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;    //向上计数模式
  TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;         //时钟分频,这里不涉及
#ifdef Time1
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
#endif
  TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure );        //配置参数
  TIM_ClearITPendingBit( TIMx, TIM_IT_Update );          //清除中断标示位

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1模式  OCx电平根据ARR与CCRx比较结果发生反转
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道1
#ifdef Time1
    TIM_OCInitStructure.TIM_OutputNState =;
#endif
  TIM_OCInitStructure.TIM_Pulse = CCR1_VAL;         //占空比800/1000
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;    //计数器值小于CCR值时输出高电平
#ifdef Time1
    TIM_OCInitStructure.TIM_OCNPolarity =;
    TIM_OCInitStructure.TIM_OCIdleState =;
    TIM_OCInitStructure.TIM_OCNIdleState =;
#endif

  TIM_OC1Init( TIMx, &TIM_OCInitStructure );
  TIM_OC1PreloadConfig( TIMx, TIM_OCPreload_Enable );         //使能CCR1预装载

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道2
  TIM_OCInitStructure.TIM_Pulse = CCR2_VAL;

  TIM_OC2Init( TIMx, &TIM_OCInitStructure );
  TIM_OC2PreloadConfig( TIMx, TIM_OCPreload_Enable );

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道3
  TIM_OCInitStructure.TIM_Pulse = CCR3_VAL;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC3Init( TIMx, &TIM_OCInitStructure );
  TIM_OC3PreloadConfig( TIMx, TIM_OCPreload_Enable );

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道4
  TIM_OCInitStructure.TIM_Pulse = CCR4_VAL;

  TIM_OC4Init( TIMx, &TIM_OCInitStructure );
  TIM_OC4PreloadConfig( TIMx, TIM_OCPreload_Enable );
  TIM_ARRPreloadConfig( TIMx, ENABLE );            //开启ARR预装载
  TIM_Cmd( TIMx, ENABLE );                //启动定时器
}

void Time_OUT_PWM_Init( void )
{
  Time_OUT_PWM_GPIO_Config( );
  Time_OUT_PWM_Config( );       //定时器配置
}

//------------------------------------------------------------------------------
//  3、输出比较模式

#include"stm32f10x.h"
#include"time.h"

static Time_NVIC_Config( void )
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 );
  NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );
  NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init( &NVIC_InitStructure );
}

static void Time_OUT_PWM_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( TIMx_OUT_GPIO_RCC, ENABLE );
  GPIO_InitStructure.GPIO_Pin = TIMx_OUT_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                    //复用推免式输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init( TIMx_OUT_Port, &GPIO_InitStructure );
}

static void Time_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE );
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                    //通用推免式输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init( GPIOD, &GPIO_InitStructure );
}

static void Time_OUT_PWM_Config( void )
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure;
  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE );        //开启时钟
  TIM_DeInit( TIM2 );

  TIM_TimeBaseInitStructure.TIM_Prescaler = ( 100 - 1 );      //时钟分频系数
  TIM_TimeBaseInitStructure.TIM_Period = ( 1000 - 1 ); //自动重装寄存器    PWM频率:72M/100/1000=720Hz
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;    //向上计数模式
  TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;         //时钟分频,这里不涉及
#ifdef Time1
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
#endif
  TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure );        //配置参数
  TIM_ClearITPendingBit( TIMx, TIM_IT_Update );          //清除中断标示位

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;  //ARR与CCRx相同时强制OCxREF为低
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道1
#ifdef Time1
    TIM_OCInitStructure.TIM_OutputNState =;
#endif
  TIM_OCInitStructure.TIM_Pulse = CCR1_VAL;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //OCx=!OCxREF    _High OCx=OCxREF
#ifdef Time1
    TIM_OCInitStructure.TIM_OCNPolarity =;
    TIM_OCInitStructure.TIM_OCIdleState =;
    TIM_OCInitStructure.TIM_OCNIdleState =;
#endif

  TIM_OC1Init( TIMx, &TIM_OCInitStructure );
  TIM_OC1PreloadConfig( TIMx, TIM_OCPreload_Enable );         //使能CCR1预装载
  TIM_ITConfig( TIMx, TIM_IT_CC1, ENABLE );            //使能OC1比较中断

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;    //ARR与CCRx相同时强制OCxREF为高
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道2
  TIM_OCInitStructure.TIM_Pulse = CCR2_VAL;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //OCx=!OCxREF    _High OCx=OCxREF     

  TIM_OC2Init( TIMx, &TIM_OCInitStructure );
  TIM_OC2PreloadConfig( TIMx, TIM_OCPreload_Enable );
  TIM_ITConfig( TIMx, TIM_IT_CC2, ENABLE );

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;    //ARR与CCRx比较结果对OCxREF无效
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道3
  TIM_OCInitStructure.TIM_Pulse = CCR3_VAL;
  TIM_ITConfig( TIMx, TIM_IT_CC3, ENABLE );

  TIM_OC3Init( TIMx, &TIM_OCInitStructure );
  TIM_OC3PreloadConfig( TIMx, TIM_OCPreload_Enable );

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ARR与CCRx比较结果对OCxREF采用PWM1模式
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;      //输出使能通道4
  TIM_OCInitStructure.TIM_Pulse = CCR4_VAL;
  //TIM_ITConfig(TIMx,TIM_IT_CC4,ENABLE);             //PWM就没有必要设置中断 当然也可以在中断里面改变占空比
  TIM_OC4Init( TIMx, &TIM_OCInitStructure );
  TIM_OC4PreloadConfig( TIMx, TIM_OCPreload_Enable );
  TIM_ARRPreloadConfig( TIMx, ENABLE );            //开启ARR预装载
  TIM_Cmd( TIMx, ENABLE );                //启动定时器
}

void Time_OUT_PWM_Init( void )
{
  Time_OUT_PWM_GPIO_Config( );
  Time_GPIO_Config( );
  Time_NVIC_Config( );
  Time_OUT_PWM_Config( );       //定时器配置
}

//------------------------------------------------------------------------------
// 4、PWMI模式
#include"stm32f10x.h"
#include"time.h"

static Time_NVIC_Config( void )
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 );
  NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );
  NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init( &NVIC_InitStructure );
}

static void Time_IN_PWM_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( TIMx_IN_GPIO_RCC, ENABLE );
  GPIO_InitStructure.GPIO_Pin = TIMx_IN_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;             //浮空输入模式
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init( TIMx_IN_Port, &GPIO_InitStructure );
}

static void Time_IN_PWM_Config( void )
{
#if 0
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
#endif
  TIM_ICInitTypeDef TIM_ICInitStructure;
  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE );        //开启时钟
  TIM_DeInit( TIM2 );
#if 0

  TIM_TimeBaseInitStructure.TIM_Prescaler =(100-1);      //时钟分频系数
  TIM_TimeBaseInitStructure.TIM_Period =(1000-1);//自动重装寄存器    PWM频率:72M/100/1000=720Hz
  TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up;//向上计数模式
  TIM_TimeBaseInitStructure.TIM_ClockDivision =0;//时钟分频,这里不涉及
#ifdef Time1
  TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
#endif
  TIM_TimeBaseInit(TIMx,&TIM_TimeBaseInitStructure);        //配置参数
  TIM_ClearITPendingBit(TIMx,TIM_IT_Update);//清除中断标示位
#endif

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿有效
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //TIM Input 1, 2, 3 or 4 is selected to be
                                                                  // connected to IC1, IC2, IC3 or IC4, respectively */
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;        //无预分频
  TIM_ICInitStructure.TIM_ICFilter = 0x0;       //无滤波
  TIM_ICInit( TIMx, &TIM_ICInitStructure );    //初始化PWM输入模式 //参见函数体与参考手册关于PWMI说明
  TIM_PWMIConfig( TIMx, &TIM_ICInitStructure );
  TIM_SelectInputTrigger( TIMx, TIM_TS_TI2FP2 ); //选择TI2FP2作为TIMx输入触发 一个信号来之后 启动定时器开始计数

  TIM_SelectSlaveMode( TIMx, TIM_SlaveMode_Reset ); //选择从模式控制器为复位模式,选中的TRGI上升沿重新初始化计数器
  //从模式控制器连接到TI1FP1 TI2FP2 只要两者有效为设置的电平,就会复位计数器 参见TIME结构图

  TIM_SelectMasterSlaveMode( TIMx, TIM_MasterSlaveMode_Enable ); //使能主从模式 ??????????????????

  TIM_Cmd( TIMx, ENABLE );

  TIM_ITConfig( TIMx, TIM_IT_CC2, ENABLE );
}

void Time_IN_PWM_Init( void )
{
  Time_IN_PWM_GPIO_Config( );
  Time_NVIC_Config( );
  Time_IN_PWM_Config( );       //定时器配置
}

//------------------------------------------------------------------------------
// 5、单脉冲模式

#include"stm32f10x.h"
#include"time.h"

static Time_NVIC_Config( void )
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 );
  NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );
  NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init( &NVIC_InitStructure );
}

static Time_SinglePluse_IN_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( TIMx_IN_GPIO_RCC, ENABLE );
  GPIO_InitStructure.GPIO_Pin = TIMx_IN_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;             //浮空输入模式
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init( TIMx_IN_Port, &GPIO_InitStructure );
}

static Time_SinglePluse_OUT_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( TIMx_OUT_GPIO_RCC, ENABLE );
  GPIO_InitStructure.GPIO_Pin = TIMx_OUT_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                     //推免复用输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init( TIMx_OUT_Port, &GPIO_InitStructure );
}

static void Time_SinglePluse_Config( void )
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  TIM_ICInitTypeDef TIM_ICInitStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure;
  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIMx, ENABLE );        //开启时钟
  TIM_DeInit( TIM2 );

  TIM_TimeBaseInitStructure.TIM_Prescaler = 1;              //时钟分频系数
  TIM_TimeBaseInitStructure.TIM_Period = 65535;           //自动重装寄存器
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;    //向上计数模式
  TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;         //时钟分频,这里不涉及
#ifdef Time1
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
#endif
  TIM_TimeBaseInit( TIMx, &TIM_TimeBaseInitStructure );        //配置参数
  TIM_ClearITPendingBit( TIMx, TIM_IT_Update );          //清除中断标示位

  TIM_ICStructInit( &TIM_ICInitStructure );

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;     //通道2为输入
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿有效
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //TIM Input 1, 2, 3 or 4 is selected to be
                                                                  // connected to IC1, IC2, IC3 or IC4, respectively*/
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;        //无预分频
  TIM_ICInitStructure.TIM_ICFilter = 0x0;       //无滤波
  TIM_ICInit( TIMx, &TIM_ICInitStructure );           //初始化输入模式
  TIM_SelectOnePulseMode( TIMx, TIM_OPMode_Single ); //选择单脉冲模式  这样 下一次更新时间是停止计数器
  TIM_SelectInputTrigger( TIMx, TIM_TS_TI2FP2 ); //选择TI2FP2作为TIMx输入触发 一个信号来之后 启动定时器开始计数
  TIM_SelectSlaveMode( TIMx, TIM_SlaveMode_Trigger ); //选择从模式控制器为触发模式 其连接到了TI2FP2  让从模式控制器启动计数器

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //输出模式选择为PWM模式2 用PWM2 向上计数时,CNT
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;     //使能通道
#ifdef Time1
    TIM_OCInitStructure.TIM_OutputNState =;
#endif
  TIM_OCInitStructure.TIM_Pulse = 12345;       //CCRx里面存放的值
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;   //OCREF与OC实际输出相同
#ifdef Time1
    TIM_OCInitStructure.TIM_OCNPolarity =;
    TIM_OCInitStructure.TIM_OCIdleState =;
    TIM_OCInitStructure.TIM_OCNIdleState =;
#endif
  TIM_OC1Init( TIMx, &TIM_OCInitStructure );           //使用通道1作为单脉冲的输出通道

  //TIM_Cmd(TIMx, ENABLE);                //使用TI2FP1来触发定时器,不需要软件启动定时器
}

void Time_SinglePluse_Init( void )
{
  Time_SinglePluse_IN_GPIO_Config( );   //配置time2的通道2为输入
  Time_SinglePluse_OUT_GPIO_Config( );  //配置time2的通道1为输出
  Time_NVIC_Config( );       //可以不用
  Time_SinglePluse_Config( );     //定时器配置
}

//------------------------------------------------------------------------------
// 6、定时器联级

#include"stm32f10x.h"
#include"time.h"

static Time_NVIC_Config( void )
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0000 );
  NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init( &NVIC_InitStructure );
}

static Time_Connect_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE );
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_6; //time2 ch1 pin.0 time3 cha1 pin.6
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         //推免复用输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init( GPIOA, &GPIO_InitStructure );
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;             //time4 ch1 pin.6
  GPIO_Init( GPIOB, &GPIO_InitStructure );
}

static void Time_Connect_Config( void )
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure;
  RCC_APB1PeriphClockCmd(
    RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE );
  TIM_DeInit( TIM2 );
  TIM_DeInit( TIM3 );
  TIM_DeInit( TIM4 );

  TIM_TimeBaseInitStructure.TIM_Prescaler = 0;              //时钟分频系数
  TIM_TimeBaseInitStructure.TIM_Period = 99;           //自动重装寄存器
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;    //向上计数模式
  TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;         //时钟分频,这里不涉及
#ifdef Time1                  //或者Time8
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
#endif
  TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStructure );        //配置参数
  TIM_ClearITPendingBit( TIM2, TIM_IT_Update );          //清除中断标示位

  TIM_TimeBaseInitStructure.TIM_Period = 5;
  TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStructure );
  TIM_ClearITPendingBit( TIM3, TIM_IT_Update );

  TIM_TimeBaseInitStructure.TIM_Period = 5;
  TIM_TimeBaseInit( TIM4, &TIM_TimeBaseInitStructure );
  TIM_ClearITPendingBit( TIM4, TIM_IT_Update );

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //输出模式选择为PWM模式2 用PWM2 向上计数时,CNT
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;     //使能通道
  TIM_OCInitStructure.TIM_Pulse = 49;           //CCRx里面存放的值
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;   //OCREF与OC实际输出相同
  TIM_OC1Init( TIM2, &TIM_OCInitStructure );           //使用通道1作为单脉冲的输出通道
  TIM_SelectMasterSlaveMode( TIM2, TIM_MasterSlaveMode_Enable );      //使能主从模式

  TIM_SelectOutputTrigger( TIM2, TIM_TRGOSource_Update );   //选择Time2的更新事件作为触发输出

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_Pulse = 3;

  TIM_OC1Init( TIM3, &TIM_OCInitStructure );
  TIM_OC1Init( TIM4, &TIM_OCInitStructure );

  TIM_SelectSlaveMode( TIM3, TIM_SlaveMode_Gated );         //从模式的输入触发选择为门控模式
  TIM_SelectInputTrigger( TIM3, TIM_TS_ITR1 ); //从模式触发源选择为内部触发1?????????????为什么是内部触发1 不是0 2 3
  //原因:ITR0 ITR1 ITR2 ITR3 对应主模式的TIM2 TIM3 TIM4 TIM5

  TIM_SelectSlaveMode( TIM4, TIM_SlaveMode_Gated );
  TIM_SelectInputTrigger( TIM4, TIM_TS_ITR1 );

  TIM_Cmd( TIM2, ENABLE );
  TIM_Cmd( TIM3, ENABLE );
  TIM_Cmd( TIM4, ENABLE );
}

void Time_Connect_Init( void )
{
  Time_Connect_GPIO_Config( );
  Time_NVIC_Config( );      //可以不用
  Time_Connect_Config( );     //定时器配置
}
时间: 2024-10-21 07:05:45

STM32 通用定时器的几种配置方式的相关文章

STM32通用定时器配置

一.STM32通用定时器原理 STM32 系列的CPU,有多达8个定时器,其中TIM1和TIM8是能够产生三对PWM互补输出的高级定时器,常用于三相电机的驱动,它们的时钟由APB2的输出产生.其它6个为普通定时器,时钟由APB1的输出产生. 下图是STM32参考手册上时钟分配图中,有关定时器时钟部分的截图: 从图中可以看出,定时器的时钟不是直接来自APB1或APB2,而是来自于输入为APB1或APB2的一个倍频器,图中的蓝色部分. 下面以通用定时器2的时钟说明这个倍频器的作用:当APB1的预分频

使用java配置定时任务的几种配置方式及示例

Spring定时器,主要有两种实现方式,包括Java Timer定时和Quartz定时器! 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 package com.land; import java.util.Date;import java.util.TimerTask; public class TimerTest extends  TimerTask  { @Override public void run() {  // TODO Auto-

stm32通用定时器中断问题

在使用stm32的通用定时器定时中断的时候,发现定时器在完成初始化配置后,定时器UIF位会立刻置位,导致在使能中断后,程序会立刻进入定时器中断. 如果设计代码时不希望定时器配置完成后,立刻进入中断,可以在定时器配置完成后,立刻清除UIF标志位(TIMx->SR &= 0xFFFE) ,再使能定时器更新中断.比如用库函数这么写: TIM_ClearITPendingBit(TIM4, TIM_IT_Update  ); TIM_ITConfig(TIM4,TIM_IT_Update,ENABL

STM32通用定时器库函数设置

通用定时器 STM32的通用定时器为:TIM2.TIM3.TIM4和TIM5 在使用通用定时器时利用库函数直接设置定时器如下: 1.使能定时器TIM_X的时钟:(X=2.3.4.5) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMX,ENABLE); 2.计算要定时的时间,根据定时时间来设定分频数和最大计数值(以向上计数为例子),其中计算关系如下: 系统时钟(一般为72MHZ) = 定时器分频数 * 计数值 假如分频数为7200,则定时器时钟为:72MHZ/7

IIS下PHP的三种配置方式比较

在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1.CGI(通用网关接口/Common Gateway Interface)一般是可执行程序,例如EXE文件,和WEB服务器各自占据着不同的进程,而且一般一个CGI程序只能处理一个用户请求.这样,当用户请求数量非常多时,会大量占用系统的资源,如内存.CPU时间等,造成效能低下. 2.ISAPI(Inte

java配置定时任务的几种配置方式及示例

使用java配置定时任务的几种配置方式及示例 (2010-08-21 13:16:10) 转载▼ 标签: spring 定时器 配置 it 分类: java Spring定时器,主要有两种实现方式,包括Java Timer定时和Quartz定时器! 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 package com.land; import java.util.Date;import java.util.TimerTask; public cla

MySQL Replication的主从同步的常用2种配置方式

因为实际的业务需求最近就需要部署一些mysql服务器,而在部署mysql服务器中在做主从同步时用的都是MySQL Replication的主从同步的方法,当然实现mysql主从同步的方法还有很多,这里就只说使用MySQL Replication的主从同步的功能,在实现mysql的主从同步的常用的2种配置方式,当然可以根据实际的生产环境选择不同的方式,在这里就简单的把2种配置方法配置my.cnf说一下,因为以前有写过mysql的主从同步方法,这里就不再赘述了,需要可以参看:http://jim12

STM32通用定时器实现LED灯闪烁

刚才看了一下STM32通用定时器的教程,其实和51的定时器使用差不多.只是因为32的时钟更复杂,可操控的寄存器更多,所以写的时候可能更复杂. 使用通用定时器中断的一般步骤:1.使能定时器时钟 这个需要看时钟树,使能对应的时钟就好了.这里我使用的是TIM3,所以使能APB1的时钟即可. 2.设置定时器的分频系数和重装载值 定时器中断的时间为:Tout = (arr+1)/[Tclk/(psc+1)] 其实这个定时器中断时间还是要具体分析,这里是这样,其他地方或许不是这样,具体还是要参考<STM32

c3p0三种配置方式(automaticTestTable)

c3p0的配置方式分为三种,分别是http://my.oschina.net/lyzg/blog/551331.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml文件 1.setters一个个地设置各个配置项这种方式最繁琐,形式一般是这样: Properties props = new Properties(); InputStream in = ConnectionManager.class.getRes