stm32之keil开发环境搭建

简介: <ol style="font-size: 14px; line-height: 26px; word-wrap: break-word; margin: 0px; padding: 0px; list-style: none; font-family: 宋体, Arial;"><li style="word-wrap: break-word; margin: 0px; padding: 0p
  1. 只要按照下面的一步步来,绝对能从0开始建立一个STM32工程。不仅包括工程建立过程,还有Jlink设置方法。本文使用芯片为STM32F103CB。

    1 下载stm32F10x的官方库

http://www.st.com/web/en/catalog/tools/PF257890

 

  1. 2 新建工程

工程名设为stm32_demo,选择芯片型号为STM32F103B,如图,

因为下载的stm32库中有启动代码,所以这里选择"",不拷贝启动代码。

在工程文件下,新建Startup HeadersUserLibrariesCMSISListsOutput文件夹。

文件夹

用途

Startup

启动文件,Flash16~32Kb小容量,64~128Kb中容量,256~512Kb大容量

CMSIS

Cortex微控制器软件接口标准文件,该目录下文件适用所有Cortex系列

Libraries

存放stm32的驱动库文件

Headers

自定义的全局头文件

User

用户文件,我们把main.c放在该目录下

Lists

编译过程中产生的文件

Output

编译后输出文件,hex/bin等可执行属性的文件将保存在该目录下

至此,stm32的工程文件结构如下

 

  1. 3 库文件拷贝

把下载stm32库中文件拷贝到新建工程中

stm32F10x的官方库

工程

Libraries\STM32F10x_StdPeriph_Driver\inc 库头文件

Libraries\STM32F10x_StdPeriph_Driver\src 库源文件

Libraries

Project\STM32F10x_StdPeriph_Template\main.c

Project\STM32F10x_StdPeriph_Template\stm32f10x_it.c 中断函数文件

User

Project\STM32F10x_StdPeriph_Template\stm32f10x_it.h 中断函数头文件

Project\STM32F10x_StdPeriph_Template\stm32f10x_conf.h 配置文件

Headers

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm\* 启动文件

Startup

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\stm32f10x.h

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\ system_stm32f10x.c

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\system_stm32f10x.h

CMSIS

Libraries\CMSIS\CM3\CoreSupport\core_cm3.c Cortex-M3系统文件

Libraries\CMSIS\CM3\CoreSupport\core_cm3.h

CMSIS

文件拷贝完成后的工程文件目录结构如下:

 

  1. 4 将文件添加到工程

点击Keil右上角的工程分组按钮,在Group一列添加分组,分组和工程的文件名可以一一对应。

 

 

 


  1. 5 工程配置

点击右上角的工程配置按钮,弹出对话框,有多个选项卡,按照下面截图逐一配置。


  1. 6 编译链接

原来的main.c从库文件中拷贝过来的,把其中的内容都删除,添加最简单的main函数:

  1. #include "stm32f10x.h"  
  2. int main(void)  
  3. {  
  4.     while(1) {  
  5.    
  6.     }  
  7. }  

修改配置文件stm32f10x_conf.h,通过注释添加或取消注释删除需要的功能模块,这里根据自己需要配置。

编译

 

  1. 7 Jlink调试配置

接上Jlink及开发板,

至此,STM32的工程搭建和Jlink调试配置都设置好了。为了方便,在工程成中添加了includes.htypes.hgpio_bits.h等提供基本功能。

  1. 8 测试程序——LED流水灯程序

在工程文件目录下新建DriversDevices文件夹,

Drivers

存放stm32相关的驱动,比如:延时函数等

Devices

存放开发板上涉及的硬件设备相关代码

工程中File->New,新建下面的一些文件。

Drivers/delay.h

延时函数头文件

Drivers/delay.c

延时函数源文件

Devices/led.h

LED流水灯头文件

Devices/led.c

LED流水灯源文件

注:作为一种好的习惯,每个.c文件都应该有一个对应的.h文件。

添加代码:

