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

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

时间:11-25 来源:互联网 点击:

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);
}

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

网站地图

Top