stm32如何使用定时器进行脉冲计数加/减

时间:2015-10-05 11:51:57

标签: 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 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);

 };
} 

2 个答案:

答案 0 :(得分:1)

此代码运作良好

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

 };
} 

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

 };

我希望这可以帮到你。侨