简体   繁体   English

stm32如何使用定时器使脉冲计数增加/减少

[英]stm32 how to make pulse count up/down with timer

I need for my personal project counting pulse and direction with timer.我的个人项目需要用计时器计算脉冲和方向。 With this code I can count only one direction.使用此代码,我只能计算一个方向。 Any suggestion are welcome for correct code (this code is pretesting)欢迎提供正确代码的任何建议(此代码是预测试的)

pulse count to PA_9 and direction input to PA_8脉冲计数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);

 };
} 

this code is working well这段代码运行良好

#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);

 };
} 

Try to use arithmetic.尝试使用算术。 Suppose direction contains the information about direction (read the value from your external pin) and counter is the final result.假设方向包含有关方向的信息(从外部引脚读取值),计数器是最终结果。

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);

 };

I hope this can help you.我希望这可以帮助你。 Ciao再见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM