微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 通用定时器的几种配置方式

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

时间:11-25 来源:互联网 点击:
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(); //定时器配置
}

以上,结束。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top