The most complete history of the introduction STM32 + environment to build anyone know everything looked

table of Contents

Chapter 4 MCU development ............................................. .................................................. .......... 2

4.1 Acquaintance STM32F407 chip ............................................. ............................................ 2

4.1.1 Introduction ............................................. SCM ................................................. 2

4.1.2 STM32F407 chip ............................................. ........................................... 2

4.2 development environment to build .............................................. .................................................. ..... 3

4.2.1 hardware platform ............................................. .................................................. ... 3

4.2.2 software development environment ............................................ ............................................... 3

4.2.3 Keil MDK software installation .......................................... ....................................... 3

4.2.4 Keil MDK new construction ........................................... ......................................... 7

4.2.4 J-LINK driver installation .......................................... ............................................. 11

4.3 GPIO port operation .............................................. .................................................. ...... 11

4.3.1 LED hardware schematics ........................................... ............................................ 11

4.3.2 STM32F407's GPIO port introduction .......................................... ............................ 11

4.3.3 STM32 standard peripherals library .......................................... ......................................... 12

4.3.4 Code Analysis ............................................. .................................................. . 12

4.3.5 code compiler download ............................................ ............................................. 15

Section 4.3.5 .............................................. .................................................. ....... 17

 

 

Chapter 4 MCU development

Embedded technology is one of the core technologies of the entire system of things. It corresponds to the perceived level of the brain, the sensing portion of the sensor layer is unified to achieve specific functions, the entire underlying portion of things.

Embedded development, the core part of the development of the chip. Currently there are single-chip embedded development, embedded linux and so on. Which microcontroller with its powerful, cost-effective, things in this industry accounted for most of the country.

4.1 acquaintance STM32F407 chip

This section describes and STM32F407 microcontroller chip.

4.1.1 SCM Introduction

SCM also known as single-chip microcontroller, it is not a completion of a certain logic function of the chip, but to a computer system integrated into a chip. Corresponds to a micro computer, and the computer compared to the lack of only the microcontroller I / O devices. Speaking in general terms: a chip to become a computer. Its small size, light weight, cheap, learning, application and development of facilities provided.

4.1.2 STM32F407 chip

The book selection ST (STMicroelectronics) launched STM32F407 series chip. It is ST (ST) introduced based on ARM Cortex ™ -M4 kernel performance microcontroller, which uses the NVM 90 nanometer technology and ART (adaptive real memory accelerator, Adaptive Real-Time MemoryAccelerator ™) .

According to market-related statistics, in 2017 the STM32 chip shipments totaled 1 billion. As one of the world's largest semiconductor companies, ST has a broad product line, sensors, power devices, automotive products and embedded processor solutions, play an important role in the ecology of things. Which MCU is one of the most important business, official data showed that in 2017, ST in general-purpose microcontroller market share of about 19 percent, according to the company has more than 800 models of STM32 products, more than 50,000 customers.

Figure 4.1 STM32F407 chip

STM32F407 use as a development mainly based on the following reasons:

( 1 high) price.

STM32F407VET6 model single purchase price is about $ 13, the bulk price will be a little lower.

( 2 ) large market, the development of information and more.

As the world's most popular chips currently on the market most of the companies are based STM32 family of chips to do development, corporate recruiters are also basic requirements will STM32. At the same time there are many online programs mature, relevant forums.

( 3 ) strong performance.

STM32F407 provide the operating frequency of 168 MHz Cortex ™ -M4 core performance (floating point unit). When executed from Flash memory, STM32F407 / 417 capable of providing 210 DMIPS / 566 CoreMark performance, and with the accelerator ART ST FLASH achieve zero wait state. DSP instructions and a Floating Point Unit expanded the scope of application of the product.

( 4 ) peripheral resources. rich

2 USB OTG (one of which supports HS)

Audio: dedicated audio PLL and two full-duplex I²S

