STM32F4 Discovery Board a Simple LED Turn-on PDF Print E-mail
Written by Monfared   
Friday, 03 April 2015 17:58

1. Open the New project and open the main.c file to start coding.

2. The Books window, displays all documents/information you may need about the software tool chain, your device and board. Double-click on Schematics (STM32F4-Discovery). Open the PDF file and go to the last page.

L1

 

 

As you can see the LEDs are connected to the GPIO PORTD.

You will turn on all LEDs which are connected to the PD12, PD13, PD14 and PD15.

 
L2

 

3. Every peripheral on the STM32Fxx microcontroller is clocked by an internal clock.
So you have to identify to which internal clock the GPIO PORTD is connected. So open the STM32F405/407 Data Sheet PDF file and go to the page 18 in the Device overview.

 
L3

 

As shown in the picture bellow the GPIO PORTD is clocked by AHB1 which its frequency can be set up to 168 MHz.

L4

 

4. You can use the functions in the reset and clock control (RCC) source file “stm32f4xx_rcc.c”, under the Std_PriphDriver group, to simply set the clock parameters for the GPIO PORT D.
Inside the stm32f4xx_rcc.c file search for the RCC_AHB1PeriphClockCmd function which is responsible for enabling or disabling the AHB1 peripheral clock. Copy this function into your main.c file and activate the GPIO PORTD.

L5

 

Apparently a full description of the function and its parameters (here two parameters: RCC_AHBPeriph: specifies the AHB1 peripheral to gates its clock and NewState: new state of the specified peripheral clock) are available which help you use the function correctly.

Copy this function to your main.c file as:

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

 

5. Now you must do the configurations for the GPIOD. You can use the functions in the GPIO source file “stm32f4xx_gpio.c”, under the Std_PriphDriver group, to simply set the port parameters for the GPIO PORT D.
Inside the stm32f4xx_gpio.c file search for GPIO_Init function which is responsible for initializing the GPIOx peripheral.

L6

 

Again a full description of the function and its parameters are available to help you use the function correctly.

As you can see in the @brief, before adding this function, one have to create a new GPIO_InitTypeDef structure called GPIO_InitStruct inside the main file.

Right click on GPIO_InitTypeDef and choose Go To Definition Of 'GPIO_InitTypeDef'.

L7

 

The associated header file stm32f4xx_gpio.h opens and you can see the structure’s definition inside it:

L8

 

This structure has five elements. All of them are mandatory to configure correctly the GPIOx.

GPIO_Pin:  pin 0/…./15/All, choose the pin numbers 12 | 13 | 14 | 15.

GPIO_Mode: Input/Output/Alternate function/Analog, choose the output.

GPIO_Speed: 2/25/50/100MHz, choose 50 MHz.

GPIO_OType: Push-Pull/ Open-Drain, choose push-pull.

GPIO_PuPd: No-Pull(float)/Pull-up/Pull-down, choose pull-up.

Note: Again you can right click on each structure (for example GPIO_pins_define) and choose Go To Definition Of … for the possible choices.


Finally you should have the bellow code in your main.c:

GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOD, &GPIO_InitStruct);

 

Note: All declarations (here GPIO_InitTypeDef GPIO_InitStruct;) must appear before executable statements, therefore must be at the beginning of the main function block.

 Note: You have to add the ‘&’ before GPIO_InitStruct because as written in the GPIO_Init function, the second parameter is a pointer to a GPIO_InitTypeDef structure.

 

6. To turn on the LED, you can easily use the GPIO_SetBits from the “stm32f4xx_gpio.c”.

L9

 

Note: To find a list of functions inside a source file, such as “stm32f4xx_gpio.c”, click on the functions window:

L10

 

Write the bellow code in the main.c:

GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);

 

7. Rebuild, Load and Run your first project.

L11

 

You can check your main file with the code bellow:

 

#include "stm32f4xx.h"                   // Device header
#include "RTE_Components.h"        // Component selection
#include "arm_math.h"                  // ARM::CMSIS:DSP
#include

int main(void)
{
    GPIO_InitTypeDef  GPIO_InitStruct;
    
    // Enable the GPIOD clock
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    
    // Configure the GPIOD
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOD, &GPIO_InitStruct);
    
    // GPIOD all LEDs on
    GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
        
    return(0);
}

Last Updated on Saturday, 23 January 2016 23:39