|
- /*************************************************************
- Function : OnePulse_TIM4_Init
- Description: 定时器4配置
- Input : none
- return : none
- *************************************************************/
- void OnePulse_TIM4_Init(u16 arr,u16 psc)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- //GPIO D12,D13初始化设置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//普通复用模式
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
- GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIO
-
- GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4); //PA0复用位定时器4
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIOA0
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//复用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //下拉
- GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化PD13
-
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); ///使能TIM4时钟
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOD时钟
- /*---------------------------------------------------------
- TIM4的定时时钟 84M
- IM4的CH1配置成PWM2输出模式,TIM4的CH2配置成输入捕获模式,
- PWM2模式增计数下,计数值小于比较值为无效电平即低电平,
- TIM2 CH2检测到一个上升沿后,经过一定的延时时间,输出一个脉冲,
- ---------------------------------------------------------*/
-
-
- TIM_TimeBaseStructure.TIM_Period = arr;//定时器周期值
- TIM_TimeBaseStructure.TIM_Prescaler =psc;//预分频
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//增计数模式
-
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);//初始化定时器时基
-
-
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//TIM4 CH1配置成PWM2输出模式
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出使能
- TIM_OCInitStructure.TIM_Pulse =3000;//设置跳变值
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高电平
-
- TIM_OC1Init(TIM4, &TIM_OCInitStructure);
-
- //TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable); //使能TIM4在CCR1上的预装载寄存器
-
-
- TIM_ICStructInit(&TIM_ICInitStructure);//初始化输入捕获结构体
-
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//TIM4 CH2 配置为输入捕获模式
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
- TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//管教与寄存器直接对应
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//不与分频
- TIM_ICInitStructure.TIM_ICFilter = 0;//不滤波
-
- TIM_ICInit(TIM4, &TIM_ICInitStructure);//初始化TIM4 CH2
-
-
- TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);//选择定时器为单脉冲模式
-
- TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);//选择触发源为IC2
-
- TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);//选择定时器从模式为上升沿触发
-
- //TIM_Cmd(TIM4, ENABLE); //开启时钟
- }
- //TIM14 PWM部分初始化
- //PWM输出初始化
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM3_PWM_Init(u32 arr,u32 psc)
- {
- //此部分需手动修改IO口设置
-
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM14时钟使能
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //使能PORTC时钟
-
- GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM3); //GPIOF9复用为定时器14
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //GPIOF9
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- //速度100MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PF9
-
- TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
- TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
-
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//初始化定时器14
-
-
- //初始化TIM14 Channel1 PWM模式
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- //TIM_OCInitStructure.TIM_Pulse =500;//设置跳变值
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低
- TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器
-
- TIM_ARRPreloadConfig(TIM3,ENABLE);//ARPE使能
-
- TIM_Cmd(TIM3, ENABLE); //使能TIM14
- }
复制代码 打算用STM32F4做一个单脉冲实验,用TIM3输出PWM波作为触发源,TIM4的PB7设置为输入引脚,PB6设置为单脉冲输出引脚,我是PB6接灯或蜂鸣器直接观察效果,但是一直没有实现单脉冲的效果,不知道是不是配置错误,检查了很久也没发现问题所在,故前来请教大神们~
|
|