Question
TIM 15 uSeconds
Posted on June 27, 2016 at 19:58
I have and EXTI fire when the signal goes high and I want to check the signal 15 microseconds after the interrupt. I'm using a TIM but it doesn't work. I'm using an STM32F4 Discovery MCU
#include ''stm32f4xx_hal.h''
#include <
string.h
>
#include <
stdlib.h
>
#include ''stm32f4xx_hal_tim.h''
#include ''stm32f4xx_hal_tim_ex.h''
void enable_exti(void);
void enable_tim(void);
void EXTI2_IRQHandler(void);
void SysTick_Handler(void);
void stopwatch(int uSecs);
TIM_HandleTypeDef tim_init;
int usec = 0;
int curr_sec = 0;
// Note: defined HAL_TIM_MODULE_ENABLED in stm32f4xx_hal_tim_ex.c
// defined HAL_TIM_MODULE_ENABLED in stm32f4xx_hal_conf.h
// clock runs at 16,000,000 Hz
int
main(void){
// setup TIM
HAL_TIM_Base_MspInit(&tim_init); // initialize the TIM basic
enable_tim();
// setup EXTI
enable_exti();
// configure clock to 16MHz (check with HAL_RCC_GetSysClockFreq())
SysTick_Config(HAL_RCC_GetHCLKFreq());
while(1);
}
void
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if(htim->Instance == TIM2){
if(curr_sec == usec){
HAL_TIM_Base_Stop(&tim_init); // stop the timer it has been 15 useconds
// print the level of the signal to the console
curr_sec = 0;
}
else
curr_sec++;
}
}
/*
* Used to start a timer that will fire an interrupt at num microseconds. The
* interrupt is configured in the HAL_TIM_PeriodElapsedCallback() function.
*/
void
stopwatch(int num){
curr_sec = 0;
usec = num;
HAL_TIM_Base_Start(&tim_init); // starts the timer
}
void
SysTick_Handler(void){
HAL_IncTick();
}
/*
* Configures pin PB02 as the SDQ wire.
*/
void
enable_exti(void){
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin PB2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI2_IRQn, 0x1, 0);
HAL_NVIC_EnableIRQ(EXTI2_IRQn);
}
/*
* Configures the timer to pin PA3.
*/
void
enable_tim(void){
tim_init.Instance = TIM2;
tim_init.Init.CounterMode = TIM_COUNTERMODE_UP;
tim_init.Init.Prescaler = 16 - 1; // 1,000,000 Hz -> 1/1,000,000 seconds
tim_init.Init.Period = 0;
tim_init.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // for now...
//tim_init.Init.RepetitionCounter = 0;
__TIM2_CLK_ENABLE(); // pin PA3
// configure the GPIO PA3
__GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0); //Enable the peripheral IRQ
HAL_NVIC_EnableIRQ(TIM2_IRQn);
HAL_TIM_Base_Init(&tim_init);
//HAL_TIM_Base_Start_IT(&tim_init); //Start the timer
}
void
TIM2_IRQHandler(void) {
HAL_TIM_IRQHandler(&tim_init);
}
void
EXTI2_IRQHandler(void){
stopwatch(15); // function that starts the timer and checks signal after 15 useconds
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
}