Hi all!
Hope all is well for you all.
Okay, I’ve been playing around with timers/interrupts on the STM32 for a few days now, and I’ve come to a mental block in regards to the actual values for the prescaler and period etc :?
I’ve looked at the manuals, and code etc., and am now just confusing myself! So I thought I would just ask the group and see if someone can just set me straight…
Lets say I have my sysclock set to 72MHz.
I want to set TIM3 to be at 1KHz (actually you can pick any number…)
How do I calculate the values (prescaler, period, etc?)
I’m sure on page xyz of the manual it shows the formula, I’ve just become blind/blocked and now cannot see anything in my minds eye… :!:
Here is a code sample that comes close to 1KHz, I used a scope and adjusted numbers until I got there…did not calculate the numbers…
#include "stm32f10x_conf.h"
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "misc.h"
// TIM2 callback handler
void TIM2_IRQHandler()
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
// toggle bit
GPIOC->ODR ^= GPIO_Pin_12;
}
void RCC_Configuration(void)
{
/* Enable TIM2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM2 configuration */
TIM_TimeBaseStructure.TIM_Period = 132; // 133
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
/* Output Compare Timing Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_Pulse = 0x0;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Immediate load of TIM2 Precaler value */
TIM_PrescalerConfig(TIM2, ((SystemCoreClock/1200) - 1), TIM_PSCReloadMode_Immediate);
/* Clear TIM2 update pending flag */
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/* Enable TIM2 Update interrupt */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the system clocks */
RCC_Configuration();
/* Enable the GPIO_LED Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
// config port
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure TIMs */
TIM_Configuration();
/* Configure two bits for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the TIM2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while(1)
{
}
}
Many thanks in advance!