STM32 one step by step to light up the LED

Publisher:bdwhscLatest update time:2016-12-01 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Since I got the stm32, I have always wanted to get started quickly, so I have been browsing various forums and learning from the blogs of various experts. As the saying goes, haste makes waste, and you can't eat hot tofu in a hurry! Is that right? 

Finally, I decided to use the library on the st official website. According to the experts, using the library can help you get started quickly. It seems to be true. The tutorials are so well written that you can just use them. But there are so many libraries. Which one should I choose? My God! To be honest, I was tossed around by these libraries! Well, I finally admitted that I used the v3.4 library, which is very convenient!

Let's get down to business and light up the LED.

Hardware: Red Bull development board, STM32F103ZET6 (144 package).

Software:  RealView MDK 4.12

stm32 firmware library: v3.4 Attached is the library I organized myself:  V3.4_clean.rar   

I sorted it out according to the official website library and created a new project template as shown below: (Main reference article "Using STM32 V3.4 library in Keil MDK+ environment.pdf")  Using STM32V3.4 library in KeilMDK+ environment.pdf   

As shown in the figure: Create a new directory 01_ProLed. It is recommended to put it in the English path to avoid unnecessary trouble. Unzip the above library v3.4 to this directory, and then create a new project directory to store the project.

Description:
CMSIS: The lowest level interface. StartUp: System startup file. StdPeriph_Lib: STM32 peripheral device driver file. Project: Project file. User: User file. New project steps: 300 words are omitted here.

Brief description:

1.core_cm3.c/core_cm3.h 
This file is the source file and header file of the kernel access layer. Most of the code is written in assembly language. I don't know much about it online. --Excerpt from "Using STM32 V3.4 Library in Keil MDK+ Environment"

2. stm32f10x.h 
is the header file of the peripheral access layer. It is one of the most important header files. Just like reg51.h in 51. For example, it defines what kind of CPU capacity the CPU is, interrupt vectors, etc. In addition to these, this header file also defines structures related to peripheral registers, such as:

  1. typedef struct

  2. {

  3.   __IO uint32_t CR;

  4.   __IO uint32_t CFGR;

  5.   __IO uint32_t CIR;

  6.   __IO uint32_t APB2RSTR;

  7.   __IO uint32_t APB1RSTR;

  8.   __IO uint32_t AHBENR;

  9.   __IO uint32_t APB2ENR;

  10.   __IO uint32_t APB1ENR;

  11.   __IO uint32_t BDCR;

  12.   __IO uint32_t CSR;


  13. #ifdef STM32F10X_CL 

  14.   __IO uint32_t AHBRSTR;

  15.   __IO uint32_t CFGR2;

  16. #endif /* STM32F10X_CL */ 


  17. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) 

  18.   uint32_t RESERVED0;

  19.   __IO uint32_t CFGR2;

  20. #endif /* STM32F10X_LD_VL || STM32F10X_MD_VL || STM32F10X_HD_VL */ 

  21. }RCC_TypeDef;

Contains so many register definitions, so in the application file (such as the main source file you write yourself), you only need to include
stm32f10x.h, instead of the previous firmware library that needs to include the stm32f10x_conf.h header file. --Excerpt from "Using the STM32 V3.4 Library in the Keil MDK+ Environment"

3.system_stm32f10x.c/h 
This header file can also be called the header file and source file of the peripheral access layer. In this file, you can define the system clock frequency,
the frequency of the low-speed clock bus and the high-speed clock bus. The most critical function is SystemInit(), which will be introduced in detail later. In short, these
two files are the focus of the new firmware library. With them, the initialization work of using stm32 is greatly simplified. --Excerpt from "Using STM32 V3.4 Library in Keil MDK+ Environment"

4. The contents of the stm32f10x_conf.h 
file are the same as those of the V2 version of the library. Uncomment the peripherals that need to be used. For example, if you need to use the GPIO function but not the SPI function, you can do this. --Excerpt from "Using the STM32 V3.4 Library in Keil MDK+ Environment"

  1. #include "stm32f10x_gpio.h"

  2. /*#include "stm32f10x_spi.h"*/

5. There is no need to say more about the main.c 
file, you can write it yourself. --Excerpt from "Using STM32 V3.4 Library in Keil MDK+ Environment" 6. The two files
 
