I'm learning to programm on an STM32F7 46 G Discovery board using Eclipse as an IDE and STM32CubeMX. I'm trying to set a timer (currently TIM2) to one second without using an ISR. I'd like to get a tick from the timer every second.
Below is what I tried so far:
#include "main.h"
#include "stm32f7xx_hal.h"
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
__TIM2_CLK_ENABLE();
while (1)
{
HAL_TIM_PeriodElapsedCallback(&htim2);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim2)
{
if (htim2->Instance==TIM2) //check if the interrupt comes from TIM2
{
HAL_GPIO_TogglePin(ARDUINO_D2_GPIO_Port,ARDUINO_D2_Pin);
}
}
static void MX_TIM2_Init(void) /* TIM2 init function */
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim2.Instance = TIM2;
htim2.Init.Prescaler = 41999; //max 65 536
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = 0;
HAL_TIM_Base_Init(&htim2);
HAL_TIM_Base_Start(&htim2);
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
I know that the MCU runs at 42MHz which finally makes the clock run at 84MHz with PLL and I think I already set the Prescaler and the Period (to fix the autoreload time) values accordingly.
Thanks for answering and sorry for any English mistake (it's not my first language)
No comments:
Post a Comment