The communication interface up to 15 (including six speeds up to 11.25 Mb / s of USART, 3 speeds up to 45 Mb / s to SPI, 3 th I²C, 2 and a two CAN SDIO)

Analog: 2 12-bit DAC, 3 speeds of 2.4 MSPS or 7.2 MSPS (interlaced mode) 12-bit ADC

Timer up to 17: 16 frequencies up to 168 MHz and 32-bit timers

It can be used to support Compact Flash, SRAM, PSRAM, NOR and NAND memory, static memory controller flexibility to easily expand storage capacity

Based on true random number generator Analog Electronic Technology

4.2 development environment to build

Hardware platform development environment is divided into two parts and software development environment.

4.2.1 hardware platform

Development STM32F407, we need the following hardware platforms:

  1. windows computer, a configuration recommended i5 or i5 above.
  2. STM32F407ZTG6 develop a board. All code in this book will STM32F407 run development board.
  3. J-LINK a. Mainly used for downloading programs.
  4. A router, two cable. Experimental later need to use the network communication.
  5. Power line, each of a serial port, providing a supply, the serial debug.

Figure 4.2 hardware platform

4.2.2 Software Development Environment

( 1 ) computer operating systems: Windows     

( 2 )       Keil MDK software. Write code, compile, download, simulation debugging.

( . 3 )       JLINK driving. For mounting jlink use driven to jlink to work properly.

( 4 ) computer serial debugging software.     

( 5 )       TCPUDP testing tools. Debugging for network communications.

More than developing software downloads can see Attachment part, this book will provide all the software to use. Convenience of the reader installed on your computer.

4.2.3 Keil MDK software installation

Keil MDK, also known as MDK-ARM, Realview MDK, I-MDK, uVision4 and so on. Keil MDK is to provide technical support and related services by the three domestic agents.

MDK-ARM provides a complete software development environment based on Cortex-M, Cortex-R4, ARM7, ARM9 processor device. MDK-ARM microcontrollers designed for applications, not only easy to use, yet powerful, able to meet the most demanding embedded applications. It is provided that includes a C compiler, macro assembler, linker, library manager and a powerful emulator debuggers, including a complete development program, through an integrated development environment (μVision) combines these parts.

( 1 ) Download

keil MDK can be downloaded to the official website: http://www2.keil.com/mdk5/ . Currently keil MDK should be charged, readers can also go online to find other versions.

( 2 ) Installation

After downloading, we will get an executable file mdk514.exe, where 514 is the version number. Double-click Run, and then the following screen appears, click Next >>

Figure 4.3, setup wizard

Users enter part of the agreement, I agree to put on all the .... checked, select Next >>

4.4 User interface protocol of FIG.

Here select the installation path of good software, click Next >>

FIG installation path selection interface 4.5

Here enter the user information, including user name, email and so on.

4.6 User Interface Information FIG.

输入信息后,点击Next之后会进入安装界面,等待安装完成即可。

图4.7 安装过程界面

安装完成后,会弹出如下提示框,我们把Show Release Notes 不要勾选。点击Finish即可

图4.8 安装完成界面

安装完成后,我们找到安装路径G:\Keil_v5\UV4 点击 UV4.exe 运行即可。

图4.9 启动界面

4.2.4 Keil MDK 新建工程

安装完Keil MDK后,我们来新建我们的第一个工程。

1)安装STM32F407 pack包。

点击下图红色框内的图标。

图4.10 主界面

选择File —— import,导入Keil.STM32F4xx_DFP.2.13.0.pack。该文件可以去官网下载,由于网速较慢,本书附录也会提供国内的下载链接。

图4.11 pack install窗口

选择打开即可。

图4.12 选择pack界面

2)新建工程

点击 Project —— New uVision project

图4.13 新建工程界面

选择工程路径,然后输入文件名为demo01,点击保存。

图4.14 保存工程界面