stm32f10x_it.c/h 
contain the stm32 interrupt function. Not all interrupt entry functions are written in the source file and header file, but
only a few abnormal interrupts of the ARM core are written. Other interrupt functions need to be written by the user. --Excerpt from "Using STM32 V3.4 Library in Keil MDK+ Environment"

OK, let’s start writing code.

Since the 3.4 library has already set the clock when it starts (which will be described later), we only need to set the corresponding GPIO.

Check the hardware connection:

Come light up PF6.

Create led.c and led.h and add them below User Code.

led.h

  1. #ifndef _LED_H_

  2. #define _LED_H_


  3. void Delay(uint32_t times);

  4. void LedInit(void);


  5. #endif

led.c

  1. #include "stm32f10x.h"


  2. /****************************************************** *************************


  3. * Function name: LedInit(void)


  4. * describe :


  5. * Input: None


  6. * Output: None


  7. * Return: None


  8. *************************************************** ************************/


  9. void LedInit(void)

  10. {

  11.     GPIO_InitTypeDef GPIO_InitStructure; 


  12.     /*Initialize GPIOF's Pin_6 as push-pull output*/

  13.     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;

  14.     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

  15.     GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

  16.     GPIO_Init(GPIOF,&GPIO_InitStructure);

  17.     

  18. }


  19. /****************************************************** *************************


  20. * Function name: Delay(uint32_t times)


  21. * Description: Delay function


  22. * Input: uint32_t times


  23. * Output: None


  24. * Return: None


  25. *************************************************** ************************/

  26. void Delay(uint32_t times)

  27. {

  28.   while(times--)

  29.   {

  30.      uint32_t i;

  31.      for (i=0; i<0xffff; i++)

  32.      ;

  33.   }

  34. }

Just add LED initialization and lighting and closing in main.c.

  1. /*!< At this stage the microcontroller clock setting is already configured, 

  2.        this is done through SystemInit() function which is called from startup

  3.        file (startup_stm32f10x_xx.s) before to branch to application main.

  4.        To reconfigure the default setting of SystemInit() function, refer to

  5.        system_stm32f10x.c file

  6.      */ 


  7.   /* Add your application code here

  8.      */


  9.     /*Initialize GPIOF clock*/

  10.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE); 


  11.     LedInit();

  12.     

  13.   /* Infinite loop */

  14.   while (1)

  15.   {

  16.        /*Turn off LED1*/

  17.         GPIO_SetBits(GPIOF,GPIO_Pin_6);

  18.         /*Delay*/

  19.         Delay(50);

  20.         /*Turn on LED1*/

  21.         GPIO_ResetBits(GPIOF,GPIO_Pin_6);

  22.         /*Delay*/

  23.         Delay(50);    

  24.   }

Study this code carefully, it is actually very simple, refer to "Using STM32 V3.4 Library in Keil MDK+ Environment". I would like to draw your attention to the previous English comment, what does this English comment mean. "Before running the main function, the system clock has completed the initialization work. Before the main function, the SystemInit function is run by calling the startup code, and this function is located in system_stm32f10x.c". According to the prompts in the text, we go back to system_stm32f10x.c to see how SystemInit initializes the system. The system clock frequency is defined at the beginning of system_stm32f10x.c. From the following code, we can see that the system frequency is
defined  as 72MHZ, which is also the frequency of most STM32s when they are running.

  1. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)

  2. /* #define SYSCLK_FREQ_HSE HSE_VALUE */

  3.  #define SYSCLK_FREQ_24MHz 24000000

  4. #else

  5. /* #define SYSCLK_FREQ_HSE HSE_VALUE */

  6. /* #define SYSCLK_FREQ_24MHz 24000000 */ 

  7. /* #define SYSCLK_FREQ_36MHz 36000000 */

  8. /* #define SYSCLK_FREQ_48MHz 48000000 */

  9. /* #define SYSCLK_FREQ_56MHz 56000000 */

  10. #define SYSCLK_FREQ_72MHz 72000000

  11. #endif

Then, according to this macro definition, the program tries to initialize the system clock to 72MHz. The code is a bit lengthy, so I won't list it here. In
the SystemInit function, the SetSysClock function is called. If the clock frequency is set to 72MHZ, SetSysCloc calls
the SetSysClockTo72 function. This function is very similar to the RCC_Configuration in each example in the V2 firmware library. It mainly
completes the allocation of the external clock to the system clock after multiplying it by 9. The APB1 clock and APB2 clock are obtained by dividing the system clock. The key code is as follows:

  1. /* HCLK = SYSCLK */

  2.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;

  3.       

  4.     /* PCLK2 = HCLK */

  5.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;

  6.     

  7.     /* PCLK1 = HCLK */

  8.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;

