从零搭建STM32开发环境(一)

最近有项目要用到STM32,感到很亲切,下面我把今天搭建keil-MDK开发环境的过程以及遇到的问题和解决方案分享出来,希望对大家有所帮助。

软件环境:keil4

硬件平台:STM32F103C8

STM32标准库版本:STM32F10x_StdPeriph_Lib_V3.5.0

步骤如下:

1、将标准库里面的CMSIS和STM32F10x_StdPeriph_Driver这两个文件夹拷贝到工程目录下。

2、打开keil4,创建新工程

从零搭建STM32开发环境(一)_第1张图片

   ①点击project菜单下的第一个选型,创建新工程,弹出对话框,将工程命名为key,即如下图,点击保存。

从零搭建STM32开发环境(一)_第2张图片

   ②接着弹出如下对话框,选择STM32F103C8。

从零搭建STM32开发环境(一)_第3张图片

  ③点击OK,紧接着出现如下提示:

从零搭建STM32开发环境(一)_第4张图片

④选择“是”。于是就会生成一个名为“startup_stm32f10x_md.s”的文件,并被组织在Source Group 1之下。到此一个工程创建完成。

从零搭建STM32开发环境(一)_第5张图片

3、将标准库文件导入工程中。

①点击如下图的按钮

②接着弹出如下对话框,最左边为工程目标,中间为工程的各个组,右边则为改组下面的文件,所以首先创建各个组,然后往各个组中添加文件。

从零搭建STM32开发环境(一)_第6张图片

添加完后如下,点击OK后工程下面就会多出CMSIS和STM32F10x_StdPeriph_Driver两项。

从零搭建STM32开发环境(一)_第7张图片

从零搭建STM32开发环境(一)_第8张图片

4、编译

①点击编译,会出现很多的错误提示:

Rebuild target 'Target 1'

assembling startup_stm32f10x_md.s...

compiling core_cm3.c...

compiling system_stm32f10x.c...

CMSIS\CM3\DeviceSupport\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling misc.c...

STM32F10x_StdPeriph_Driver\src\misc.c(24): error:  #5: cannot open source input file "misc.h": No such file or directory

compiling stm32f10x_adc.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_adc.c(23): error:  #5: cannot open source input file "stm32f10x_adc.h": No such file or directory

compiling stm32f10x_bkp.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_bkp.c(23): error:  #5: cannot open source input file "stm32f10x_bkp.h": No such file or directory

compiling stm32f10x_can.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_can.c(23): error:  #5: cannot open source input file "stm32f10x_can.h": No such file or directory

compiling stm32f10x_cec.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_cec.c(23): error:  #5: cannot open source input file "stm32f10x_cec.h": No such file or directory

compiling stm32f10x_crc.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_crc.c(23): error:  #5: cannot open source input file "stm32f10x_crc.h": No such file or directory

compiling stm32f10x_dac.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_dac.c(23): error:  #5: cannot open source input file "stm32f10x_dac.h": No such file or directory

compiling stm32f10x_dbgmcu.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_dbgmcu.c(23): error:  #5: cannot open source input file "stm32f10x_dbgmcu.h": No such file or directory

compiling stm32f10x_dma.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_dma.c(23): error:  #5: cannot open source input file "stm32f10x_dma.h": No such file or directory

compiling stm32f10x_exti.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_exti.c(23): error:  #5: cannot open source input file "stm32f10x_exti.h": No such file or directory

compiling stm32f10x_flash.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_flash.c(23): error:  #5: cannot open source input file "stm32f10x_flash.h": No such file or directory

compiling stm32f10x_fsmc.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_fsmc.c(23): error:  #5: cannot open source input file "stm32f10x_fsmc.h": No such file or directory

compiling stm32f10x_gpio.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_gpio.c(23): error:  #5: cannot open source input file "stm32f10x_gpio.h": No such file or directory

compiling stm32f10x_i2c.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_i2c.c(23): error:  #5: cannot open source input file "stm32f10x_i2c.h": No such file or directory

compiling stm32f10x_iwdg.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_iwdg.c(23): error:  #5: cannot open source input file "stm32f10x_iwdg.h": No such file or directory

compiling stm32f10x_pwr.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_pwr.c(23): error:  #5: cannot open source input file "stm32f10x_pwr.h": No such file or directory

compiling stm32f10x_rcc.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_rcc.c(23): error:  #5: cannot open source input file "stm32f10x_rcc.h": No such file or directory

compiling stm32f10x_rtc.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_rtc.c(23): error:  #5: cannot open source input file "stm32f10x_rtc.h": No such file or directory

compiling stm32f10x_sdio.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_sdio.c(23): error:  #5: cannot open source input file "stm32f10x_sdio.h": No such file or directory

compiling stm32f10x_spi.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_spi.c(23): error:  #5: cannot open source input file "stm32f10x_spi.h": No such file or directory

compiling stm32f10x_tim.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_tim.c(23): error:  #5: cannot open source input file "stm32f10x_tim.h": No such file or directory

compiling stm32f10x_usart.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_usart.c(23): error:  #5: cannot open source input file "stm32f10x_usart.h": No such file or directory

