How to turn on the LED on STM32F4 Discovery Board

Thread Starter

skyr6546

Joined Mar 22, 2019
73
Hi everyone,

I want to turn on a LED on my STM32F4-discovery board using atollic truestudio

Followed following step's to create the project

File -> New -> C Project
Project Name : LEDTOGGLE
EMBEDDEC C PROJECT
Next
Target -> STM32F4-> Boards->STM32F4_Discovery
Next
Next
Finish

generated code after a finish

Code:
/* Includes */
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"

/* Private macro */
/* Private variables */
/* Private function prototypes */
/* Private functions */

/**
**===========================================================================
**
**  Abstract: main program
**
**===========================================================================
*/
int main(void)
{
  int i = 0;

  /**
  *  IMPORTANT NOTE!
  *  The symbol VECT_TAB_SRAM needs to be defined when building the project
  *  if code has been located to RAM and interrupts are used.
  *  Otherwise the interrupt table located in flash will be used.
  *  See also the <system_*.c> file and how the SystemInit() function updates
  *  SCB->VTOR register.
  *  E.g.  SCB->VTOR = 0x20000000;
  */

  /* TODO - Add your application code here */

  /* Initialize LEDs */
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);

  /* Turn on LEDs */
  STM_EVAL_LEDOn(LED3);
  STM_EVAL_LEDOn(LED4);
  STM_EVAL_LEDOn(LED5);
  STM_EVAL_LEDOn(LED6);

  /* Infinite loop */
  while (1)
  {
    i++;
  }
}


/*
* Callback used by stm32f4_discovery_audio_codec.c.
* Refer to stm32f4_discovery_audio_codec.h for more info.
*/
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){
  /* TODO, implement your code here */
  return;
}

/*
* Callback used by stm324xg_eval_audio_codec.c.
* Refer to stm324xg_eval_audio_codec.h for more info.
*/
uint16_t EVAL_AUDIO_GetSampleCallBack(void){
  /* TODO, implement your code here */
  return -1;
}
Here is mine code :

Code:
/* Includes */
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"

GPIO_InitTypeDef  GPIO_InitStructure;


#define DELAY 1000000

void delay( unsigned long d)
{
  while(--d);
}


int main(void)
{
      /* GPIOD Periph clock enable */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
      /* Configure PD12 in output pushpull mode */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOD, &GPIO_InitStructure);

  /* Infinite loop */
  while (1)
  {
        GPIO_SetBits(GPIOD, GPIO_Pin_12);
        delay(DELAY);
        GPIO_ResetBits(GPIOD, GPIO_Pin_12);
        delay(DELAY);


  }
}


/*
* Callback used by stm32f4_discovery_audio_codec.c.
* Refer to stm32f4_discovery_audio_codec.h for more info.
*/
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){
  /* TODO, implement your code here */
  return;
}

/*
* Callback used by stm324xg_eval_audio_codec.c.
* Refer to stm324xg_eval_audio_codec.h for more info.
*/
uint16_t EVAL_AUDIO_GetSampleCallBack(void){
  /* TODO, implement your code here */
  return -1;
}
It compiles successfully but LED doesn't turn ON. I don't know what is wrong with this.

Can you help me ?
 

shteii01

Joined Feb 19, 2010
4,644
delay(DELAY) seems wrong. You are calling a function delay(), when you do that you should be passing a number in unsigned long format, but instead you are passing a string of characters (DELAY). That seems wrong, to me.
 

MrChips

Joined Oct 2, 2009
35,031
It would appear that you have not configured the System Clock properly.
Learn to use STM32CubeMX. It will make your life much easier.
 

mvas

Joined Jun 19, 2017
539
delay(DELAY) seems wrong. You are calling a function delay(), when you do that you should be passing a number in unsigned long format, but instead you are passing a string of characters (DELAY). That seems wrong, to me.
DELAY is not a "string of characters" type variable
DELAY will be compiled as a numeric constant.
 

mvas

Joined Jun 19, 2017
539
If you remove the LED from the Output Pin then does the program change the Voltage on the Output Pin ?

divide and conquer ...
Is the problem in your software or is the problem in your hardware ( LED schematic ) ?
 

MrChips

Joined Oct 2, 2009
35,031
Use STM32CubeMX to generate your code template.
Here is my code snippet for SystemClock_Config().
C:
void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
  HAL_StatusTypeDef ret = HAL_OK;

  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 432; 
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 9;
  ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  if(ret != HAL_OK)
  {
    while(1) { ; }
  }
  /* Activate the OverDrive to reach the 216 MHz Frequency */ 
  ret = HAL_PWREx_EnableOverDrive();
  if(ret != HAL_OK)
  {
    while(1) { ; }
  }
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 
  ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
  if(ret != HAL_OK)
  {
    while(1) { ; }
  } 
}
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
If you remove the LED from the Output Pin then does the program change the Voltage on the Output Pin ?

divide and conquer ...
Is the problem in your software or is the problem in your hardware ( LED schematic ) ?
I don't think the problem with hardware I have connected current limiting resistor with LED

I think the problem would be with code.
 

MrChips

Joined Oct 2, 2009
35,031
I already told you what is wrong with your code.
ARM chips are not like other MCUs. There are registers in every module which must be configured before using.

No, you do not have to use CubeMX.
 

