paolo della vedova
/
stm32_pulse_counter
pulse counter stm32
Embed:
(wiki syntax)
Show/hide line numbers
counter.cpp
00001 00002 #include "mbed.h" 00003 #include "stm32f4xx.h" 00004 #include "stm32f4xx_hal_tim_ex.h" 00005 00006 TIM_HandleTypeDef timer; 00007 TIM_Encoder_InitTypeDef encoder; 00008 00009 //direction to PA_9 -- step to PA_8 00010 00011 int main(){ 00012 GPIO_InitTypeDef GPIO_InitStruct; 00013 __TIM1_CLK_ENABLE(); 00014 __GPIOA_CLK_ENABLE(); 00015 GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; 00016 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 00017 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00018 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; 00019 GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; 00020 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 00021 00022 timer.Instance = TIM1; 00023 timer.Init.Period = 0xffff; 00024 timer.Init.Prescaler = 1; 00025 timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 00026 timer.Init.CounterMode = TIM_COUNTERMODE_UP; 00027 00028 encoder.EncoderMode = TIM_ENCODERMODE_TI1; 00029 encoder.IC1Filter = 0x0f; 00030 encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; //step signal 00031 encoder.IC1Prescaler = TIM_ICPSC_DIV1; 00032 encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI; 00033 00034 encoder.IC2Filter = 0x0f; 00035 encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE; //check direction 00036 encoder.IC2Prescaler = TIM_ICPSC_DIV1; 00037 encoder.IC2Selection = TIM_ICSELECTION_INDIRECTTI; 00038 00039 HAL_TIM_Encoder_Init(&timer, &encoder); 00040 HAL_TIM_Encoder_Start(&timer,TIM_CHANNEL_1); 00041 00042 00043 TIM1->EGR = 1; // Generate an update event 00044 TIM1->CR1 = 1; // Enable the counter 00045 00046 00047 while (1) { 00048 int16_t count1; 00049 count1=TIM1->CNT; 00050 00051 printf("%d\r\n", count1); 00052 wait(1.0); 00053 00054 }; 00055 }
Generated on Tue Jul 19 2022 22:55:20 by 1.7.2