ibrahim ozturk

Entrepreneur, Tech CTO, Software Developer, Electronics Engineer, Author




User Push Button and LEDs on STM32F4

During the first lab, a number of students have asked me similar implementation questions about the controlling of 8 switches on the external board. Therefore, I have decided to make simple demo here based on controlling user push button on the based board. However, you can easily extend the functions to 8 switches control on the external board.

This demonstration shows you how to use the GPIO blocks in the STMicroelectronics STM32F4-Discovery library to control the user push-button (blue one) and the four LEDs on the STMicroelectronics STM32F4-Discovery board. The aim is to use blue button to turn it on and off the four LEDs. So once you have pressed the blue button, all LEDs will be turned on and once you have pressed to the same button again, all LEDs will be turned off.

As a brief introduction, there are four user LEDs and one user push button on the current board. The user push button and the LEDs are connected to GPIO pins. The blue user push button is connected to GPIOA port – pin 0. The four LEDs labeled on the board as LED3, LED4, LED5 and LED6 are connected to GPIOD port pin 13, pin 12, pin 14 and pin 15 respectively.

This tutorial will help to you :

  • Initialize LEDs on STM32F4-Discovery board,
  • Initialize the user button,
  • Driving and observing the orange, green and blue LEDs on the board,
  • Reading and using push button as a control input,
  • Introduction to multi-functional button,
  • De-bouncing.

Although this example briefly illustrated to you overall design about various features of implementation on STM32F4, I suggest to you that you can use External Interrupt in order to get the button state. Instead of controlling push button inside the infinite loop, you can efficiently control it over interrupt routines.

In order to implement the below code, I have used stm32f4_discovery.c source file which can be found on your ST directory. The only function here is TIMER3_Config which you can implement it whatever you want to do. Basically, I am just counting 1msec inside this timer interrupt. This function probably is available on my project page so you can download it separately.

Code : 

#define PRESSED_BUTTON_NONE        					0x00
#define PRESSED_BUTTON_USER        					0x01

int main(void)
{
	u8 MyKey = PRESSED_BUTTON_NONE;
	u8 MyUserKeyState = 0;

	TIMER3_Config();
	
	STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);
	STM_EVAL_LEDInit(LED3);
	STM_EVAL_LEDInit(LED4);
	STM_EVAL_LEDInit(LED5);
	STM_EVAL_LEDInit(LED6);
	while(1)
	{
	    /* Read Key */
    	    MyKey = Read_User_Button();
		if(MyKey == PRESSED_BUTTON_USER)
		{
			MyUserKeyState++;
			if(MyUserKeyState > 1)
				MyUserKeyState = 0;
		}
		//else if(MyKey == PRESSED_BUTTON_NONE)
		//{
		//}
		//Update LEDs
		if(MyUserKeyState == 0)
		{
			STM_EVAL_LEDOff(LED3);STM_EVAL_LEDOff(LED4);STM_EVAL_LEDOff(LED5);STM_EVAL_LEDOff(LED6);
		}
		else if(MyUserKeyState == 1)
		{
			STM_EVAL_LEDOn(LED3);STM_EVAL_LEDOn(LED4);STM_EVAL_LEDOn(LED5);STM_EVAL_LEDOn(LED6);
		}
	}
}

tU8 Read_User_Button(void)
{
	tU32 CurrentTimerVal;
	/* “User-Blue” button is pressed */
	if(STM_EVAL_PBGetState(BUTTON_USER))
	{
		CurrentTimerVal = coreTimer(0);	
		while(STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET);
		if((coreTimer(CurrentTimerVal) > 100))
			return PRESSED_BUTTON_USER; 	
		else
			return PRESSED_BUTTON_NONE;
	}
	else 
	{
		return PRESSED_BUTTON_NONE;
	}
}

Demo: 

SHARE :


error: Content is protected !!