compiling stm32f10x_wwdg.c...

STM32F10x_StdPeriph_Driver\src\stm32f10x_wwdg.c(23): error:  #5: cannot open source input file "stm32f10x_wwdg.h": No such file or directory

Target not created

分析:上面这些错误提示都是因为编译器找不到头文件,因此必须告诉编译器这些头文件在哪。按照如下图的①②③④步操作就可以把inc文件的路径添加进来。

从零搭建STM32开发环境(一)_第9张图片

从零搭建STM32开发环境(一)_第10张图片

②再次点击编译,会出现如下错误:

Rebuild target 'Target 1'

assembling startup_stm32f10x_md.s...

compiling core_cm3.c...

compiling system_stm32f10x.c...

CMSIS\CM3\DeviceSupport\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling misc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_adc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_bkp.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_can.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_cec.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_crc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_dac.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_dbgmcu.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_dma.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_exti.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_flash.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_fsmc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_gpio.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_i2c.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_iwdg.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_pwr.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_rcc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_rtc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_sdio.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_spi.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_tim.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_usart.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

compiling stm32f10x_wwdg.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(96): error:  #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"

Target not created

解决这个错误的方法就是重新进入如下这个设置页面中,并在如下的Define栏中添加 STM32F10X_MD。

从零搭建STM32开发环境(一)_第11张图片

③再次点击编译,还会出现如下错误提示,也就是提示所有的库文件都缺失assert_param申明。

Rebuild target 'Target 1'

assembling startup_stm32f10x_md.s...

compiling core_cm3.c...

compiling system_stm32f10x.c...

compiling misc.c...

STM32F10x_StdPeriph_Driver\src\misc.c(99): warning:  #223-D: function "assert_param" declared implicitly

STM32F10x_StdPeriph_Driver\src\misc.c(117): warning:  #223-D: function "assert_param" declared implicitly

STM32F10x_StdPeriph_Driver\src\misc.c(159): warning:  #223-D: function "assert_param" declared implicitly

STM32F10x_StdPeriph_Driver\src\misc.c(178): warning:  #223-D: function "assert_param" declared implicitly

linking...

key.axf: Error: L6218E: Undefined symbol assert_param (referred from misc.o).

key.axf: Error: L6218E: Undefined symbol main (referred from rtentry2.o).

Target not created

其实,assert_param函数在文件“stm32f10x_conf.h”中,它是一个宏定义,用于对函数的输入参数进行检查。

从零搭建STM32开发环境(一)_第12张图片

 

在stm32f10x.h文件中搜索stm32f10x_conf.h,果然找到如下预编译代码,就是说如果定义了USE_STDPERIPH_DRIVER宏,那么stm32f10x_conf.h就包含该文件,否则不包含该文件,所以为了包含该文件,就必须定义USE_STDPERIPH_DRIVER宏。那么怎么定义呢?

有两种方法定义USE_STDPERIPH_DRIVER宏:

第一种方法就是标准宏定义:

直接在stm32f10x.h文件的起始位置定义USE_STDPERIPH_DRIVER。由于stm32f10x.h文件文件是只读属性,所以首先要把该文件修改文可读可写属性,然后通过编辑器打开,在文件头部添加代码#define USE_STDPERIPH_DRIVER。这样就算是定义了USE_STDPERIPH_DRIVER宏。

第二种方法就是偷懒的方法:

如下图所示,在下图的Define栏中添加USE_STDPERIPH_DRIVER,注意:STM32F10X_MD与USE_STDPERIPH_DRIVER之间是逗号隔开。

从零搭建STM32开发环境(一)_第13张图片

④再点击编译,依然提示如下错误:

Rebuild target 'Target 1'

assembling startup_stm32f10x_md.s...

compiling core_cm3.c...

compiling system_stm32f10x.c...

CMSIS\CM3\DeviceSupport\ST\STM32F10x\stm32f10x.h(8297): error:  #5: cannot open source input file "stm32f10x_conf.h": No such file or directory

compiling misc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(8297): error:  #5: cannot open source input file "stm32f10x_conf.h": No such file or directory

compiling stm32f10x_adc.c...

C:\Keil\ARM\Inc\ST\STM32F10x\stm32f10x.h(8297): error:  #5: cannot open source input file "stm32f10x_conf.h": No such file or directory

Target not created

从提示中可以看出,目录中根本就没有stm32f10x_conf.h这个文件,把这个文件添加进去。

从零搭建STM32开发环境(一)_第14张图片

⑤最后编译,是不是完美通过呢?很抱歉,依然出现如下提示:

从零搭建STM32开发环境(一)_第15张图片

是不是快崩溃了,不过没关系,这个问题才是最后的一个问题,错误提示工程中确实了名为main的函数,那我们创建一个文件,然后创建一个main函数就可以了。

⑥创建一个名为user的组,然后创建一个名为key.c的文件,并在这个文件中定义个名为main()的函数,然后把这个文件添加到user这个组中,即如下:

从零搭建STM32开发环境(一)_第16张图片

再次点击编译,OK,通过!

从零搭建STM32开发环境(一)_第17张图片

你可能感兴趣的:(STM32开发)