OpenEdv-开源电子网

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

STM32F4 AB相编码模式计数读取失败

[复制链接]

2

主题

8

帖子

0

精华

新手上路

积分
27
金钱
27
注册时间
2017-5-20
在线时间
3 小时
发表于 2017-5-20 14:23:22 | 显示全部楼层 |阅读模式
2金钱
各位大神好:
以下是我STM32F4  TIM9 CH1 CH2  以编码模式读取AB相脉冲计数程序,但 读出来的 TIM9->CNT一直为0  ,请大家帮看看
感谢!

哪位大神帮我看一下 ,我AB相计数 TIM9->CNT 读到的数据一直为0啊。


// ////////////////////////////////////////////////////////////
void T9_Encoder_Configration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
/* TIM9 clock source enable */
  RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
//PE5-A相, PE6-B相     
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);//使能GPIOA时钟

  GPIO_StructInit(&GPIO_InitStructure);                     
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;    // 使用PE5,PE6
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;     // 输入模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOE,&GPIO_InitStructure);

  /* Timer configuration in Encoder mode */
  TIM_DeInit(TIM9);                   //TIM9复位
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  // 设定计数器分频系数为0,不分频
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
  TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);

// 设TIM9为编码模式3
  TIM_EncoderInterfaceConfig(TIM9, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  TIM_ICStructInit(&TIM_ICInitStructure);    // 初始化为默认值
  TIM_ICInitStructure.TIM_ICFilter = 6;
  TIM_ICInit(TIM9, &TIM_ICInitStructure);
  TIM_ClearFlag(TIM9, TIM_FLAG_Update);//清除TIM1的更新标志位
TIM_ITConfig(TIM9, TIM_IT_Update, ENABLE);
  //Reset counter
  TIM9->CNT = 0;
  TIM_Cmd(TIM9, ENABLE);
}
//  TIM9->CNT的内容就是编码器的计数脉冲。



最佳答案

查看完整内容[请看2#楼]

OK了 void T5_Encoder_Configration(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* TIM5 clock source enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //PA0-A相, PA1-B相 GPIO_StructInit(&GPIO_InitStructure); ...
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

0

主题

125

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2728
金钱
2728
注册时间
2015-10-29
在线时间
428 小时
发表于 2017-5-22 11:15:35 | 显示全部楼层
F4的TIM9没有编码器功能吧
回复

使用道具 举报

2

主题

8

帖子

0

精华

新手上路

积分
27
金钱
27
注册时间
2017-5-20
在线时间
3 小时
 楼主| 发表于 2017-5-20 14:23:23 | 显示全部楼层
OK了



void T5_Encoder_Configration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
/* TIM5 clock source enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
//PA0-A相, PA1-B相                                                
  GPIO_StructInit(&GPIO_InitStructure);                     
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;    // 使用PA0,PA1
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;         //推挽输出
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                 //上拉

  GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5);        // PA0
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5);        // PA1


  /* Timer configuration in Encoder mode */
  TIM_DeInit(TIM5);                   //TIM5复位
  
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  // No prescaling
  TIM_TimeBaseStructure.TIM_Period = 10000;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
  TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

// 设TIM5为编码模式3
  TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  TIM_ICStructInit(&TIM_ICInitStructure);    // 初始化为默认值
  TIM_ICInitStructure.TIM_ICFilter = 6;
  TIM_ICInit(TIM5, &TIM_ICInitStructure);
   
  //Reset counter
  TIM5->CNT = 0;
  TIM_Cmd(TIM5, ENABLE);
}
////////////  TIM5->CNT的内容就是编码器的计数脉冲。

回复

使用道具 举报

0

主题

16

帖子

0

精华

初级会员

Rank: 2

积分
189
金钱
189
注册时间
2017-3-17
在线时间
47 小时
发表于 2017-5-21 16:31:17 | 显示全部楼层
你的TIM9是作为定时器功能使用的, 所以PE5, PE6不应该初始化为普通输入模式。
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;         //推挽输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                 //上拉
  GPIO_Init(GPIOE, &GPIO_InitStructure);
       
        GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_TIM9);        // PE5
        GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_TIM9);        // PE6

另外, 你用到了TIM9的Update中断, 需要初始化NVIC和编写中断响应函数.
回复

使用道具 举报

2

主题

8

帖子

0

精华

新手上路

积分
27
金钱
27
注册时间
2017-5-20
在线时间
3 小时
 楼主| 发表于 2017-5-22 08:54:07 | 显示全部楼层
houdw2006 发表于 2017-5-21 16:31
你的TIM9是作为定时器功能使用的, 所以PE5, PE6不应该初始化为普通输入模式。
        GPIO_InitStructure.GPIO_P ...

哦,我试一下,谢谢
回复

使用道具 举报

2

主题

8

帖子

0

精华

新手上路

积分
27
金钱
27
注册时间
2017-5-20
在线时间
3 小时
 楼主| 发表于 2017-5-23 17:08:08 | 显示全部楼层
void T5_Encoder_Configration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
/* TIM5 clock source enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
//PA0-A相, PA1-B相                                                
  GPIO_StructInit(&GPIO_InitStructure);                     
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;    // 使用PA0,PA1
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA,&GPIO_InitStructure);

  /* Timer configuration in Encoder mode */
  TIM_DeInit(TIM5);                   //TIM5复位
  
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  // No prescaling
  TIM_TimeBaseStructure.TIM_Period = 10000;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
  TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

// 设TIM5为编码模式3
  TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  TIM_ICStructInit(&TIM_ICInitStructure);    // 初始化为默认值
  TIM_ICInitStructure.TIM_ICFilter = 6;
  TIM_ICInit(TIM5, &TIM_ICInitStructure);
   
  //Reset counter
  TIM5->CNT = 0;
  TIM_Cmd(TIM5, ENABLE);
}
////////////  TIM5->CNT的内容就是编码器的计数脉冲。




现在改成TIM5 AB相  TIM5->CNT  读出来还是为0,郁闷啊。
回复

使用道具 举报

2

主题

8

帖子

0

精华

新手上路

积分
27
金钱
27
注册时间
2017-5-20
在线时间
3 小时
 楼主| 发表于 2017-5-24 11:55:13 | 显示全部楼层
回复

使用道具 举报

0

主题

4

帖子

0

精华

新手入门

积分
17
金钱
17
注册时间
2017-5-29
在线时间
3 小时
发表于 2017-5-30 20:35:31 | 显示全部楼层
帮顶!d=====( ̄▽ ̄*)b
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-7-18 08:54

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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