cancel
Showing results for 
Search instead for 
Did you mean: 

how to use timer _ interrupt

accounts
Associate II
Posted on January 26, 2012 at 17:58

i am beginner about programming on  stm32l discovery board.  this is my first time  programming on stm microcontrollers.  I can open led. close led.  i can use lcd on it. but i want to  do that.

i have   int i=0;   and i want to increase value of  it  every 1 second or 100 ms and  i want to show   i value on lcd.    Showing on lcd  not problem but.  using tiimer  problem..   i did somethings but it  is not working .  finally i decided to write my problem here.  

thanks.

#how-to-use-timer-on-stm32l
8 REPLIES 8
mard
Associate II
Posted on January 27, 2012 at 18:33

The best way (IMHO) - use the Systick. Looking for some Systick demo example.

accounts
Associate II
Posted on January 27, 2012 at 21:03

I did what you sad. it worked. But   I need Timer using . beacuse   i need 3 timer and their handlers .  but now. i couldn't do anything on timer. I looked a lot examples. anycode isn't working. all corrupt, missing.

I found  an example about TIM9 but it is only working on pin.  i need  function.  

But thanks for systick reply.

accounts
Associate II
Posted on January 30, 2012 at 13:11

can anyone post a timer example. 

Posted on January 31, 2012 at 22:16

I did what you sad. it worked. But   I need Timer using . beacuse   i need 3 timer and their handlers .  but now. i couldn't do anything on timer. I looked a lot examples. anycode isn't working. all corrupt, missing.

 

 

I found  an example about TIM9 but it is only working on pin.  i need  function.  

The STM32L can't be that different to the other STM32 parts, it should be a matter of enabling the TIM_Update interrupt, and providing a simple service routine. Why don't you paste in some of the examples that are supposedly not working, and we'll see if there are any obvious coding mistakes or issues.

You also don't specify your development environment, which might be helpful if you want some click-n-run examples.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
accounts
Associate II
Posted on February 01, 2012 at 01:13

#include ''stm32l1xx.h''

//#include ''stm32l1xx.h''

#include ''discover_board.h''

#include ''stm32l1xx_tim.h''

static volatile uint32_t TimingDelay;

void TimingDelay_Decrement(void);

int kutay = 0;

int tick_sayici=0;

void GPIO_Configuration(void);

void NVIC_Configuration(void);

int main(void)

{

  NVIC_Configuration();   /* <--- Tried with and without this call. */

  GPIO_Configuration();

  /* Setup SysTick Timer for 1 msec interrupts.

   */

  int  asd=0;

  asd =  SystemCoreClock / 1000; // 16 mhz sistemin ic hizi

  //asd = SysTick_Config(SystemCoreClock / 1000);

    //SysTick_CounterCmd(SysTick_Counter_Enable);

/*  if (SysTick_Config(SystemCoreClock / 10000))

  {

    while (1);                  /// Capture error

  }*/

  SysTick_Config(SystemCoreClock / 1);

 // GPIO_ResetBits(LED_PORT, LED1_PIN); /* LED on. */

  GPIO_HIGH(GPIOB,GPIO_Pin_8);

  GPIO_HIGH(GPIOB,GPIO_Pin_7);

  while (1)

  {

  }

}

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  /*

  RCC_APB2PeriphClockCmd(LED_RCC_APB2Periph_GPIO | RCC_APB2Periph_AFIO, ENABLE);

  // YUKARDAKI NE OLUYOR  FARKI NE ANLAMADIM ...

  */

  //RCC_APB2PeriphClockCmd(RCC_APB1Perip)

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_6;  // 7 ,6

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

}

void NVIC_Configuration(void)