弹出此界面,由于本书选择的开发板芯片型号是 STM32F407ZGT6,故而我们选择STMicrolectronics——STM32F407——STM32F407ZG——STM32F407ZGTx。读者需要根据自己的开发板芯片型号选择。

图4.15 芯片型号选择界面

之后弹出如下界面,我们直接选择OK即可。

图4.16 MRTE界面

至此,我们的工程创建完成。

4.2.4 J-LINK驱动安装

本书使用的仿真器是J-LINK,需要在电脑上安装J-LINK驱动。读者可以自己去网上下载相关驱动,也可以直接使用本书附录的驱动文件。

该驱动安装比较简单,点击运行Setup_JLinkARM_V434.exe后一路选择Next 即可安装完成。本书在此不做赘述,读者自行安装即可。

4.3 GPIO口操作

在嵌入式系统中,经常需要控制许多结构简单的外部设备或者电路,这些设备有的需要通过CPU控制,有的需要CPU提供输入信号。对设备的控制,使用传统的串口或者并口就显得比较复杂,所以,在嵌入式微处理器上通常提供了一种“通用可编程I/O端口”,也就是GPIO。

4.3.1 LED硬件原理图

本章节将通过操作LED亮灭的方式,来实现对STM32F407的GPIO口操作。翻看开发板LED相关的硬件原理图,如图4.17所示。

图4.17 LED原理图

根据原理图以及LED灯的特性,我们可知:当LED0、LED1、LED2引脚输出低电平的时候,三个LED灯将会发光。反之输出高电平的时候,三个LED灯将熄灭。

而LED0、LED1、LED2三个引脚又分别对应到STM32F407芯片上的GPIOE_3、GPIOE_4、GPIOG_9。故而,LED的亮灭操作可以转化成STM32F407的引脚输出操作。

4.3.2 STM32F407的GPIO口介绍

1)分组

STM32F407有7组IO。分别为GPIOA~GPIOG,每组IO有16个IO口,共有112个IO口  通常称为 PAx、PBx、PCx、PDx、PEx、PFx、PGx,其中x为0-15。

2GPIO的复用

STM32F4 有很多的内置外设,这些外设的外部引脚都是与 GPIO 共用的。也就是说,一个引脚可以有很多作用,但是默认为IO口,如果想使用一个 GPIO内置外设的功能引脚,就需要GPIO的复用,那么当这个 GPIO 作为内置外设使用的时候,就叫做复用。    比如说串口  就是GPIO复用为串口

3GPIO的输入模式

GPIO_Mode_IN_FLOATING 浮空输入

GPIO_Mode_IPU 上拉输入

GPIO_Mode_IPD 下拉输入

GPIO_Mode_AIN 模拟输入

4GPIO的输出模式

GPIO_Mode_Out_OD 开漏输出(带上拉或者下拉)

GPIO_Mode_AF_OD 复用开漏输出(带上拉或者下拉)

GPIO_Mode_Out_PP 推挽输出(带上拉或者下拉)

GPIO_Mode_AF_PP 复用推挽输出(带上拉或者下拉)

4GPIO的最大输出速度

2MHZ  (低速)

25MHZ  (中速)

50MHZ  (快速)

100MHZ  (高速)

4.3.3 STM32标准外设库

STM32标准外设库是一个固件函数包,它由程序、数据结构和宏组成,包括了微控制器所有外设的性能特征。该函数库还包括每一个外设的驱动描述和应用实例,为开发者访问底层硬件提供了一个中间API,通过使用固件函数库,无需深入掌握底层硬件细节,开发者就可以轻松应用每一个外设。

因此,使用固态函数库可以大大减少开发者开发使用片内外设的时间,进而降低开发成本。每个外设驱动都由一组函数组成,这组函数覆盖了该外设所有功能。同时,STM32官方还给出了大量的示例代码以供学习。

STM32标准外设库可以到ST官网下载,也可以直接使用本书附录部分提供的下载好的STM32标准外设库。

