Tutorial 5: Interfacing button

In this tutorial a button was interface with STM32F3 discovery board. User button attached to Pin A0 was used for this purpose.

A new project was created and Libraries from repository was added as same as LED blinking project. Same libraries were added in this case also.

Then following program was entered in IDE.

#include <stm32f30x.h>
#include <stm32f30x_rcc.h>
#include <stm32f30x_gpio.h>

int main(void)
{
	unsigned int input_val;
	//volatile unsigned int i=0;

	//Enable clock for both GPIOE and GPIOA
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOA, ENABLE);

	GPIO_InitTypeDef GPIO_InitStruct;

	//configuration for led connected to PE13
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOE, &GPIO_InitStruct);

	//configuration for user button connected to PA0
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA, &GPIO_InitStruct);

    while(1)
    {
    	input_val = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
    	if (input_val ==1)
    		GPIO_SetBits(GPIOE,GPIO_Pin_13);
    	else {
    		GPIO_ResetBits(GPIOE,GPIO_Pin_13);
		}
    }
}

 

Most of the lines in this program was discussed in LED blinking project. In this program RCC_AHBPeriphClockCmd method was used to enable clock for two peripherals GPIOE and GPIOA at the same time. GPIO_mode of pin A0 was configured as input in this case. In initialization part everything else is same as previous blinking project.

GPIO_ReadInputDataBit() method reads the specified input port pin and return value as 1 or zero.

The program should light on the LD10 led connected to pin E13 when user button on board is pressed. Otherwise it should be off.

Now compile the program and flash into board and run.

Example 2 simple counter application using button and LEDs.

In this application a counter was created using button. Project was created as mentioned in previous example and same libraries was included into project.

Following code was entered in IDE.

#include <stm32f30x.h>
#include <stm32f30x_rcc.h>
#include <stm32f30x_gpio.h>

GPIO_InitTypeDef GPIO_InitStruct;

int main(void)
{
	unsigned int count=0;
	unsigned int display_count=0;
	unsigned int i=0;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOA, ENABLE);

	//configuration for 8 leds connected to PORTE
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;	//which pins to setup, seperated by |
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOE, &GPIO_InitStruct);

	//configuration for user button connected to PA0
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA, &GPIO_InitStruct);

	GPIO_Write(GPIOE, 0x00);

    while(1)
    {
    	if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==1)				//is the pushbutton pressed (input = 0)
    	    {

    	       count = count + 1;         //increment count
    	       display_count = count << 7;
    	       for ( i = 0; i < 500000; ++i) ;

    	    }
    	GPIO_Write(GPIOE, display_count);  //display count on LEDs
    }
}

 

In this application LEDs were used to display the count. And new function GPIO_Write() was used to set the value to GPIO pins. On the board LEDs are connected to pin8 – pin15 in port E. Therefore value was shifted by 7 to display using LEDs.

Here a simple de-bounce was used to avoid instant increment that can happen on the event of pressing the button. But pressing the for long time will crease the value.

Compile the code and try it yourself.

Watch the video below