stm32 PWM输入问题?
时间:10-02
整理:3721RD
点击:
程序为求出输入波形的占空比而设计,所用资源为time3的1通道和2通道
当我在管脚输入1个周期为2.2ms,占空为0.5ms左右的波形时,捕获到的数据却为198和78
原理来算应该为 时钟分频为 :71
计数频率为 :72M/(72+1) = 1us
每计数一次时间为:1us
2.2ms计数应该为:2200
0.5ms计数应该为:500
不知道我推理的对吗?我写的代码有错误吗?请求帮助?
//初始化捕获
void Study_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ClocksTypeDef RCC_Clocks;
RCC_APB1PeriphClockCmd(RF_TIM_RCC ,ENABLE); //////////时钟选始能
NVIC_InitStructure.NVIC_IRQChannel = RF_TIM_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); ////中断配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RF_GPIO_RCC ,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin = RF_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RF_GPIO, &GPIO_InitStructure); //////输入管脚配置
/* RF_TIM configuration: PWM Input mode ------------------------
The external signal is connected to RF_TIM CH2 pin (PA.01),
The Rising edge is used as active edge,
The RF_TIM CCR2 is used to compute the frequency value
The RF_TIM CCR1 is used to compute the duty cycle value
------------------------------------------------------------ */
RCC_GetClocksFreq(&RCC_Clocks);/////////////////////系统时钟为72M
TIM_TimeBaseInitTypeDef tim_base;
tim_base.TIM_Prescaler = (RCC_Clocks.PCLK2_Frequency/1000000 - 1);////分频系数 71
tim_base.TIM_CounterMode= TIM_CounterMode_Up;
tim_base.TIM_Period = 0xffff; ////ARR 自动装载寄存器
tim_base.TIM_ClockDivision = 0; ////CR1中CDK[1:0]时钟分频因子为0
tim_base.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(RF_TIM, &tim_base);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
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 = 0x3;
TIM_PWMIConfig(RF_TIM, &TIM_ICInitStructure);
/* Select the RF_TIM Input Trigger: TI1FP1 */
TIM_SelectInputTrigger(RF_TIM, TIM_TS_TI1FP1);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(RF_TIM, TIM_SlaveMode_Reset);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(RF_TIM, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(RF_TIM, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(RF_TIM, TIM_IT_CC1, ENABLE);
}
当我在管脚输入1个周期为2.2ms,占空为0.5ms左右的波形时,捕获到的数据却为198和78
原理来算应该为 时钟分频为 :71
计数频率为 :72M/(72+1) = 1us
每计数一次时间为:1us
2.2ms计数应该为:2200
0.5ms计数应该为:500
不知道我推理的对吗?我写的代码有错误吗?请求帮助?
//初始化捕获
void Study_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ClocksTypeDef RCC_Clocks;
RCC_APB1PeriphClockCmd(RF_TIM_RCC ,ENABLE); //////////时钟选始能
NVIC_InitStructure.NVIC_IRQChannel = RF_TIM_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); ////中断配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RF_GPIO_RCC ,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin = RF_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RF_GPIO, &GPIO_InitStructure); //////输入管脚配置
/* RF_TIM configuration: PWM Input mode ------------------------
The external signal is connected to RF_TIM CH2 pin (PA.01),
The Rising edge is used as active edge,
The RF_TIM CCR2 is used to compute the frequency value
The RF_TIM CCR1 is used to compute the duty cycle value
------------------------------------------------------------ */
RCC_GetClocksFreq(&RCC_Clocks);/////////////////////系统时钟为72M
TIM_TimeBaseInitTypeDef tim_base;
tim_base.TIM_Prescaler = (RCC_Clocks.PCLK2_Frequency/1000000 - 1);////分频系数 71
tim_base.TIM_CounterMode= TIM_CounterMode_Up;
tim_base.TIM_Period = 0xffff; ////ARR 自动装载寄存器
tim_base.TIM_ClockDivision = 0; ////CR1中CDK[1:0]时钟分频因子为0
tim_base.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(RF_TIM, &tim_base);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
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 = 0x3;
TIM_PWMIConfig(RF_TIM, &TIM_ICInitStructure);
/* Select the RF_TIM Input Trigger: TI1FP1 */
TIM_SelectInputTrigger(RF_TIM, TIM_TS_TI1FP1);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(RF_TIM, TIM_SlaveMode_Reset);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(RF_TIM, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(RF_TIM, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(RF_TIM, TIM_IT_CC1, ENABLE);
}