I want to generate ONE PULSE TRIGGER by comparing the encoder counts input from 2MHz to 4MHz and setting the delay time and duty.
hello.
I want to generate ONE PULSE TRIGGER by comparing the encoder counts input from 2MHz to 4MHz and setting the delay time and duty.
Currently on TIM6
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) is used and trigger is generated using the toggle function of general GPIO.
After comparing the encoder pulse input from 2MHz to 4MHz with a period of 5pulse (10us), accurate on-time trigging of 2us is required.
(2MHz input: 1 pulse per 2us)
(4MHz input: 1 pulse per 1us)
So the toggle function of general gpio with delay and offset cannot be used.
Any advice on how to fire an adjustable one pulse trigger after comparing tim2 -> cnt ?
If there is another way, I'll work **** ** it.
thank you
BOARD: NUCLEO-F746ZG (max 216MHz TIMER CLOCK)
IDE: STM32CubeIDE (Version: 1.10.1)
tim2 : HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
tim6 : HAL_TIM_Base_Start_IT(&htim6);
< initial code>
///////////////////////////////////////////////////////////////////////////////////////////////////////
void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_Encoder_InitTypeDef sConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 4294967295;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
sConfig.IC1Filter = 0;
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
sConfig.IC2Filter = 0;
if (HAL_TIM_Encoder_Init(&htim2, &sConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MX_TIM6_Init(void)
{
/* USER CODE BEGIN TIM6_Init 0 */
/* USER CODE END TIM6_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 1-1;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 400-1;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM6_Init 2 */
/* USER CODE END TIM6_Init 2 */
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
< callback code>
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM6)
{
if(TrgBusy == LOW) //TrgBusy : processing check
{
PreviousTrgCount = TIM2->CNT;
TrgBusy = HIGH;
}
CheckTrgCount = TIM2->CNT;
if (CheckTrgCount > PreviousTrgCount+TrgPeriod-1)
{
HAL_GPIO_TogglePin(GPIOB, LD1_Pin);
TrgBusy = LOW;
}
}
}