{

  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = SysTick_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

void Delay(uint32_t nTime){

  TimingDelay = nTime;

  while(TimingDelay != 0);

}

void TimingDelay_Decrement(void){

  if (TimingDelay != 0x00){

    TimingDelay--;}

  tick_sayici++;

  if(tick_sayici%2){ GPIO_LOW(GPIOB,GPIO_Pin_7);}else

  { GPIO_HIGH(GPIOB,GPIO_Pin_7); }

  if(tick_sayici==16000000)tick_sayici=0;

}

accounts
Associate II
Posted on February 01, 2012 at 01:15

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6jF&d=%2Fa%2F0X0000000bu5%2FWlcQLeciWB.898Ph9YeAu1V4m8hMwIIggT.Tj1vMuDA&asPdf=false
Posted on February 01, 2012 at 02:16

Something like this, perhaps

#include ''stm32l1xx.h''
#include ''discover_board.h''
#include ''stm32l1xx_tim.h''
static volatile uint32_t TimingDelay;
void TimingDelay_Decrement(void);
int kutay = 0;
int tick_sayici = 0;
void GPIO_Configuration(void);
int main(void)
{
GPIO_Configuration();
/* Setup SysTick Timer for 1 second interrupts */
SysTick_Config(SystemCoreClock / 1); // Cannot exceed 16,777,215
/* Set SysTick Preemption Priority, it's a system handler rather than a regular interrupt */
NVIC_SetPriority(SysTick_IRQn, 0x04);
GPIO_HIGH(GPIOB,GPIO_Pin_6); // PB.06 BLUE
GPIO_HIGH(GPIOB,GPIO_Pin_7); // PB.07 GREEN
while (1)
{
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Only important for remapping
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
// PB.06 BLUE LED, PB.07 GREEN LED
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Delay(uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
// Called from SysTick_Handler
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00){
TimingDelay--;}
tick_sayici++;
if (tick_sayici >= 60) // Minute
tick_sayici = 0;
if (tick_sayici % 2)
{
GPIO_LOW(GPIOB,GPIO_Pin_7);
}
else
{
GPIO_HIGH(GPIOB,GPIO_Pin_7);
}
}
// This would normally exist in stm32l1xx_it.c, or equivalent
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 01, 2012 at 02:54

Or this for SysTick + TIM2, you might have to play with the #include's

#include ''stm32l1xx.h'' // low power mcu
#include ''discover_board.h''
#include ''stm32l1xx_tim.h''
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void RCC_Configuration(void);
void TIM2_Configuration(void);
void SysTick_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
GPIO_HIGH(GPIOB,GPIO_Pin_6); // PB.06 BLUE
GPIO_HIGH(GPIOB,GPIO_Pin_7); // PB.07 GREEN
SysTick_Configuration();
TIM2_Configuration();
while(1); // Infinite loop, main() must not exit
}
void RCC_Configuration(void)
{
/* Enable the GPIOs Clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Enable APB1 clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_PWR,ENABLE);
/* Enable SYSCFG */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Allow access to the RTC */
PWR_RTCAccessCmd(ENABLE);
/* Reset Backup Domain */
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
/*!< 
LSE
Enable */
RCC_LSEConfig(RCC_LSE_ON);
/*!< Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {}
/*!< LCD Clock Source Selection */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel
= 
TIM2_IRQn
;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority
= 
2
;
NVIC_InitStructure.NVIC_IRQChannelSubPriority
= 
1
;
NVIC_InitStructure.NVIC_IRQChannelCmd
= 
ENABLE
;
NVIC_Init(&NVIC_InitStructure);
/* Set SysTick Preemption Priority, it's a system handler rather than a regular interrupt */
NVIC_SetPriority(SysTick_IRQn, 0x04);
}
void SysTick_Configuration()
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1); // 1 Hz
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler
= 
16000
- 1; // 16 MHz / 
16000
= 1 KHz
TIM_TimeBaseStructure.TIM_Period
= 
1000
- 1; // 1 KHz / 
1000
= 1 Hz;
TIM_TimeBaseStructure.TIM_ClockDivision
= 
0
;
TIM_TimeBaseStructure.TIM_CounterMode
= 
TIM_CounterMode_Up
;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Enable TIM2 Update Interrupt */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* Enable TIM2 */
TIM_Cmd(TIM2,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// PB.06 BLUE LED, PB.07 GREEN LED
GPIO_InitStructure.GPIO_Pin
= 
GPIO_Pin_6
| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode
= 
GPIO_Mode_OUT
;
GPIO_InitStructure.GPIO_OType
= 
GPIO_OType_PP
;
GPIO_InitStructure.GPIO_PuPd
= 
GPIO_PuPd_UP
;
GPIO_InitStructure.GPIO_Speed
= 
GPIO_Speed_10MHz
;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
// These would normally exist in stm32l1xx_it.c, or equivalent
void SysTick_Handler(void)
{
static int 
SYSTICK_Tick
= 
0
;
SYSTICK_Tick++;
if (SYSTICK_Tick >= 60) // 1 Minute
SYSTICK_Tick = 0;
if (SYSTICK_Tick % 2) // Toggle PB.07 GREEN LED
{
GPIO_LOW(GPIOB,GPIO_Pin_7);
}
else
{
GPIO_HIGH(GPIOB,GPIO_Pin_7);
}
}
void TIM2_IRQHandler(void)
{
static int TIM2_Tick = 0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM2_Tick++;
if (TIM2_Tick >= 60) // 1 Second
TIM2_Tick = 0;
if (TIM2_Tick % 2) // Toggle PB.06 BLUE LED
{
GPIO_LOW(GPIOB,GPIO_Pin_6);
}
else
{
GPIO_HIGH(GPIOB,GPIO_Pin_6);
}
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..