stm32f407编码器测速,CNT值一直不变,是什么原因?

2019-07-20 03:36发布

我的是stm32f407的板子,编码器测速的时候,我输出的cnt一直不变,而且和我在中断里的那个CNT值相同。  
我这个程序是参考后,自己改的。用TIM3,TIM4,TIM5,来测速,TIM2,作为中断处理计算速度。
#include "stm32f4xx.h"
#include "encoder.h"
#include "./usart/bsp_debug_usart.h"

float cnt1=0,cnt2=0,cnt3=0;
float speed1=0,speed2=0,speed3=0;
TIM_ICInitTypeDef  TIM_ICInitStructure;
void TIM4_Encoder_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;   
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM5);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);                           
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;         
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//TIM3,TIM4,TIM5配置
  TIM_DeInit(TIM3);
TIM_DeInit(TIM4);
TIM_DeInit(TIM5);
TIM_TimeBaseStructure.TIM_Period =arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);              
  TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising ,TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising ,TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising ,TIM_ICPolarity_Rising);

TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInit(TIM5, &TIM_ICInitStructure);

TIM_ClearFlag(TIM3,TIM_FLAG_Update);
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

TIM_ClearFlag(TIM4,TIM_FLAG_Update);
  TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);

TIM_ClearFlag(TIM5,TIM_FLAG_Update);
  TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);

TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);   
  TIM_Cmd(TIM5, ENABLE);
TIM_SetCounter(TIM3,0);
TIM_SetCounter(TIM4,0);
TIM_SetCounter(TIM5,0);
}
void TIM2_Int_Init(void)//TIM2,中断配置
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

TIM_TimeBaseStructure.TIM_Period = 9999;//重装载值
TIM_TimeBaseStructure.TIM_Prescaler =8399; //预分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE );

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);   
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!= RESET)
{

    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除标志位
   cnt1  = (float)((uint16_t)0x7fff)-(float)((uint16_t)(TIM3->CNT)) ;
    printf(" cnt1=%f   ",cnt1);
    TIM3->CNT = 0;//就是这儿,如果我把CNT设置为n,printf打印的就是0x7fff-n的值??????
    speed1=cnt1/16/30;

    cnt2 = (float)((uint16_t)0x7fff) - (float)((uint16_t)(TIM4->CNT)) ;
    TIM4->CNT = 0x7fff;
    speed2=cnt2/16/30;
    cnt3  = (float)((uint16_t)0x7fff) - (float)((uint16_t)(TIM5->CNT)) ;
    TIM5->CNT = 0x7fff;
    speed3=cnt3/16/30;
}
}


大佬们帮忙看一下啊!!!!谢谢了!

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
正点原子
1楼-- · 2019-07-20 08:48
说明根本没配置对。
Jerry12315
2楼-- · 2019-07-20 12:43
 精彩回答 2  元偷偷看……
wx981260874
3楼-- · 2019-07-20 13:46
Jerry12315 发表于 2018-8-16 10:11
晓得了,我那个GPIO那儿配置错了,谢谢了

请问是哪里出错了,我也是这种情况
Holybird
4楼-- · 2019-07-20 14:16
是不是因为GPIO没配置成 复用输入功能
heyangfengyue
5楼-- · 2019-07-20 14:50
 精彩回答 2  元偷偷看……

一周热门 更多>