STM32F4 Discovery project with CMSIS library;

Modular Coding

  In this example we will use from Example-1 as a template, and explian another project in Keil uVision.

  We want to build a PWM in PD12 with "frequency = 1 kHz". We want to build PWM signal by set-and-resetting of a PD12 (not with using pin-out of a timer as you see in the datasheet of our micro-controller: TIM4-CH1).

  We want to change duty cycle of signal by pressing user button in STM32F4-Discovery Board. Because user button in  STM32F4-Discovery Board is connected to PA0 we need to set PA0 as input and because (as you can see in the User Manual of STM32F4-Discovery Board) PA0 is physically pull-down we need not to pull-down (or pull-up) PA0, but if we need to use a input pin as a input key we must pull-down (or pull-up) the pin for good performance of the the key. (designer and programmer maybe prefer pull-up or pull-down for a special application difference between to this and no-pull is when the pin is not connected to a voltage).

First time Keil uVision

  As you see in the above picture in STM32F4-Discovery Board PD12 is connected to Green-LED and with this exercise we can change the lightness of this LED.

1. Copy & past "LastName-StudentID-001", and rename it to "LastName-StudentID-002". Then Open "GPIO.uvprojx" using Keil uVision5.

2. Open "main.c" and change it to:

#include "stm32f4xx.h"
void Key_Init(void);
void LED_Init(void);
void usDelay(unsigned long msTime);
void Set_Reset_LED1(unsigned long Delay);

int main(void){
  unsigned long Delay=500;
  char flag=0;
  LED_Init();
  Key_Init();
  // Wait until PushButton is pressed PA0=1
  while(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0));
  
  while(1){
    Set_Reset_LED1(Delay%1000);
    if(Delay==1000) flag=1;
    if(Delay==0 ) flag=0;
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)){
      if(flag==0) Delay++;
      if(flag==1) Delay--;
    }
  }
}
//--------------------------------------------------------------------
void Key_Init(void){
  GPIO_InitTypeDef GPIO_InitStructure;
  /* GPIOA Periph clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  /* Configure PA0 in input mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//--------------------------------------------------------------------
void LED_Init(void){
  GPIO_InitTypeDef GPIO_InitStructure;
  /* 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);
}
//--------------------------------------------------------------------
void Set_Reset_LED1(unsigned long Delay){
  GPIO_SetBits(GPIOD, GPIO_Pin_12);
  usDelay(Delay);
  GPIO_ResetBits(GPIOD, GPIO_Pin_12);
  usDelay(1000-Delay);
}
//--------------------------------------------------------------------
void usDelay(unsigned long usTime){
  unsigned long i,j, nCount=0x29;
  for(i=0;i<usTime;i++)
    for(j=0;j<nCount;j++);
}

3. Press F7 (or click on ) to compile and make sure your code do not has any error. Then click on to download code to flash memory.

THE END.

Sharif University of Technology, Tehran, Iran