cancel
Showing results for 
Search instead for 
Did you mean: 

Toggle leds - EXTI and timers

kasabian012
Associate II
Posted on November 18, 2013 at 15:55

I'm trying to create a program that toggles 4 leds every 2 seconds. After you press the user button, 2 of them should blink every 2 seconds and the other one every second. I'm having troubles to understand how you can do it using timers and connecting it with the user button.

4 REPLIES 4
Posted on November 18, 2013 at 16:02

The exact implementation would depend a lot on the specific board and part you're talking about. There are at least half a dozen ''Discovery'' boards, so you'll need to be more precise.

The firmware libraries for the boards and parts do have a reasonably extensive set of examples that could be crafted into the solution you need, start there.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kasabian012
Associate II
Posted on November 18, 2013 at 16:14

Sorry. I'm using STM32F4Discovery. In our classes we've been introduced to timers and interruptions, however, not how to make a connection between that and the use of the user button.

We made this for a single led, I just don't know how to include the button part and create the infinite loop that makes that everytime you press the button it change from one to another state.

#include <stm32f4xx.h>

int main ()

{

//GPIO structure declaration

GPIO_InitTypeDef GPIO_InitStruct;

//Timer Timebase structure declaration

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

//NVIC structure declaration

NVIC_InitTypeDef NVIC_InitStructure;

//GPIO clock

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

//TIM2 peripherical clock

RCC_AHB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

//GPIO peripherical properties specification

GPIO_InitStruct.GPIO_Pin= GPIO_Pin_13;

GPIO_InitStruct.GPIO_Mode= GPIO_Mode_OUT;

GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_OType= GPIO_OType_PP;

GPIO_InitStruct.GPIO_PuPd= GPIO_PuPd_NOPULL;

//TIM2 Timebase properties specifications

TIM_TimeBaseStructure.TIM_Prescaler = 42000 - 1;

TIM_TimeBaseStructure.TIM_Period = 2000 - 1;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

//Interruption

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

//GPIO initialization

GPIO_Init(GPIOD, &GPIO_InitStruct);

//Enabling TIM_IT

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

TIM_Cmd(TIM2, ENABLE);

while(1)

{

}

}

kasabian012
Associate II
Posted on November 18, 2013 at 16:15

Sorry. I'm using STM32F4Discovery. In our classes we've been introduced to timers and interruptions, however, not how to make a connection between that and the use of the user button.

We made this for a single led, I just don't know how to include the button part and create the infinite loop that makes that everytime you press the button it change from one to another state.

#include <stm32f4xx.h>

int main ()

{

//GPIO structure declaration

GPIO_InitTypeDef GPIO_InitStruct;

//Timer Timebase structure declaration

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

//NVIC structure declaration

NVIC_InitTypeDef NVIC_InitStructure;

//GPIO clock

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

//TIM2 peripherical clock

RCC_AHB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

//GPIO peripherical properties specification

GPIO_InitStruct.GPIO_Pin= GPIO_Pin_13;

GPIO_InitStruct.GPIO_Mode= GPIO_Mode_OUT;

GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_OType= GPIO_OType_PP;

GPIO_InitStruct.GPIO_PuPd= GPIO_PuPd_NOPULL;

//TIM2 Timebase properties specifications

TIM_TimeBaseStructure.TIM_Prescaler = 42000 - 1;

TIM_TimeBaseStructure.TIM_Period = 2000 - 1;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

//Interruption

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

//GPIO initialization

GPIO_Init(GPIOD, &GPIO_InitStruct);

//Enabling TIM_IT

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

TIM_Cmd(TIM2, ENABLE);

while(1)

{

}

}

crt2
Associate II
Posted on November 20, 2013 at 11:04

Button = user toggled interrupt

Timer = time toggled interrupt

Basically what you want to do is detect button pressed (watch for debouncing - I'm sure google will give nice example of such code) and you want to change variables that are used to determine Timer values. Some simple state when button is pressed you use certain values, when it is pressed again you use other values.

If you leave while(1), you will have to change values in button interrupt routine function. Might not be the cleanest but it will work. Otherwise you can just say that after timer has elapsed, you recheck values for timers (timer interrupt routine), while in button irq (interrupt routine) you change the values with each press.