STM32第一课(Keil, Type, macro)

keil中建立工程

在目标文件夹下,新建四个文件夹,
core, hallib, obj, user.
在keil中,project->new uvision project,定位到建立的user文件夹,这里面,存放工程以及自己建立的源文件。
选择器件为stm32f407zgtx,然后下一步,
如果不需要配置runtime,选择cancel。
在keil中,project显示为树状结构了。

拷贝驱动文件,从cubemx中找到STM32Cube_FW_F4_V1.24.1\Drivers\STM32F4xx_HAL_Driver,
将inc文件夹和src文件夹,拷贝到hallib文件夹中。

拷贝核心启动文件,从cubemx中找到STM32Cube_FW_F4_V1.24.1\Drivers\CMSIS\Device\ST\STM32F4xx\Source\Templates\arm,
将startup_stm32f407xx.s复制到core下面。

拷贝核心头文件,从cubemx中找到
STM32Cube_FW_F4_V1.24.1\Drivers\CMSIS\Include,
将cmsis_armcc.h,cmsis_armclang.h,
cmsis_compiler.h, cmsis_version.h,
mpu_armv7.h, core_cm4.h,复制到core下面。

拷贝关键用户头文件,从cubemx中找到
STM32Cube_FW_F4_V1.24.1\Drivers\CMSIS\Device\ST\STM32F4xx\Include ,
将stm32f4xx.h, system_stm32f4xx.h ,stm32f407xx.h ,复制到user下面。

拷贝模板文件,从cubemx中找到
STM32Cube_FW_F4_V1.24.1\Projects\STM32F4-Discovery\Templates ,
从inc中,拷贝stm32f4xx_it.h, stm32f4xx_hal_conf.h 和main.h。
从src中,拷贝 system_stm32f4xx.c,stm32f4xx_it.c, stm32f4xx_hal_msp.c 和 main.c 。

建立实用函数文件,
例如delay.h,delay.c,
sys.h,sys.c,
usart.h, usart.c。

然后,管理工程源码。
project->manage project items,可以配置工程中包含的源代码。
修改target name。
增加4个source group。core,hallib,user,system。
分别为group添加文件。
对于hallib,添加hallib/src 下面的各种外设文件,需要什么外设,就添加什么外设的文件。例如
stm32f4xx_hal_gpio.c。一般为了方便,通常全部添加。
对于core,由于需要添加的是s文件和h文件,所以需要使用all file方式添加文件。

然后,管理工程编译配置。
project->option for target “XXX”,打开配置选项卡。

在c/c++选项卡下,
配置include path,选择include path后面的"…",依次添加core,hallib/inc,system, user几个文件夹,这几个文件夹,都包含有h文件。所以需要给它们配置路径。
keil会自动将绝对路径转换成相对于uvproj的相对路径。

配置全局宏开关,在preprocessor symbols框中,
define USE_HAL_DRIVER,STM32F407xx。
注意这里是两个标识符USE_HAL_DRIVER 和 STM32F407xx, 他们之间是用逗号隔开的。

在output选项卡下,
配置object的输出位置,配置到之前新建的OBJ文件夹下。
配置name of executable, 配置为template,与target一致。
配置create hex file,编译后生成hex文件。
取消browse information,如果在HAL库下,勾选这个选项,编译将非常慢,所以取消掉,加快编译速度。

++++++++++++++++++++++++++++++++
keil中需要注意的选项设置

选择仿真器,
在工具栏上,选择魔法棒,option for target “XXX”,
选择debug选项卡,左侧是simulation,右侧是debugger,
右侧选择ulink2/me,
勾选load application at startup,勾选run to main,

然后点击旁边的setting,
在debug选项卡上,设置port为swd,max clock设置为10MHZ或者更小。
设置connect为normal,设置reset为SYSRESETREQ,勾选reset after connect。
勾选cache code ,cache memory,
不勾选 download to flash。
不勾选 verify code download。

在flash download选项卡上,勾选program 和 verify。勾选erase section。勾选reset and run。
ram for algorithm,设置地址,start为0x20000000,size为0x00001000。(4KB)
programming algorithm,设置地址,start为0x08000000,size为0x00100000。(1MB)
在trace 选项卡上,勾选use core clock。

在工具栏上,选择魔法棒,option for target “XXX”,
选择utility选项卡,选择use target driver for flash programming,
勾选use debug driver ,勾选update target before debugging。
设置add output image to group为user。

+++++++++++++++++++++++++++++++++
keil中需要修改的编辑设置

edit->configuration->color and font->c/C++下,
number,橙色,bold,
operator,红色,nomal,
keyword,蓝色,bold,
string,亮紫色,bold,
preprocessor,草绿色,bold,
user keyword,深紫色,bold,

edit->configuration->userkeyword->c/c++下,添加typedef的各种类型,例如,
添加u32,u16,u8,s32,s16,s8等。
添加__I,__O,__IO等,
添加uint32_t,uint16_t,uint8_t,int32_t,int16_t,int8_t等。

++++++++++++++++++++++++++++++
stm32中常用的类型定义

typedef   signed          char int8_t;
typedef   signed short     int int16_t;
typedef   signed           int int32_t;

typedef unsigned          char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;

typedef   signed           int intptr_t;
typedef unsigned           int uintptr_t;

typedef uint32_t u32
typedef uint16_t u16
typedef uint8_t u8

typedef int32_t s32
typedef int16_t s16
typedef int8_t s8

++++++++++++++++++++++++++++++
stm32中常用的宏定义

#define _I volatile
#define _O volatile
#define _IO volatile

+++++++++++++++++++++++++++++++

猜你喜欢

转载自blog.csdn.net/weixin_42418557/article/details/121899973
今日推荐