This is another continue of STM32 tutorial (STM32F746 tutorial). I would like to show example code for turn on/off LED with button.
This example code is able to use for another microcontrollers by ST as STM32L100, L152, STM32F103, F407 and more.
LED is connected to PORT I, GPIO 1. Button is connected to PORT I, GPIO 11.
Other tutorials with STM32F7:
Tutorial: STM32F746 (STM32F7 Discovery) – how to turn on/off LED by button
Tutorial: STM32F746 (STM32F7 Discovery) – how to turn on LED (controlling GPIO)
STM32F746 discovery and AC6 (System Workbench for STM32)
How to begin with STM32 and why – tutorial
Button pinout is showed on these pictures:
The example code will be describe with comments.
main.h
/*
* main.c
*
* Created on: 14. 8. 2015
* Author: petus
* Website: https://chiptron.cz
*/
#ifndef MAIN_H_
#define MAIN_H_
#include “stm32f7xx.h”
#include “stm32f746xx.h”
#include “system_stm32f7xx.h”
#include
#include
#endif /* MAIN_H_ */
main.c
/*
* main.c
*
* Created on: 14. 8. 2015
* Author: petus
* Website: https://chiptron.cz
*/
#include “main.h”
int main(void)
{
//—-LED—-//
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOIEN; //Enable clock for PORT I
RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIOIRST; //Reset PORT I
RCC->AHB1RSTR &= ~RCC_AHB1RSTR_GPIOIRST;
GPIOI->MODER |= GPIO_MODER_MODER1_0; //Set mode register PIN 1, PORT I
GPIOI->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR1; //Set speed register PIN 1, PORT I
GPIOI->ODR |= GPIO_ODR_ODR_1; //Set output data register PIN 1, input PIN 11, PORT I
//—-GPIO—-//
GPIOI->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR11; //Set speed register PIN 11, PORT I
while(1)
{
if(!(GPIOI->IDR & GPIO_IDR_IDR_11)) // check PIN 11, PORT I, if PIN 11 isn’t High – button isn’t pressed
GPIOI->ODR |= GPIO_ODR_ODR_1; // turn on LED on PIN 1, PORT 1
else // button is pressed
GPIOI->ODR &= ~GPIO_ODR_ODR_1; // turn off LED on PIN 1, PORT 1
}
}
Be careful if you have LED and BUTTON on same PORT. Don’t reset peripheral again.
You can use this code for setting of OSPEEDR of PORT.
GPIOI->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR1 | GPIO_OSPEEDER_OSPEEDR11; // LED, Button
If you have LED on another PORT then BUTTON, you shall enable both peripherals (I mean enable e.g. PORTx and PORTy)
Project by AC6 System Workbench for STM32 is here.
Other tutorials with STM32F7:
Tutorial: STM32F746 (STM32F7 Discovery) – how to turn on/off LED by button
Tutorial: STM32F746 (STM32F7 Discovery) – how to turn on LED (controlling GPIO)
STM32F746 discovery and AC6 (System Workbench for STM32)
How to begin with STM32 and why – tutorial