cancel
Showing results for 
Search instead for 
Did you mean: 

Problem: Time Base generation on STM32F411 Nucleo

e0225235
Associate II
Posted on January 26, 2015 at 16:36

Hello,

as a new comer to the world of STM32, i started with some tutorials and examples to get into working with those nice microprocessors/micro controllers. Im trying since 1 week to produce a simple time base in order to start with the timer modules. Actually it runs right now, but no matter which parameters i use for the prescaler and top value (in ARR-register), i still get the same signal as output (period ~770ns with 50%dutyCycle). Does anyone have an idea what the problem is? some facts: + Board: STM32F411 NucleoI use the HAL-Library from ST. + The program should toggle a pin each time the top value is reached.

int
main(
void
)
{
HAL_Init();
/* Configure the System clock to 100 MHz */
SystemClock_Config();
/* Configure output port */
port_init();
/* == Timer Challenge == */
TimHandle.Instance = TIM3;
TimHandle.Init.Period = 0x0fff;
TimHandle.Init.Prescaler = 0x00ff;
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
/* ++ debug info output ++ */
//printf(''\nHello world!\n'');
//printf(''%d\n'',HAL_RCC_GetSysClockFreq());
/* ++ debug info output ++ */
HAL_TIM_Base_Init(&TimHandle);
HAL_TIM_Base_Start_IT(&TimHandle);
/* == Timer Challenge == */
while
(1)
{
}
}
void
HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
// Enable TIM Interface Clock
__TIM3_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM3_IRQn, 4, 0);
// Enable the TIMx global Interrupt
HAL_NVIC_EnableIRQ(TIM3_IRQn);
}
void
port_init(
void
)
{
/* Enable the LED Clock */
__GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIOA_InitStruct;
GPIOA_InitStruct.Pin = GPIO_PIN_6;
GPIOA_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIOA_InitStruct.Pull = GPIO_PULLUP;
GPIOA_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOA, &GPIOA_InitStruct);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6,GPIO_PIN_RESET); 
}

File stm32f4xx_it.c

void TIM3_IRQHandler(void)
{
HAL_NVIC_ClearPendingIRQ(TIM3_IRQn);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6);
}

#set-time-base #stm32-nucleo-stm32f411
2 REPLIES 2
e0225235
Associate II
Posted on January 27, 2015 at 10:48

should this post be moved to the ''STM32 Software Tools and Firmware'' section?

e0225235
Associate II
Posted on January 27, 2015 at 13:02

Now i tried to understand the settings of period and prescaler parameters

According to the following settings, an output signal with the frequency of 10 kHz should be expected (explanations for the values are as comments below)

/* Set TIMx instance */
uint32_t timer_freq = 10000; 
// 10kHz
uint32_t P = 1; 
// P=prescaler+1, use no prescaler, i.e. prescaler=0 
TimHandle.Instance = TIM3;
/* 
* Calculate Period from the equation: timer_freq = SystemCoreClock / (P*Q) => 
* Q = SystemCoreClock / (P*timer_freq)
*/
TimHandle.Init.Period = ((SystemCoreClock / timer_freq) / P) - 1; 
TimHandle.Init.Prescaler = P-1;
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

I hope its theoretically correct, but I'm still getting the same output signal with a period of ~775ns and frequency 1.290 MHz. Anyone can help with that? EDIT1: I'm sure that

SystemCoreClock

is 100 MHz, but not really aware if any AHB or APBx prescalers are set. the upper calculation assumes a timer clock equals the

SystemCoreClock

, i.e. 100 MHz. EDIT2: I found the problem. The processor was all the time in the interrupt routine, it does nothing else than toggeling the pin. After reading the Datasheet:

Bit 0 UIF: Update interrupt flag. This bit is set by hardware on an update event. It is cleared by software.

I cleared the bit in the interrupt routine manually and it worked! the

HAL_NVIC_ClearPendingIRQ(TIM3_IRQn);

function provided by the HAL-Library didn't do what I expected. Anyhow, the interrupt routine now looks like:

void
TIM3_IRQHandler(
void
)
{
TIM3->SR &= ~((uint32_t)0x01);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6);
}