使用keil MDK编写代码时,我们需要将STM32标准外设库添加到工程中去。这里推荐读者直接使用附录已经添加好的工程文件。

4.3.4 代码分析

1)工程文件结构

使用keil MDK的new project选项,打开LED demo代码的工程文件——01_demo.uvprojx

图4.18 LED工程代码

左边是工程的代码文件。

common —— 整个工程的公共代码部分,主要是实现delay函数等。

main         —— 工程的main函数部分,程序启动后的入口函数。我们从main.c 文件开始分析。

startup_config —— 汇编启动代码部分,我们后续再讲解。

stm32f4_fwlib —— STM32F407的标准外设库文件部分。

user                   —— 用户编写的代码部分。

2main函数分析

打开main.c文件,可以看到如下代码内容:

图4.19 main.c文件

void delay(int ms) 函数:通过使用两个for循环,实现延时等待。

int main(void) 函数:程序启动后的入口函数,调用LED_Init()函数实现GPIO口的初始化。

之后进入 while 循环,调用GPIO_WriteBit 使引脚输出高低电平。

其中,GPIO_WriteBit 是STM32标准外设库里面的函数,其函数原型如下:

void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)

参数列表:

GPIO_TypeDef* GPIOx            ——对应STM32F407的GPIO口分组,可填参数有GPIOA ~ GPIOG

uint16_t GPIO_Pin                   ——具体引脚编号,可填参数有GPIO_Pin_0 ~ GPIO_Pin_16。

BitAction BitVal                        ——控制引脚输出的状态,可填参数有Bit_SET 表示输出高电平,Bit_RESET 表示输出低电平。

3LED初始化部分

打开led.c文件,可以看到如下代码内容:

图4.20 led.c 文件

该文件直接使用STM32标准外设库的函数去初始化GPIO口。具体每一行代码如下:

GPIO_InitTypeDef  GPIO_InitStructure;      ——定义GPIO_InitTypeDef结构体的局部变量,用于后面的初始化GPIO引脚。

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOG, ENABLE);  

——打开GPIOE、GPIOG时钟。

以下代码的含义是将GPIOE 这一组的 3、4引脚设置为 输出模式、推挽输出、100MHz速度、上拉输出。对应了STM32F407的GPIO口介绍 这一小节。

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;        

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_UP;                  

GPIO_Init(GPIOE, &GPIO_InitStructure);                        

GPIO_SetBits(GPIOE, GPIO_Pin_3 | GPIO_Pin_4); ——使用STM32标准外设库函数,设置GPIOE这一组的 3、4 引脚输出高电平。

4.3.5 代码编译下载

1)编译

点击下图红色框的build按钮,开始编译。

图4.21 编译代码

编译结束后,可以看到下面的Build Output输出信息,则表示编译成功。

 

图4.22 编译成功

2)代码下载

代码下载需要使用j-link把开发板和电脑连接起来。之后点击keil MDK中的Options for Target按钮。

图4.23 Options for Target按钮

点击Debug,在下拉菜单中选择J-LINK/J-TRACE Cortex。之后点击右边的Setting按钮。

图4.24 Debug界面

 

弹出来的界面中,选择Flash Download,如果 Progamming algorithm内容是空的。则点击Add按钮。

图4.25 Flash Download界面

选择STM32F407xx Flash ,点击Add 即可。之后点击确认、OK,退出Options for Target回到keil MDK主界面。

点击红色框的download按钮即可下载程序到开发板运行。

图4.25 下载按钮

4.3.5 小节

本节通过使用LED灯的例子,讲解了STM32的GPIO口操作,同时介绍了STM32标准外设库文件的使用。让读者第一次接触STM32的代码开发,程序下载等操作。

 

发布了151 篇原创文章 · 获赞 57 · 访问量 22万+

Guess you like

Origin blog.csdn.net/aa120515692/article/details/104225027