Timer stm32如何使用定时器进行脉冲上/下计数

Timer stm32如何使用定时器进行脉冲上/下计数,timer,count,stm32,pulse,updown,Timer,Count,Stm32,Pulse,Updown,我需要为我的个人项目计数脉冲和计时器的方向。 用这个代码,我只能计算一个方向。 欢迎对正确代码提出任何建议(此代码为预测试) 脉冲计数至PA_9和方向输入至PA_8 #include "mbed.h" #include "stm32f4xx.h" #include "stm32f4xx_hal_tim_ex.h" TIM_HandleTypeDef timer; TIM_Base_InitTypeDef inizializza; TIM_IC_InitTypeDef st

我需要为我的个人项目计数脉冲和计时器的方向。 用这个代码,我只能计算一个方向。 欢迎对正确代码提出任何建议(此代码为预测试)

脉冲计数至
PA_9
和方向输入至
PA_8

#include "mbed.h"
#include "stm32f4xx.h"
#include "stm32f4xx_hal_tim_ex.h"


TIM_HandleTypeDef timer;          
TIM_Base_InitTypeDef inizializza;
TIM_IC_InitTypeDef startclock;
TIM_ClockConfigTypeDef ClockConfig;
TIM_SlaveConfigTypeDef sSlaveConfigure;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_Encoder_InitTypeDef hEncoder1;

int main(){
     GPIO_InitTypeDef GPIO_InitStruct;
        __TIM1_CLK_ENABLE();
        __GPIOA_CLK_ENABLE();
        GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLDOWN;
        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    timer.Instance = TIM1;
    timer.Init.Period = 0xffff;
    timer.Init.Prescaler = 0;
    timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    timer.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3;
    timer.Init.RepetitionCounter = 0;

    HAL_TIM_Base_Init(&timer);


  sSlaveConfigure.SlaveMode = TIM_SLAVEMODE_DISABLE;
  HAL_TIM_SlaveConfigSynchronization(&timer, &sSlaveConfigure);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&timer, &sMasterConfig);

  ClockConfig.ClockFilter = 0;
  ClockConfig.ClockPolarity = TIM_CLOCKPOLARITY_RISING;
  ClockConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
  ClockConfig.ClockSource = TIM_CLOCKSOURCE_TI2; 
  HAL_TIM_ConfigClockSource( &timer, &ClockConfig );
   TIM1->CR1 |= TIM_CR1_ARPE; // autoreload on
   //TIM1->CR1 |= TIM_CR1_CEN;
   TIM1->CR1 = 1;  // enable timer

 while (1) {
        int16_t count1;
        count1=TIM1->CNT; 

        printf("%d\r\n", count1);
        wait(1.0);

 };
} 

尝试使用算术。假设方向包含有关方向的信息(从外部引脚读取值),计数器是最终结果

unsigned char direction=0;
unsigned int counter=0;

while (1) {
    int16_t count1;

    //Read current counter value
    count1=TIM1->CNT; 

    //Check for direction Up (use PA_8 instead direction here)
    if(direction=0){
        counter = count;
    }
    else
    {
        //Check for direction down
        counter = TIMER_MAX - count1;
    }

    printf("%d\r\n", count1);
    wait(1.0);

 };

我希望这能帮助你。再见

此代码运行良好

#include "mbed.h"
#include "stm32f4xx.h"
#include "stm32f4xx_hal_tim_ex.h"

TIM_HandleTypeDef timer;
TIM_Encoder_InitTypeDef encoder;

//direction to PA_9 -- step to PA_8

int main(){
     GPIO_InitTypeDef GPIO_InitStruct;
        __TIM1_CLK_ENABLE();
        __GPIOA_CLK_ENABLE();
        GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLDOWN;
        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    timer.Instance = TIM1;
    timer.Init.Period = 0xffff;
    timer.Init.Prescaler = 1;
    timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    timer.Init.CounterMode = TIM_COUNTERMODE_UP;


    encoder.EncoderMode = TIM_ENCODERMODE_TI1; 
    encoder.IC1Filter = 0x0f;
    encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; //step signal
    encoder.IC1Prescaler = TIM_ICPSC_DIV1;
    encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;

    encoder.IC2Filter = 0x0f;
    encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;  //check direction  
    encoder.IC2Prescaler = TIM_ICPSC_DIV1;
    encoder.IC2Selection = TIM_ICSELECTION_INDIRECTTI;

    HAL_TIM_Encoder_Init(&timer, &encoder);
    HAL_TIM_Encoder_Start(&timer,TIM_CHANNEL_1);   


    TIM1->EGR = 1;           // Generate an update event
    TIM1->CR1 = 1;           // Enable the counter


 while (1) {
        int16_t count1;
        count1=TIM1->CNT; 

        printf("%d\r\n", count1);
        wait(1.0);

 };
} 

您是否可以配置更改时中断的输入?如果是,您可以管理自定义计时器。我想提醒使用paolo della vedova答案的任何人。我有一个项目,在那里我使用他们发布的代码,并发现它的工作。经过一段时间和单位编程后,我发现这种行为非常不可靠。TIM仍将充当步进和方向计数器,但将在不同方向对引导之间方向引脚上的相同输入电平进行计数。我阅读了我的STM32芯片(STM32F446)的参考手册,发现paolo的回答中使用了一些无效的配置。-极性不应该是两边-编码器接口不能使这个解决方案工作得很好,我认为你的是最好的解决方案!再见