MrChips

Joined Oct 2, 2009
35,031
Remove the delay(DELAY); instructions and single step your program.

When I get a chance I will test your code, but not today.
 

MrChips

Joined Oct 2, 2009
35,031
Did the eval code work?
Did you try adding the initialization code to your code?
C:
/* Initialize LEDs */
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);

  /* Turn on LEDs */
  STM_EVAL_LEDOn(LED3);
  STM_EVAL_LEDOn(LED4);
  STM_EVAL_LEDOn(LED5);
  STM_EVAL_LEDOn(LED6);
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
Did the eval code work?
Did you try adding the initialization code to your code?
yes but LED doesn't turn ON

Code:
/* Includes */
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"

GPIO_InitTypeDef  GPIO_InitStructure;


#define DELAY 1000000

void delay( unsigned long d)
{
  while(--d);
}


int main(void)
{
      /* GPIOD Periph clock enable */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
      /* Configure PD12 in output pushpull mode */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOD, &GPIO_InitStructure);

      /* Initialize LEDs */
      STM_EVAL_LEDInit(LED3);
      STM_EVAL_LEDInit(LED4);
      STM_EVAL_LEDInit(LED5);
      STM_EVAL_LEDInit(LED6);

      /* Turn on LEDs */
      STM_EVAL_LEDOn(LED3);
      STM_EVAL_LEDOn(LED4);
      STM_EVAL_LEDOn(LED5);
      STM_EVAL_LEDOn(LED6);

  /* Infinite loop */
  while (1)
  {
        GPIO_SetBits(GPIOD, GPIO_Pin_12);
        delay(DELAY);
        GPIO_ResetBits(GPIOD, GPIO_Pin_12);
        delay(DELAY);


  }
}


/*
* Callback used by stm32f4_discovery_audio_codec.c.
* Refer to stm32f4_discovery_audio_codec.h for more info.
*/
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){
  /* TODO, implement your code here */
  return;
}

/*
* Callback used by stm324xg_eval_audio_codec.c.
* Refer to stm324xg_eval_audio_codec.h for more info.
*/
uint16_t EVAL_AUDIO_GetSampleCallBack(void){
  /* TODO, implement your code here */
  return -1;
}
 

MrChips

Joined Oct 2, 2009
35,031
I ran your original program in post #1 and it runs fine.

Are you sure that you are debugging the program (F11) followed by Resume (F8)?
 

MrChips

Joined Oct 2, 2009
35,031
That is correct.
However, there is already on the board a green LED (LD4) being driven from PD12. You do not need to add another LED to the circuit.
 

be80be

Joined Jul 5, 2008
2,395
I have that board it takes a whole lot more code then posted you have to set a bunch of stuff up the gpio the clock.
I played with it a time or two it in the box now.
 

MrChips

Joined Oct 2, 2009
35,031
It would be best for you to start with a working example.

Go to
File>Import...>Example Projects>Download new example projects from TrueSTORE
Next
STMicroelectronics>STM32F4_Discovery>STM32F4_Discovery_IO_Toggle

You can try this example first.
After you get it working, replace the main( ) function with your own.
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
It would be best for you to start with a working example.

Go to
File>Import...>Example Projects>Download new example projects from TrueSTORE
Next
STMicroelectronics>STM32F4_Discovery>STM32F4_Discovery_IO_Toggle
Thank you
Example project and my first code are running successfully Now

I am getting error for LED and Button Code

Code:
/* Includes */
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"


int main(void)
{
      /* GPIOD Periph clock enable */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

      GPIO_InitTypeDef  GPIO_InitStructure;


      /* Configure pin_A0 as input */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_Init(GPIOD, &GPIO_InitStructure);

      /* Configure PD12 in output */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOD, &GPIO_InitStructure);

  /* Infinite loop */
  while (1)
  {
      if (GPIO_ReadPin(GPIOD, GPIO_PIN_0))
          {
                 GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
          }

      else
          {
                  GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
          }


  }
}



void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){
  /* TODO, implement your code here */
  return;
}


uint16_t EVAL_AUDIO_GetSampleCallBack(void){
  /* TODO, implement your code here */
  return -1;
}
Following error need to correct

..\src\main.c: In function 'main':
..\src\main.c:32:8: warning: implicit declaration of function 'GPIO_ReadPin' [-Wimplicit-function-declaration]
if (GPIO_ReadPin(GPIOD, GPIO_PIN_0))
^~~~~~~~~~~~
..\src\main.c:32:28: error: 'GPIO_PIN_0' undeclared (first use in this function)
if (GPIO_ReadPin(GPIOD, GPIO_PIN_0))
^~~~~~~~~~
..\src\main.c:32:28: note: each undeclared identifier is reported only once for each function it appears in
..\src\main.c:34:15: warning: implicit declaration of function 'GPIO_WritePin' [-Wimplicit-function-declaration]
GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
^~~~~~~~~~~~~
..\src\main.c:34:36: error: 'GPIO_PIN_12' undeclared (first use in this function)
GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
^~~~~~~~~~~
..\src\main.c:34:49: error: 'GPIO_PIN_RESET' undeclared (first use in this function)
GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
^~~~~~~~~~~~~~
..\src\main.c:39:50: error: 'GPIO_PIN_SET' undeclared (first use in this function)
GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
 
Top