OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 7616|回复: 4

stm32 测车轮转速 除了配置定时器对外部脉冲计数 ,还需要配置其他定时器么来采集脉冲么?(请教大家)

[复制链接]

14

主题

98

帖子

0

精华

初级会员

Rank: 2

积分
174
金钱
174
注册时间
2014-3-5
在线时间
0 小时
发表于 2014-3-11 20:29:57 | 显示全部楼层 |阅读模式
硬件连接 :PE.7复用为TIM1.ETR, PD.13复用为TIM4-CH2(输出PWM),PE.7连接PD.13,对PWM计数。
问题如下:1想配置PE.8输出PWM,但是PWM和TIM1的周期一样,担心溢出所以改用TIM4对PE.7计数。实际可以用PE.8?
2.要完成测车轮转速,是不是还要开定时器,在T时间内对外部脉冲计数,从而得到转速?
3.能不能使用软件来定时采样脉冲,再计数处理?
 下面脉冲计数的代码:
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);  //TIM4初始化
u16 counter=0;

int main(void)
// u8 i=0; 
  /* System Clocks Configuration */
  RCC_Configuration();
        
  /* Configure the GPIO ports */
  GPIO_Configuration();

  /*Configure tim1 and tim4 */
  tim1_confi();
  tim4_confi();
  while (1)
  {
  counter=TIM_GetCounter(TIM1);  //只能读出最终值,采样的时钟要比输入信号快。
  }
}

void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}

void RCC_Configuration(void)
{
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();
   
  /* EnableGPIO Port, GPIO_LED and AFIO clock */
  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 = 72-1;
  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, ENABLE); //使能打开
 }

  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=2.35ms,34%
    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(TIM3, 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, ENABLE);
//TIM1_OC通道输出PWM(一定要加)
TIM_CtrlPWMOutputs(TIM4, ENABLE);
  
  }
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(uint8_t* file, uint32_t line)
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif





正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

12

主题

85

帖子

0

精华

初级会员

Rank: 2

积分
153
金钱
153
注册时间
2013-11-24
在线时间
0 小时
发表于 2014-3-11 22:12:26 | 显示全部楼层
     码盘吗? 那得两路信号,测车轮的速度方向       
学一天不会,我学两天;再不会,再两天·········
回复 支持 反对

使用道具 举报

14

主题

98

帖子

0

精华

初级会员

Rank: 2

积分
174
金钱
174
注册时间
2014-3-5
在线时间
0 小时
 楼主| 发表于 2014-3-12 08:43:31 | 显示全部楼层
回复【2楼】正点妹子:
---------------------------------
左右各一路。上面只列举了一路
回复 支持 反对

使用道具 举报

14

主题

98

帖子

0

精华

初级会员

Rank: 2

积分
174
金钱
174
注册时间
2014-3-5
在线时间
0 小时
 楼主| 发表于 2014-3-12 08:50:19 | 显示全部楼层
回复【2楼】正点妹子:
---------------------------------
用的光电管,应该测不出方向
回复 支持 反对

使用道具 举报

7

主题

21

帖子

0

精华

初级会员

Rank: 2

积分
69
金钱
69
注册时间
2014-5-7
在线时间
0 小时
发表于 2015-3-17 16:05:18 | 显示全部楼层
为什么需要两个定时器
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-24 21:06

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表