Direct Link
Direct Link
示例使用ST标准库,资源可从ST官网获取STM32F10x standard peripheral library。
一个空的工程已经建立完成。
STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\CoreSupport目录下的core_cm3.c,core_cm3.h,复制到工程目录下的core子目录。
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,复制到工程目录下的user子目录。
STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template目录下的4个文件main.c,stm32f10x_conf.h,stm32f10x_it.c,stm32f10x_it.h复制到工程目录下的user子目录。
项目的基础文件已经复制完成。
右键点工程目标,选择Options for Target 'Hello',在C/C++选项卡中“Include Paths”中指定搜索路径,搜索路径应为.\core;.\lib\inc;.\user
右键点工程目标,选择Options for Target 'Hello',在C/C++选项卡中“Define”中定义“STM32F10X_MD,USE_STDPERIPH_DRIVER”,用于指定库函数的适用器件,宏在stm32f10x.h中被使用。
#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 "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)" #endif
// main.c #include "stm32f10x.h" 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, ENABLE); 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, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_Init(GPIOA, &GPIO_InitStructure); while(1) { /* Set PA8 and PD2 */ GPIOD->BSRR = 0x00000004; GPIOA->BSRR = 0x00000100; delay(100); /* Reset PA8 and PD2 */ GPIOD->BRR = 0x00000004; GPIOA->BRR = 0x00000100; delay(100); } } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t* file, uint32_t line) { while(1) {} } #endif
完成后,可以成功编译了。
1. keil 5 出现 error: #67: expected a “}” 错误解决
在工程属性中,c/c++ 选项中去掉手动添加的一个宏定义STM32F103_HD,KEIL5已经自动添加了
2. L6236E: No section matches selector 报错
没有添加startup_*.s 文件