stm32 how to make pulse count up/down with timer

主宰稳场 提交于 2019-12-24 00:22:52

问题


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

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

 };
} 

回答1:


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

 };
} 



回答2:


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



来源:https://stackoverflow.com/questions/32947972/stm32-how-to-make-pulse-count-up-down-with-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!