本文介绍了stm32如何使用计时器使脉冲递增/递减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的个人项目需要使用计时器来计数脉冲和方向。
使用此代码,我只能计算一个方向。
任何建议均欢迎使用正确的代码(此代码正在测试中)
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)
脉冲计数为 PA_9
和方向输入到 PA_8
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);
};
}
推荐答案
此代码运行良好
#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);
};
}
这篇关于stm32如何使用计时器使脉冲递增/递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!