中级会员
 
- 积分
- 205
- 金钱
- 205
- 注册时间
- 2019-3-31
- 在线时间
- 31 小时
|
3金钱
我用TIM4开编码器模式,但是读不出计数值(应该是计数值没变),编码器经测试是好用的,不用接上拉电阻(应该是电压输出),我这个系统上还用了另一个编码器,那一个就好用,而且这个编码器的代码是从好用的那个那里移植过来的,就改了一下输入方式,而这个就一直读不出数,但是之前正确过一次所以我现在也不清楚是程序的问题还是电路连接的问题。下面是程序:(1的程序是好用的那个编码器的初始化函数)
#include "encoder.h"
void encoder1_init(TIM_TypeDef*TIMx,u16 pr)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
if(TIMx==TIM3)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if(TIMx==TIM4)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
if(TIMx==TIM5)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
TIMx->ARR = 36000;
TIMx->PSC = 0;
TIMx->CR1 &=~(3<<8);
TIMx->CR1 &=~(3<<5);
TIM_EncoderInterfaceConfig(TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);
TIM_SetCounter(TIMx,18000);
TIM_ARRPreloadConfig(TIMx,ENABLE);
TIM_Cmd(TIMx, ENABLE);
}
void encoder2_init(TIM_TypeDef*TIMx,u16 pr)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
if(TIMx==TIM3)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if(TIMx==TIM4)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
if(TIMx==TIM5)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
TIMx->ARR = 36000;
TIMx->PSC = 0;
TIMx->CR1 &=~(3<<8);
TIMx->CR1 &=~(3<<5);
TIM_EncoderInterfaceConfig(TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);
TIM_SetCounter(TIMx,18000);
TIM_ARRPreloadConfig(TIMx,ENABLE);
TIM_Cmd(TIMx, ENABLE);
}
|
|