0

如何在更改方向时添加此代码重置?计数器用步进电机信号测试,工作正常,计数上下,但在 -32766 -32765 之后限制为 32767 ..

对于更改方向的重置,我想添加以下行:

TIM_SlaveConfigTypeDef SlaveModeselect
TIM_MasterConfigTypeDef MasterConfig

SlaveModeselect.SlaveMode = ....
SlaveModeselect.InputTrigger = TIM_TS_TI1FP1 

MasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_......
MasterConfig.MasterOutputTrigger = .....

HAL_TIM_SlaveConfigSynchronization(&timer, &SlaveModeselect)
HAL_TIMEx_MasterConfigSynchronization(&timer, &MasterConfig) 

当前代码:

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

TIM_HandleTypeDef timer;
TIM_Encoder_InitTypeDef encoder;

//direction to PA_9 -- step pulse 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; 
    encoder.IC1Prescaler = TIM_ICPSC_DIV1;
    encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;

    encoder.IC2Filter = 0x0f;
    encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_RISING;    
    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);

 };
} 
4

1 回答 1

1

使用uint16_t count1而不是int16_t让您的计数器计数到 65535,然后在溢出时重置为 0。

于 2015-10-16T22:00:23.117 回答