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.
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.
#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; }
You are now able to play with the LCD screen and the User button!
Good job, you've made it.
Comments
chiranth gowda (not verified)
Friday, December 22, 2017 - 6:29am
Permalink
Here how to add stxng.h
Here how to add stxng.h header file, can you please give me a link of header file.
Mi-K
Saturday, December 23, 2017 - 12:11am
Permalink
Hello chiranth,
Hello chiranth,
Check http://www.st.com/content/st_com/en/products/development-tools/hardware-development-tools/development-tool-hardware-for-mcus/debug-hardware-for-mcus/debug-hardware-for-stm32-mcus/st-link.html#sw-tools-scroll.
You should see ST-Link utility, try one of them, inside there are libraries for your STM32 board.
Add new comment