初级会员
- 积分
- 106
- 金钱
- 106
- 注册时间
- 2021-3-14
- 在线时间
- 20 小时
|
10金钱
目前已经排除硬件连接的问题,原因如下
(1)本人连接编码器的情况下转动轮子,mpu6050 stm32的电源指示灯都会亮!说明有脉冲信号!
(2)用万用表打过,确定了编码器中间的黄线跟绿线是与A0 A1 B6 B7四个角连通。
(3)如果是硬件问题则两个电机都坏了的概率太小。
(4)用C13指示灯,在程序中设置如果TIM2或TIM4进入中断,则灯亮,但是灯没亮!,说明无法进入定时器中断!
(5)OLED 显示两个轮子SPEED(速度)为0,且两定时器计数一直为0。
综上,本人怀疑是定时器初始化程序的问题 ,特附如下,诚邀各位大佬,帮帮小白,真的因为这个问题被折磨了好几天!
如果不是初始化的问题,请各位根据工程经验,提提想法。
#include "encoder.h"
#include "led.h"
void Encoder_TIM2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//¿aÆôê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;//3õê¼»ˉGPIO--PA0¡¢PA1
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0 |GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);//3õê¼»ˉ¶¨ê±Æ÷
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//2»·ÖÆμ
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//ÏòéϼÆêy
TIM_TimeBaseInitStruct.TIM_Period=65535;//ÖØ×°3õÖμ
TIM_TimeBaseInitStruct.TIM_Prescaler=0;//·ÖÆμÏμêy
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct); //ÅäÖö¨ê±Æ÷
TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//ÅäÖñàÂëÆ÷ģ꽣¨¶¨ê±Æ÷2£¬Ä£ê½3£¬éÏéyÑØ£¬éÏéyÑØ£©
TIM_ICStructInit(&TIM_ICInitStruct);//3õê¼»ˉêäèë2¶»ñ
TIM_ICInitStruct.TIM_ICFilter=10;//ÂË2¨Æ÷
TIM_ICInit(TIM2,&TIM_ICInitStruct);//êäèë2¶»ñÅäÖÃ
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//ÅäÖÃòç3ö¸üDÂÖD¶Ï±ê־λ
TIM_SetCounter(TIM2,0);//Çåá㶨ê±Æ÷¼ÆêyÖμ
TIM_Cmd(TIM2,ENABLE);//¿aÆô¶¨ê±Æ÷
}
void Encoder_TIM4_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6 |GPIO_Pin_7;
GPIO_Init(GPIOB,&GPIO_InitStruct);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=65535;
TIM_TimeBaseInitStruct.TIM_Prescaler=0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStruct);
TIM_ICInitStruct.TIM_ICFilter=10;
TIM_ICInit(TIM4,&TIM_ICInitStruct);
TIM_ClearFlag(TIM4,TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_SetCounter(TIM4,0);
TIM_Cmd(TIM4,ENABLE);
}
/**********************
±àÂëÆ÷Ëù¶è¶áè¡oˉêy
èë¿ú2Îêy£o¶¨ê±Æ÷
**********************/
int Read_Speed(int TIMx)
{
int value_1;
switch(TIMx)
{
case 2:value_1=(short)TIM_GetCounter(TIM2);TIM_SetCounter(TIM2,0);break;//IFêǶ¨ê±Æ÷2£¬1.2é¼ˉ±àÂëÆ÷μļÆêyÖμ2¢±£′æ¡£2.½«¶¨ê±Æ÷μļÆêyÖμÇåáã¡£
case 4:value_1=(short)TIM_GetCounter(TIM4);TIM_SetCounter(TIM4,0);break;
default:value_1=0;
}
return value_1;
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=0)//èç1ûÖD¶Ï±ê־λÎa1£¬Ôò½«′ËλÇåáã
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
// LED0=0;
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=0)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
LED0=0;
}
}
|
|