OpenEdv-开源电子网

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

各位大侠,有没有用过stm32f030 的编码器模式,为啥不计数呢

[复制链接]

3

主题

39

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
311
金钱
311
注册时间
2013-4-5
在线时间
82 小时
发表于 2019-9-24 10:29:00 | 显示全部楼层 |阅读模式
10金钱
本帖最后由 dujq 于 2019-9-24 14:53 编辑

已解决,经验分享,有时候遇到解决不了的难题,不妨出去溜达一圈,说不定回来就有惊喜呢!

用过stm8和stm32f1得编码器,感觉很好用啊,但是换成030c8t6就不行了死活不计数,

void Knob_Init(void)
{

GPIO_InitTypeDef GPIO_InitStructure;  
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;

    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;

//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //设置引脚模式为上拉输入模式  
GPIO_Init(GPIOA,&GPIO_InitStructure);

TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
//TIM_ARRPreloadConfig(TIM1, ENABLE);//
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);




TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);  
TIM_ICInitStructure.TIM_ICFilter = 6;//ICx_FILTER;
//TIM_ICInit(TIM1, &TIM_ICInitStructure);
//TIM_ClearFlag(TIM1, TIM_FLAG_Update);
//TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

  


//TIM_SetCounter(TIM1, 16);




TIM_Cmd(TIM1, ENABLE);


}

//直接调用以下函数获得计数值
//n_Counter1 = TIM_GetCounter(TIM1);

最佳答案

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

void Knob_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_2); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_2); GPI ...
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

3

主题

39

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
311
金钱
311
注册时间
2013-4-5
在线时间
82 小时
 楼主| 发表于 2019-9-24 10:29:01 | 显示全部楼层
void Knob_Init(void)
{

GPIO_InitTypeDef GPIO_InitStructure;  
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;

    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
       
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_2);
          GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_2);
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
   
     GPIO_Init(GPIOA,&GPIO_InitStructure);

       
        TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 2400;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
//TIM_ARRPreloadConfig(TIM1, ENABLE);//
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);




TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);  
TIM_ICInitStructure.TIM_ICFilter = 6;//ICx_FILTER;
//TIM_ICInit(TIM1, &TIM_ICInitStructure);
//TIM_ClearFlag(TIM1, TIM_FLAG_Update);
//TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

  


TIM_SetCounter(TIM1, 350*4);




TIM_Cmd(TIM1, ENABLE);


}
回复

使用道具 举报

109

主题

5564

帖子

0

精华

资深版主

Rank: 8Rank: 8

积分
10571
金钱
10571
注册时间
2017-2-18
在线时间
1914 小时
发表于 2019-9-24 12:24:43 | 显示全部楼层
帮顶~~
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-6-8 17:38

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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