In this STM32F103ZE-SK tutorial we are going to see how to use the User button of the board by turning on and off the LCD screen.
The LCD screen is on the GPIO B peripheral and on the GPIO_Pin_5 port.
The User button is on the GPIO G and on the GPIO_Pin_8 port.

Explanation

We have:

  • PB5 -> LCD
  • PG8 -> User button

The lightToMe() function initializes both GPIO B and G, being respectively the LCD
screen and the User button.

First of all, we must enable the clock of each GPIO and after we fill the structure of each pin.
Then we set the LCD to Bit_SET (1) and when we release the User button, we set the value to Bit_RESET (0).
So the LCD is turned on, at the beginning, until the button is pressed.
Each time we push the User button, the light is switched off.
For the tutorial we use two defines (GPIO_LED and GPIO_BUT) to avoid repetition on the code.

Code

#include "stm32f10x.h"
#include "stxng.h"
#define GPIO_LED GPIO_Pin_5
#define GPIO_BUT GPIO_Pin_8
void lightToMe() {
	// initializing a structure
	GPIO_InitTypeDef GPIO_InitStructure;
	// enabling all GPIO clock
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);
	// setting the GPIO_Speed by default (2MHz)
	GPIO_StructInit(&GPIO_InitStructure);
	// setting the LCD pin
	GPIO_InitStructure.GPIO_Pin = GPIO_LED;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	// setting the USER BUTTON pin
	GPIO_InitStructure.GPIO_Pin = GPIO_BUT;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOG, &GPIO_InitStructure);
	// infinite loop
	while (1337) {
		if (GPIO_ReadInputDataBit(GPIOG, GPIO_BUT)) {
			GPIO_WriteBit(GPIOB, GPIO_LED, Bit_SET);
		} else {
			GPIO_WriteBit(GPIOB, GPIO_LED, Bit_RESET);
		}
	}
}
/**
* Main, what else?
*/
int main(void) {
	lightToMe();
	return 0;
}

Conclusion

You are now able to play with the LCD screen and the User button!
Good job, you’ve made it.