芯片包的下载:芯片包官方链接:https://www.keil.com/dd2/Pack/
固件库的官方地址链接:http://www.st.com/en/embedded-software/stm32-standard-peripheral-libraries.html?querycriteria=produ ctId=LN1939
下载103固件库
创建一个空项目——————按OK就行
Objects和Listings文件是keil自动生成的文件,存放编译过程的中间文件,这里我们在template文件夹目录下新建几个文件夹,建立CORE文件夹,用来存放STM32的核心文件和启动文件,
-
建立OBJ文件夹,用来存放编译工程的中间文件以及hex文件,
-
建立SYSTEM文件夹,用来存放自己写的驱动文件和其他.c, .h文件,
-
建立STM32F10x_FWLib文件夹,用来存放STM32的库函数源码文件,
开始将相关文件存放到对应文件夹
-
STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\STM32F10x_StdPeriph_Driver文件夹下面,将src(库文件的.c文件),inc(库文件的.h文件)存放到STM32F10x_FWLib文件夹下。这两个文件其实是官方库函数源码。
-
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 文件夹下将startup_stm32f10x_hd.s文件放到CORE文件夹下,这个文件是启动文件,是根据芯片容量来选择启动文件,还有md(中容量),ld(小容量)。
-
STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x 文件夹下,将stm32f10x.h, system_stm32f10x.c, system_stm32f10x.h这三个文件放到USER文件夹下。
-
STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template文件下,将main.c, stm32f10x_conf.h, stm32f10x_it.c, stm32f10x_it.h 放到USER文件夹下。此时,基本移植完成了所有文件
进入创建的Keil空文件,点击彩色盒子那个
开始添加文件路径,方法为选中Target1,右键,有个品字形的manage功能,通过这个添加.C文件,需要注意的是在CORE文件下添加,需要将启动文件添加进去。选中ALL file可以看到这个文件。
接下来为配置环境
STM32F10X_HD,USE_STDPERIPH_DRIVER
修改到OJB路径
Delay.c的代码
#include "stm32f10x.h"
/**
* @brief 微秒级延时
* @param xus 延时时长,范围:0~233015
* @retval 无
*/
void Delay_us(int xus)
{
SysTick->LOAD = 72 * xus; //设置定时器重装值
SysTick->VAL = 0x00; //清空当前计数值
SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器
while(!(SysTick->CTRL & 0x00010000)); //等待计数到0
SysTick->CTRL = 0x00000004; //关闭定时器
}
/**
* @brief 毫秒级延时
* @param xms 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_ms(int xms)
{
while(xms--)
{
Delay_us(1000);
}
}
/**
* @brief 秒级延时
* @param xs 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_s(int xs)
{
while(xs--)
{
Delay_ms(1000);
}
}
Delay.h
#ifndef __DELAY_H
#define __DELAY_H
void Delay_us(int us);
void Delay_ms(int ms);
void Delay_s(int s);
#endif
main.c
/**
******************************************************************************
* @file Project/STM32F10x_StdPeriph_Template/main.c
* @author MCD Application Team
* @version V3.6.0
* @date 20-September-2021
* @brief Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2011 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
#include "Delay.h"
#ifdef USE_STM32100B_EVAL
#include "stm32100b_eval_lcd.h"
#elif defined USE_STM3210B_EVAL
#include "stm3210b_eval_lcd.h"
#elif defined USE_STM3210E_EVAL
#include "stm3210e_eval_lcd.h"
#elif defined USE_STM3210C_EVAL
#include "stm3210c_eval_lcd.h"
#elif defined USE_STM32100E_EVAL
#include "stm32100e_eval_lcd.h"
#endif
/** @addtogroup STM32F10x_StdPeriph_Template
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifdef USE_STM32100B_EVAL
#define MESSAGE1 "STM32 MD Value Line "
#define MESSAGE2 " Device running on "
#define MESSAGE3 " STM32100B-EVAL "
#elif defined (USE_STM3210B_EVAL)
#define MESSAGE1 "STM32 Medium Density"
#define MESSAGE2 " Device running on "
#define MESSAGE3 " STM3210B-EVAL "
#elif defined (STM32F10X_XL) && defined (USE_STM3210E_EVAL)
#define MESSAGE1 " STM32 XL Density "
#define MESSAGE2 " Device running on "
#define MESSAGE3 " STM3210E-EVAL "
#elif defined (USE_STM3210E_EVAL)
#define MESSAGE1 " STM32 High Density "
#define MESSAGE2 " Device running on "
#define MESSAGE3 " STM3210E-EVAL "
#elif defined (USE_STM3210C_EVAL)
#define MESSAGE1 " STM32 Connectivity "
#define MESSAGE2 " Line Device running"
#define MESSAGE3 " on STM3210C-EVAL "
#elif defined (USE_STM32100E_EVAL)
#define MESSAGE1 "STM32 HD Value Line "
#define MESSAGE2 " Device running on "
#define MESSAGE3 " STM32100E-EVAL "
#endif
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
/* Private function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* Private functions ---------------------------------------------------------*/
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;//框架
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3 ;//pin_3 蜂鸣器
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//传输速度
GPIO_Init(GPIOG,&GPIO_InitStructure);
while(1){
GPIO_ResetBits(GPIOG,GPIO_Pin_3);
Delay_ms(400);
GPIO_SetBits(GPIOG,GPIO_Pin_3);
Delay_ms(400);
}
}
最后运行,如有报错,把main方法里面的报错的删掉重新运行,进行烧入。