STM32 外部脉冲计数值不正确,求解

2019-07-19 21:59发布

使用的TIM4-CH2(PD.13)产生10K的PWM波,接入TIM1-ETR(PE.7),1ms后读取计数器的值,读的值与实际不符合,实际波形个数如图2所示



源码如下:
EXTI_InitTypeDef EXTI_InitStructure; TIM_OCInitTypeDef  TIM_OCInitStructure; void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void Delay(__IO uint32_t nCount); void tim1_confi(void); void tim4_confi(void); u16 Count_pulse(u16 c);   u16 counter=0;
int main(void) {  // u8 i=0;    /* System Clocks Configuration */   RCC_Configuration();           /* NVIC configuration */   NVIC_Configuration();        /* Configure the GPIO ports */   GPIO_Configuration();   /*Configure tim1*/   tim1_confi();   tim4_confi();
  while (1)   {
Delay(10000);  //1ms TIM_Cmd(TIM1, ENABLE); TIM_Cmd(TIM4, ENABLE); Delay(10000);  //1ms TIM_Cmd(TIM4, DISABLE); TIM_Cmd(TIM1, DISABLE); counter=TIM_GetCounter(TIM1); //      for(i = 0; i < 4; i ++)  //  { //  GPIO_SetBits(GPIOE, GPIO_Pin_8); //  Delay(100); //15US //  GPIO_ResetBits(GPIOE, GPIO_Pin_8); //  Delay(100); //  } //  //counter=TIM_GetCounter(TIM1); //  counter=TIM1->CNT;   } }
void Delay(__IO uint32_t nCount)   //0.1us {   for(; nCount != 0; nCount--); } void RCC_Configuration(void) {     SystemInit();   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE  | RCC_APB2Periph_AFIO, ENABLE);   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);  //使能定时器TIM1时钟 }
void GPIO_Configuration(void) {  GPIO_InitTypeDef  GPIO_InitStructure;    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //使能PE端口时钟  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);  //使能定时器TIM1时钟  GPIO_PinRemapConfig (GPIO_FullRemap_TIM1 ,ENABLE );  //重映射  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // 端口配置  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //输入  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz  GPIO_Init(GPIOE, &GPIO_InitStructure); //根据设定参数初始化GPIOE.7
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PE.8 端口配置  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz  GPIO_Init(GPIOE, &GPIO_InitStructure); //根据设定参数初始化GPIOE.8
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PD端口时钟   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);  //使能定时器TIM4时钟   GPIO_PinRemapConfig (GPIO_Remap_TIM4  ,ENABLE );  //重映射   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13 ;   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    GPIO_Init(GPIOD, &GPIO_InitStructure);  }
 void tim1_confi(void)  //TIM1初始化  {   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;     TIM_DeInit( TIM1 ) ;   /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Period = 0XFFFF; // TIM_TimeBaseStructure.TIM_Prescaler = 0;   TIM_TimeBaseStructure.TIM_ClockDivision = 0;   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);  /* TIM1-ETR Configuration */   TIM_ETRClockMode2Config (TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0); TIM_SetCounter( TIM1,0) ; //从零计数 TIM_Cmd(TIM1, DISABLE); //使能打开  }
  void tim4_confi(void)  //TIM4初始化   {  // TIM4-CH2基本计数器设置(设置PWM频率) // 频率=TIM1_CLK/(ARR+1)      TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;     TIM_DeInit( TIM4 ) ;     TIM_BaseInitStructure.TIM_Period = 100;  // 确定T=0.1ms     TIM_BaseInitStructure.TIM_Prescaler = 72-1;       TIM_BaseInitStructure.TIM_ClockDivision = 0;     TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;     TIM_BaseInitStructure.TIM_RepetitionCounter = 0;     TIM_TimeBaseInit(TIM4, &TIM_BaseInitStructure);  //启用ARR的影子寄存器(直到产生更新事件才更改设置) TIM_ARRPreloadConfig(TIM4, ENABLE); //TIM4_OC2模块设置 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_Pulse = 50; TIM_OC2Init(TIM4, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //TIM3开启 TIM_Cmd(TIM4, DISABLE); //TIM1_OC通道输出PWM(一定要加) TIM_CtrlPWMOutputs(TIM4, ENABLE);      } u16 Count_pulse(u16 c)  { TIM_Cmd(TIM1, ENABLE); TIM_Cmd(TIM4, ENABLE); Delay(1000);  //1ms TIM_Cmd(TIM1, DISABLE); TIM_Cmd(TIM4, DISABLE);    counter=TIM_GetCounter(TIM1);  //只能读出最终值,采样的时钟要比输入信号快。    c= counter;   return c; } #ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line) { 
  while (1)   {   } } #endif
使用红 {MOD}部分产生20us方波,然后连接PE.7作为输入,计数结果正常。。请问是不是对输入脉冲的频率有要求?



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
xiaoyan
1楼-- · 2019-07-20 02:28
 精彩回答 2  元偷偷看……
fangxiaoyanzhou
2楼-- · 2019-07-20 05:21
回复【2楼】xiaoyan:
---------------------------------
楼主,用定时器输出的方波周期为0.1ms,使用延时产生的方波周期为20us,输入方波的频率已经降低了。。
fangxiaoyanzhou
3楼-- · 2019-07-20 05:49
回复【2楼】xiaoyan:
---------------------------------
http://www.amobbs.com/thread-4146690-1-1.html,参照这个,应该是正常的。。

一周热门 更多>