FIRMWARE DEVELOPER
Embedded Systems

Stm32F407 Discovery GPIO tutorial with LED on Board

 

____________________________________________________________________________________________________

In this tutorial we will try to explain the working of GPIO(Gernal Purpose Input Output) registers of Stm32f4 using CMSIS library provided by ST.

LED on STM32F4 are connected on pins of PORTD pins (PD12, PD13, PD14 and PD15). So we first need to enable the clock of PORTD. For that we need to call the following function.

After Enabling the clock of PORTD ,configure the register of GPIO by using CMSIS Library structure. So we need to configure following 5 options.

  1. GPIO_Pin:- Select the pin which are used
  2. GPIO_Mode:- Select the mode in which want to operate see below
  3. GPIO_OType:- Select the type of output
  4. GPIO_PuPd:- Select pull resistor
  5. GPIO_Speed:- Select pin speed

Every setting have options

  1. GPIO_Pin:-
                               we can select GPIO pins as follows:-

1. GPIO_Mode:-Type of mode of operation
GPIO_Mode_IN: Set pin as input
GPIO_Mode_OUT: Set pin as output
GPIO_Mode_AF: Set pin as alternating function ( like when used as SPI, USART, etc)
GPIO_Mode_AN: Set pin as analog (ADC or DAC)
2. GPIO_OType:- Type of Mode of output pins
GPIO_OType_PP: Output type is push-pull
GPIO_OType_OD: Output type is open drain
3. GPIO_PuPd:-  Select type of Pull-up/Pull-down
GPIO_PuPd_UP: Enable pull up resistor
GPIO_PuPd_DOWN: Enable pull down resistor
GPIO_PuPd_NOPULL: Disable pull resistor
4. GPIO_Speed:- Select GPIO speed
GPIO_Speed_100MHz
GPIO_Speed_50MHz
GPIO_Speed_25MHz
GPIO_Speed_2MHz

After That call the GPIO initialize function GPIO_Init()  present in stm32f4xx_gpio.h header file.

We need to initialize the PD12, PD13, PD14 and PD15 pins as output mode .

To toggle the LED present on stm32f4 discovery call the below function.

A sample example of blinking the led as follows

 

Dependency  to compile the program as follows:-

  1. stm32f4xx.h
  2. stm32f4xx_gpio.h
  3. stm32f4xx_rcc.h
  4. fm_stm32f4_led.h
  5. fm_stm32f4_delay.h

Led and delay library is given as follows:

   LED Library                                                                                               Delay Library

download (1)download (1)

 

  1. fm_stm32f4_led.h function

      3. fm_stm32f4_led.c function

 

     3. fm_stm32f4_delay.h function

4. fm_stm32f4_delay.c function

 

Complete project compiled in coocox is given below

download (1)

  1. Led blinking on stm32f4 Discovery Board