谁有 STM32正交编码器的程序啊 求给发一份 。还有 tiM_SetCounter(TIM2,10); TIM_SetCompare1(TIM2, 5);这两个函数有有什么区别? 再用正交编码器时如何取值啊?
1
|
|
|
|
这两个函数,从字面上看应该是TIM_SetCounter(TIM2,10); 设置计数寄存器值为10、 TIM_SetCompare1(TIM2, 5);设置比较寄存器值为5吧 ![](https://staticbbs.elecfans.com/static/image/smiley/fashaoyou/20.gif) ;
我木有正交编码器
|
|
|
|
|
STM32正交编码器程序
用TIM3的CH1和CH2做编码器的输入
- 编码器输入配置
- #include "encoder.h"
- void TIM3_Mode_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- //TIM_OCInitTypeDef TIM_OCInitStructure;
-
- /*----------------------------------------------------------------*/
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
-
- GPIO_StructInit(&GPIO_InitStructure);
- /* Configure PA.06,07 as encoder input */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /*----------------------------------------------------------------*/
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3
- TIM_DeInit(TIM3);
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
-
- TIM_TimeBaseStructure.TIM_Period =0xffff; //
- TIM_TimeBaseStructure.TIM_Prescaler =0; //设置预分频:
- TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ; //设置时钟分频系数:不分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
- /*初始化TIM2定时器 */
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
-
- /*-----------------------------------------------------------------*/
- //编码配置 编码模式
- TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,
- TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising上升沿捕获
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 6; //比较滤波器
- TIM_ICInit(TIM3, &TIM_ICInitStructure);
-
- //TIM_ARRPreloadConfig(TIM3, ENABLE);
- // Clear all pending interrupts
- TIM_ClearFlag(TIM3, TIM_FLAG_Update);
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能中断
- //Reset counter
- TIM3->CNT =0;
-
-
- TIM_Cmd(TIM3, ENABLE); //使能定时器3
- }
- void TIM_Init(void)
- {
- TIM3_Mode_Config();
- }
复制代码
最后附上全部程序:
|
|
|
|
|
你设置了编码器接口模式之后,计数器的内容始终指示着编码器的位置。读计数器里值就行了吧。
评分
-
查看全部评分
|
|
|
|
|
感谢楼主分享
|
|
|
|
|
代码错误.。。。。。。。。。。。。。。。。。。。。。。。。。。。。
|
|
|
|
|
顶顶~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
感谢楼主分享 ![](https://staticbbs.elecfans.com/static/image/smiley/default/lol.gif) ,最近正在弄这个
|
|
|
|
|
fhafshafhgsfahgfsghafshgafghsfhagfshaf
|
|
|
|
|