2014-06-03 08:04 AM
Hi All,
I am using STM32 F0 Discovery board with Raisonance IDE, I am with LED blink using TIM. I want to blink for every 1s but I dont know how to set the timer, Please tell me how the calculation is done for 1s and 1ms. And what should be the value of Prescalar value,TIM_Period n TIM_Prescaler. Right now I have put random values. Please guide me.PrescalerValue = (uint16_t) (SystemCoreClock /2400000 ); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1000; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;2014-06-03 08:39 AM
It's simple math using factors, the Prescaler and Period are feed with maximal values, where N ticks are represented as 0 to N-1. The Prescaler is 16-bit, the Period may be 16-bit or 32-bit depending on the timer, not sure on F0 options
With a 24 MHz clock, 1 ms = 24000 ticks, TIM Update = TIM Clock / (X * Y) Prescaler = X - 1 Period = Y - 1 For 1 ms, X = 1, Y = 24000 or X = 24, Y = 1000 For 1 s X = 24000, Y = 1000 or X = 2400, Y = 10000 or X = 800, Y = 30000 or X = 400, Y = 60000 I'd use the UP count mode, one could also drive an LED directly from a TIM channel2014-06-03 10:29 PM
Hello Clive,
I set the values as per ur suggestion for 1s but the LED does not blink exactly at 1s, so I have attached the file of my project. Mine is 48MHz so I have adjusted as as per that clock. Appreciate your work. Thanks ________________ Attachments : main.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0za&d=%2Fa%2F0X0000000bgf%2FnPPkmk_nooBoH4t7splw9qhgt9XwgpM2MUGH5QCbztI&asPdf=false2014-06-04 04:11 AM
I don't see any interrupt handling code!
You want to use the UPDATE interrupt, not CC12014-06-04 04:41 AM
Below is my IRQ handler. It is in ''stm32f0xx_it.c''. I replaced CC1 with Update. I can blink the Led for every 1s. But only when
Prescaler= 2000Period= 24000And Blinking time differs for other values which u had mentioned for 1s. Why is it so???void TIM14_IRQHandler(void){ if (TIM_GetITStatus(TIM14, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM14, TIM_IT_Update); TimingDelay_Decrement(); }}2014-06-04 06:31 AM
And Blinking time differs for other values which u had mentioned for 1s. Why is it so???
Probably because I made the assumption you were using a 24 MHz clocks, and you aren't?2014-06-04 06:53 AM
This is how I might present an example, a somewhat blind implementation but should be functional
/* TIM14 1 Hz Toggle STM32F0 sourcer32@gmail.com */
#include ''stm32f0xx.h''
/***************************************************************************/
void RCC_Configuration(void)
{
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* TIM14 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
}
/***************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
}
/***************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM14 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/***************************************************************************/
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1; // To KHz
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // To Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
/* TIM Interrupts enable */
TIM_ITConfig(TIM14, TIM_IT_Update, ENABLE);
/* TIM14 enable counter */
TIM_Cmd(TIM14, ENABLE);
}
/***************************************************************************/
void TIM14_IRQHandler(void)
{
if (TIM_GetITStatus(TIM14, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM14, TIM_IT_Update);
/* LED toggling */
GPIOC->ODR ^= GPIO_Pin_8 | GPIO_Pin_9;
}
}
/***************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
TIM_Configuration();
/* Infinite loop */
while (1);
}
/***************************************************************************/
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d
'', file, line) */
/* Infinite loop */
while (1)
{}
}
#endif
/***************************************************************************/
2014-06-04 09:11 PM
Thank you so much Clive, it worked. Now what should I do for setting 1ms in this
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1; TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // To HzI didn't understand the calculation behind this?? Please2014-06-05 09:28 AM
Thank you so much Clive, it worked. Now what should I do for setting 1ms in this
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1;
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // To Hz I didn't understand the calculation behind this?? Please
I explained the math earlier, you are dividing a source clock with two factors. 1 ms TIM_TimeBaseStructure.TIM_Prescaler = 0; // No division TIM_TimeBaseStructure.TIM_Period = (uint16_t) (SystemCoreClock / 1000) - 1; // Clocks for 1/1000th of a second (millisecond) Understand that 1 second is 48000000 ticks of a 48 MHz clock, 1 millisecond would be 1/1000th of that, ie 48000 The values programmed into the hardware for comparison are N-1