This shows you the differences between two versions of the page.
— |
library:stm32:helloword [2022/05/02 00:32] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Hello word for STM32F103RC ====== | ||
+ | ---- | ||
+ | |||
+ | 示例使用ST标准库,资源可从ST官网获取[[https:// | ||
+ | |||
+ | |||
+ | ====== 新建工程 ====== | ||
+ | - 打开MDK,如果已打开默认工程,关闭默认工程(Project-> | ||
+ | - 开始建立新工程(Project-> | ||
+ | - 选择目标器件(STM32F103RC), | ||
+ | 一个空的工程已经建立完成。 | ||
+ | |||
+ | ====== 复制基础文件 ====== | ||
+ | - 在工程目录下新建三个子目录,user,lib,core,分别用于归类存放工程中的用户代码, | ||
+ | - 添加库文件,《STM32F10x standard peripheral library》包中STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\STM32F10x_StdPeriph_Driver目录下inc, | ||
+ | - 添加启动文件,《STM32F10x standard peripheral library》包中 | ||
+ | STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\CoreSupport目录下的core_cm3.c, | ||
+ | STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm目录下的文件,复制到工程目录下的core子目录。\\ | ||
+ | STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x目录下的3个文件stm32f10x.h,system_stm32f10x.c,system_stm32f10x.h, | ||
+ | STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template目录下的4个文件main.c,stm32f10x_conf.h,stm32f10x_it.c,stm32f10x_it.h复制到工程目录下的user子目录。 | ||
+ | 项目的基础文件已经复制完成。 | ||
+ | |||
+ | ====== 添加代码文件到工程 ====== | ||
+ | 将需要的文件条件到工程,右键点工程目标,选择Manage Components, | ||
+ | {{: | ||
+ | |||
+ | ====== 设定头文件搜索路径 ====== | ||
+ | 右键点工程目标,选择Options for Target ' | ||
+ | |||
+ | ====== 设定编译宏 ====== | ||
+ | 右键点工程目标,选择Options for Target ' | ||
+ | #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined | ||
+ | (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && | ||
+ | !defined (STM32F10X_CL) | ||
+ | #error " | ||
+ | #endif | ||
+ | |||
+ | ====== 编写主函数代码 ====== | ||
+ | <code c> | ||
+ | // main.c | ||
+ | #include " | ||
+ | GPIO_InitTypeDef GPIO_InitStructure; | ||
+ | |||
+ | static void delay(vu32 nCount) | ||
+ | { | ||
+ | vu32 index = 0; | ||
+ | for(index = (34000 * nCount); index != 0; index--) ; | ||
+ | } | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | SystemInit(); | ||
+ | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, | ||
+ | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; | ||
+ | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | ||
+ | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; | ||
+ | GPIO_Init(GPIOD, | ||
+ | |||
+ | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, | ||
+ | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; | ||
+ | GPIO_Init(GPIOA, | ||
+ | |||
+ | while(1) | ||
+ | { | ||
+ | /* Set PA8 and PD2 */ | ||
+ | GPIOD-> | ||
+ | GPIOA-> | ||
+ | delay(100); | ||
+ | /* Reset PA8 and PD2 */ | ||
+ | GPIOD-> | ||
+ | GPIOA-> | ||
+ | delay(100); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #ifdef USE_FULL_ASSERT | ||
+ | void assert_failed(uint8_t* file, uint32_t line) | ||
+ | { | ||
+ | while(1) {} | ||
+ | } | ||
+ | #endif | ||
+ | |||
+ | |||
+ | </ | ||
+ | 完成后,可以成功编译了。 | ||
+ | |||
+ | |||
+ | ====== 问题与解决 ====== | ||
+ | 1. keil 5 出现 error: | ||
+ | |||
+ | 在工程属性中,c/ | ||
+ | |||
+ | 2. L6236E: No section matches selector 报错 | ||
+ | |||
+ | 没有添加startup_*.s 文件 | ||
+ | |||
+ | |||
+ | |||
+ | |||