cancel
Showing results for 
Search instead for 
Did you mean: 

Timer question

scott
Associate II
Posted on June 16, 2014 at 08:55

I'm very new to electronics and have a basic question regarding timer setup. I'm using an STM8L151K6 and am trying to configure TIM2 to simply generate an interrupt every 30 seconds using the standard peripheral library. I don't require any type of waveform generation, just a 30 second timeout to disable an attached IC. The MCU is halted during this time, so I do require an interrupt to wake. If possible, how do I configure TIM2 for this interval?

3 REPLIES 3
fggnrc2
Associate II
Posted on June 16, 2014 at 17:51

Scott,

when a STM8L151 is halted, its timers are halted too, so it's impossible to generate an interrupt every 30 seconds with TIMx.

This doesn't mean that a STM8L151 can't provide what you need.

You should use the RTC, enable its indipendent RC oscillator or an 32768 Hz external resonator, and configure a RTC alarm.

Regards

EtaPhi

scott
Associate II
Posted on June 16, 2014 at 18:19

Thanks for response. I actually misstated the sleep mode. The MCU is in wait-for-interrupt as opposed to halt. I've been able to generate an interrupt every 1ms, but was assuming that by setting the counter to 30000, it would wait for 30s. For example:

static void InitDelay2()

{

   // Enable TIM4 clock

   CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);

  // Time base configuration (~1ms)

  TIM2_TimeBaseInit(TIM2_Prescaler_128, TIM2_CounterMode_Down, U8(125));

  TIM2_SetCounter(30000);

  // No repeat

  TIM2_SelectOnePulseMode(TIM2_OPMode_Single);

  // Enable update interrupt

  TIM2_ITConfig(TIM2_IT_Update, ENABLE);

  // Start timer

  TIM2_Cmd(ENABLE);

}

I'll look into setting the RTC, as well.
scott
Associate II
Posted on June 16, 2014 at 20:21

RTC is working fine. I've configured LSI clock source. It seems closer to 36KHz rather than 38KHz. I don't need exact timing, so this is working fine. Thanks again for the help.