STM32_Starter Program LED

Publisher:cloudsousou6Latest update time:2018-12-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

***********************************************************************************************************************************************************


Development board: STM32


cpu         :STM32F103


Development environment: keil uVsion4


*************************************************************************************************************************************************************


Preface: After two days, I finally built the development environment and wrote the first entry program of 32. We know that the first thing to do when playing with a single-chip microcomputer development board is to light up the LED light. Because I have played with the arm development board before, I also learned STM32 according to this idea! Because there are a lot of reference materials on the Internet, I feel that the learning process is quite smooth! Learning 32 has library development and register development, because library development is more efficient, so


The rest of the STM32 learning journey will be based on the library development model!


Preliminary preparation:


    1. MDK installation (mkd453)


    2. J-LINK download and installation (Setup_JLinkARM_V410i.exe)


    3. J-LINK cable or serial cable (download program to development board)


1. New construction project


(1) Click project->NEW uVision Project to create a new project file. You can name the file as you like and save the file in the path you set.


(2) The next window is for selecting the company and chip model. I use ST's STM32F103VE, so select STM32F103VE and click OK.


 (3) Next, you will be asked whether to start the code into the project file. Here I use the ST library, so click "No" (usually you should click "No").


(4) The project is created successfully, but our project does not have any files. Next, add the required files manually.


(5) Copy the official STM32 official library to the folder under the path we set and create 5 new folders: USER, FWlib, CMSIS, Output, Listing


     *FWlib is placed in the inc and src folders under the \Libraries\STM32F10x_StdPeriph_Driver folder


     *USER放main.c stm32f10x_conf.h,stm32f10x_it.h和stm32f10x_it.c


     *CMSIS puts all folders under \Libraries\CMSIS\CM3,


(6) Return to the project. Under Target, select ADD Group to create STARTCODE, USER, FElib, CMSIS. The code to be placed in it is as shown in the figure:


   


(7) Set parameters output, Listing, c/c++, Debug, Utilities


 ( 8 ) Jlink download settings


Second, write main.c


#include "stm32f10x.h"

 

GPIO_InitTypeDef GPIO_InitStructure;

#define LED2_ON GPIO_SetBits(GPIOD,GPIO_Pin_3) 

#define LED2_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_3)

 

void RCC_Configuration(void);

void LED_Config(void);

void Delay(__IO uint32_t nCount);

 

void LED_Config(void)

{

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOD,ENABLE);

    GPIO_InitStructure.GPIO_Pin= GPIO_Pin_3;

    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; 

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOD,&GPIO_InitStructure);  

}

 

int main(void)

{

    RCC_Configuration();

  LED_Config();

  while(1)

{   

                     LED2_ON;

            Delay(0xAFFFF);

             LED2_OFF;

            Delay(0xAFFFF);

  

                }

}

 

void Delay(__IO uint32_t ncount)

{

     for(;ncount != 0;ncount--);

}

 

void RCC_Configuration(void)

{

     SystemInit();

}


3. Main program compilation, connection and debugging


4. Use J-link to download to the development board and run


Click "LOAD"


  

It was found that the LED light on the development board was flashing successfully. . .


5. Main.c Programming Analysis


First, we want to program to light up the LED, which is divided into several steps:


1. Initialization structure—GPIO_InitTypeDef type


typedef struct

{

  uint16_t GPIO_Pin;             /*!< Specifies the GPIO pins to be configured.

                                      This parameter can be any value of @ref GPIO_pins_define */

 

  GPIOSpeed_TypeDef GPIO_Speed;  /*!< Specifies the speed for the selected pins.

                                      This parameter can be a value of @ref GPIOSpeed_TypeDef */

 

  GPIOMode_TypeDef GPIO_Mode;    /*!< Specifies the operating mode for the selected pins.

                                      This parameter can be a value of @ref GPIOMode_TypeDef */

}GPIO_InitTypeDef;

2. Initialization library function—GPIO_Init()


void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

{

  uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;

  uint32_t tmpreg = 0x00, pinmask = 0x00;

  /* Check the parameters */

  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));

  assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  

  

/*---------------------------- GPIO Mode Configuration -----------------------*/

  currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);

  if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)

  { 

    /* Check the parameters */

    assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));

    /* Output mode */

    currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;

  }

     ......

}


3. Enable the peripheral clock

  

The clock PCLK2 used by GPIO adopts the default value of 72MHz. To enable the GPIOD pin, we usually call the RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE) function. If you want to enable other gpio pins at the same time, change the function parameter to the corresponding pin.


void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)

{

  /* Check the parameters */

  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));

  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)

  {

    RCC->APB2ENR |= RCC_APB2Periph;

  }

  else

  {

    RCC->APB2ENR &= ~RCC_APB2Periph;

  }

}

**************************************************************************************************************************************************


Note: Before turning on the peripheral clock, you must first configure the system clock SYSCLK. To configure SYSCLK, you need to set a series of clock sources, frequency multiplication, frequency division and other control parameters. These tasks are completed by the SystemInit() library function. When the chip is reset (including power-on reset), the startup_stm32f10x_hd.s assembly code will start running. This assembly code first calls SystemInit() and then enters the "__main" function in C language to execute. This is an initialization function of the C standard library. After executing this function, it will eventually jump to the "main" function interface in the user file and start running the main program.


***************************************************************************************************************************************************


4. Control I/O output high and low level


(1) Call GPIO_SetBits() function to control the output high level.


void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

{

  /* Check the parameters */

  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  assert_param(IS_GPIO_PIN(GPIO_Pin));

  

  GPIOx->BSRR = GPIO_Pin;

}

(2) Call GPIO_ResetBits() function to control the output low level.


void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

{

  /* Check the parameters */

  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  assert_param(IS_GPIO_PIN(GPIO_Pin));

  

  GPIOx->BRR = GPIO_Pin;

}

5. Fill in the GPIO_InitTypeDef structure


GPIO_InitStructure.GPIO_Pin= GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;


Of course, this should refer to the schematic diagram of the stm32 development board:


According to this schematic, we can change the configuration of LED_Config(void), GPIO_SetBits(), and GPIO_ResetBits() functions! Then we can light up the three LEDs.


Keywords:STM32 Reference address:STM32_Starter Program LED

Previous article:A brief analysis of assert_param function in STM32
Next article:stm32 bootloader update firmware restart IWDG independent watchdog

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号