led.c

  1. /* 
  2. * LED example 
  3. * Author : xiahouzuoxin 
  4. * Date : 2013.08 
  5. */  
  6. #include "LED.h"  
  7.    
  8. GPIO_InitTypeDef LED_InitStructure;  
  9.    
  10. /******************************************************************** 
  11. Function Name: Init_LED 
  12. Author : 夏侯佐鑫 
  13. Date : 2011-09-28 
  14. Description : 
  15. Inputs : None 
  16. Outputs : None 
  17. Notes : 
  18. Revision : 
  19. ********************************************************************/  
  20. void InitLED(void)  
  21. {  
  22. //使能PA端口时钟  
  23. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  
  24.    
  25.     //端口配置推挽输出  
  26.     LED_InitStructure.GPIO_Pin = LED1 | LED2 | LED3 | LED4;  
  27.     LED_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  28.     LED_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  29.     GPIO_Init(GPIOA, &LED_InitStructure);  
  30.    
  31.     //初始输出高电平  
  32.     GPIO_SetBits(GPIOA, LED1 | LED2 | LED3 | LED4);  
  33. }  
  34.    
  35. /******************************************************************** 
  36. Function Name: Flash_LED 
  37. Author : xiahouzuoxin 
  38. Date : 2011-09-28 
  39. Description : 
  40. Inputs : None 
  41. Outputs : None 
  42. Notes : 
  43. Revision : 
  44. ********************************************************************/  
  45. void FlashLED(void)  
  46. {  
  47. GPIO_SetBits(GPIOA, LED2 | LED3 | LED4);  
  48.     GPIO_ResetBits(GPIOA, LED1);  
  49.     delay_ms(500);  
  50. GPIO_SetBits(GPIOA, LED1 | LED3 | LED4);  
  51.     GPIO_ResetBits(GPIOA, LED2);  
  52.     delay_ms(500);  
  53. GPIO_SetBits(GPIOA, LED1 | LED2 | LED4);  
  54.     GPIO_ResetBits(GPIOA, LED3);  
  55.     delay_ms(500);  
  56. GPIO_SetBits(GPIOA, LED1 | LED2 | LED3);  
  57.     GPIO_ResetBits(GPIOA, LED4);  
  58.     delay_ms(500);  
  59. }  

led.h

  1. /* 
  2. * LED example 
  3. * Author : xiahouzuoxin 
  4. * Date : 2013.08 
  5. */  
  6. #ifndef __LED_H__  
  7. #define __LED_H__  
  8.    
  9. #include "../Headers/includes.h"  
  10.    
  11. #define LED1                      GPIO_Pin_4  
  12. #define LED2                      GPIO_Pin_5  
  13. #define LED3                      GPIO_Pin_6  
  14. #define LED4                      GPIO_Pin_7  
  15.    
  16. extern void InitLED(void);  
  17. extern void FlashLED(void);  
  18.    
  19. #endif  

delay.h

  1. #ifndef _DELAY_H  
  2. #define _DELAY_H  
  3.    
  4. #include "includes.h"  
  5.    
  6. extern void delay_ms(UINT32 ms);  
  7.    
  8. #endif  

delay.c

  1. #include "delay.h"  
  2.    
  3. /********************************************************************* DELAY_MS 
  4. * Discription : delay for 1 ms if ms=1, not accurate 
  5. * Author : xiahouzuoxin 
  6. * data : 2012-08-01 
  7. * inputs :    ms -- ms number 
  8. * outputs : 
  9. * Modified : 
  10. ********************************************************************/  
  11. void delay_ms(UINT32 ms)  
  12. {  
  13. int i = 0;  
  14.     int j = 0;  
  15.    
  16. for(i = 0; i < ms; i++)  
  17. {  
  18. for(j = 0; j < 8040; j++)  
  19. {  
  20. // Delay n ms  
  21. }  
  22. }  
  23. }  

includes.h

  1. #include "../Devices/LED.h"  

main.c

  1. #include "includes.h"  
  2.    
  3. int main(void)  
  4. {  
  5. InitLED();  
  6. while(1) {  
  7.      FlashLED();  
  8. }  
  9. }  

编译下载,运行,OK

相关文章
|
7月前
|
存储 编译器 C语言
STM32开发 -- Keil基本使用
STM32开发 -- Keil基本使用
303 0
|
9月前
STM32 Keil工程中使用abs函数报警告 warning: #223-D: function "abs" declared implicitly
STM32 Keil工程中使用abs函数报警告 warning: #223-D: function "abs" declared implicitly
459 0
|
安全 芯片
STM32在keil下开发时候文件options配置的一些小技巧
STM32在keil下开发时候文件options配置的一些小技巧
220 1
STM32在keil下开发时候文件options配置的一些小技巧
|
物联网 C语言 芯片
Stm32 向 keil 中添加库及应用程序 | 学习笔记
快速学习 Stm32 向 keil 中添加库及应用程序
595 0
Stm32 向 keil 中添加库及应用程序 | 学习笔记
|
芯片
STM32开发---Keil中使用printf 卡死的解决办法
方法1:使用 **use MicroLIB(微库)**,在魔术棒 / Targer 选项页中勾选use MicroLIB(下面代码的13~35行不用写) 方法2:不使用**use MicroLIB(微库)**,就要加入以下全部代码, 以支持printf函数
1630 0
STM32开发---Keil中使用printf 卡死的解决办法
STM32 KEIL不能输入仿真引脚端口error 65: access violation at 0x40021000 : no &#39;read&#39; permission
使用MDK自己创建一个STM32F103ZE核的项目 加入源码后编译,正常,在线仿真单步执行出现如下问题 error 65: access violation at 0x40021000 : no 'read' permission   发现是Debug里面的设置有问题  http://www.
1409 0
KEIL MDK STM32如何建立工程
  2.   3 4 5 6 7 QQ 463431476 8 9  
1072 0
|
芯片 内存技术
stm32开发之使用Keil MDK以及标准外设库创建STM32工程
<p align="justify" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(0, 0, 0);">通过上一节对标准外设库的介绍,想必各位读者对标准外设库已经有了基本的认识,然而由于标准外设库中文件众多,很多初学
2959 0