From the above analysis, we can see that SystemInit does not need to be called by the user, and the startup code will be executed automatically, which is equivalent to missing most of the content of
the RCC_Configuration function. Please note that it is most of the content, not all of it. However, please pay special
attention to the peripherals used. You should still enable the clock of the peripherals as soon as possible. Don't forget a sentence like this.

    /*Initialize GPIOF clock*/
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);

-------------------------------------------------- --------------------------------------------------

If not set correctly, compilation may fail:

1. Set up macros. Refer to another article: http://blog.chinaunix.net/space.php?uid=20788517&do=blog&id=296932

2. Set the header file directory: Add all the .h directories. One more thing to note is that you do not need to load header files in the project. The compiler will automatically search and link to the corresponding directory according to the settings.

Pictures speak louder than words:

Source code:  ProLed.rar   

In addition: Since the official website library is used, you often need to find files or keywords in the project. At this time, you can use the Source Insight tool to assist in reading the code. Its Lookup reference is very useful. You can create another directory Source Insight under the project directory. It is more troublesome, but it is practical!


Keywords:STM32 Reference address:STM32 one step by step to light up the LED

Previous article:S3c2440 Hardware Part 2: SDRAM
Next article:Research on STM32 clock control RCC

Recommended ReadingLatest update time:2024-11-15 14:34

STM32 MDK common errors and solutions
1, warning Deprecated declaration/function/ - give arg types Solution: If the function has no parameters, add "void" in the brackets. 2. USER\main.c(39): warning: #223-D: function "function" declared implicitly Function not declared Solution: Be sure to declare the function before using it   3,warning: #1-D: last li
[Microcontroller]
STM32 sleep mode low power consumption (stop mode)
At present, stm32 is very popular, so this article discusses the low power mode of stm32. Let's go to the manual first!  This is an English document that is not easy to understand, right? Let's look at the Chinese document below!  I compared STM32F0 and STM32F1. Both enter low power consumption in the same way. The
[Microcontroller]
STM32 sleep mode low power consumption (stop mode)
STM32 Light up LED
When learning a new processor, the first program must be to light up the LED. It allows us to quickly and clearly understand the program structure of a processor. Learning 32 is no exception. First, the first program is to light up the LED. There are many kinds of programs to light up the LED. Here we use the library
[Microcontroller]
STM32 Light up LED
Detailed explanation of ucosii transplantation on stm32 5
    Detailed explanation 1-4 has introduced the transplantation process. The next task is to verify whether the transplantation is ok and how to develop applications based on the transplanted ucosii. The former problem can be said to be a special case of the latter problem. Generally, we will create two simple tasks to
[Microcontroller]
Discussion on task switching in uCOS in STM32
   Under the STM32 platform, the uCOS V291 core was ported. Then a porting file was downloaded from the Internet: os_cpu_c.c os_cpu_asm.asm     I have not looked at the specific implementation of the task switching process in great detail, but I have a general understanding of it.     When OSCtxSw() or OSInt
[Microcontroller]
STM32-IIC analog slave mode
Here we are talking about simulated IIC, not hardware IIC. Why use software simulated IIC instead of hardware IIC? In addition to the problems of ST's IIC module itself, it is also because hardware IIC is not convenient to transplant and cannot be used universally in different MCUs; and some projects require switching
[Microcontroller]
Using the built-in high-speed ADC of STM32 to realize a simple oscilloscope
Making a digital sampling oscilloscope has been my long-standing wish, but after all, this goal is quite difficult, and there are too many aspects involved, such as analog front-end circuit, high-speed ADC, microcontroller, CPLD/FPGA, communication, host computer program, data processing, etc. It is not something that
[Microcontroller]
Design of wireless communication module based on STM32 and SIM900A
  In order to design a module that can realize SMS sending and receiving and wireless data transmission, this paper uses the mainstream product STM32 with ARM Cortex-M3 core as the main control chip and SIM900A of SIMCom as the communication chip. After consulting a large number of relevant literature and data manuals
[Microcontroller]
Design of wireless communication module based on STM32 and SIM900A
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号