一、新建工程
1、创建Start和User文件夹
Libraries->CMSIS -> CM3-> DeviceSupport-> ST-> STM32F10x-> startup-> arm->是启动文件,复制到工程模板(新建Start)
回到STM32F10x(复制那三个文件stmxxxh、systemxxx.c、systemxxx.h)复制到Start
打开CM3->CoreSupport(将两个文件复制到Start)回到Keil将文件添加到工程->点击Target1,将Source Group1单击改名Start->右键选择添加已存在文件,打开Start文件夹,打开筛选器All files,添加启动文件(视频所用型号添加后缀md.s文件)和剩下所有.c.h文件
在工程选项添加该文件夹头文件路径(魔术棒按钮,C/C++,Include Paths栏点右边三个点按钮新建路径再点右三个点按钮添加Start路径,最后点ok)
打开新建工程文件夹添加新文件夹User(放main函数)
接着keil里Target右键新建(组别)改名User添加文件选.c名叫main(注意路径选择Userwenj夹)
在main里右键插入头文件stm32f10x.h(若用寄存器开发32,到此就完成工程建立)
扳手里Encoding->UTF-8可以防止中文乱码
2、建立Library文件夹
- Libraries\STM32F10x_StdPeriph_Driver\src和Libraries\STM32F10x_StdPeriph_Driver\inc中所有的文件放到Library中,在使用上一步的方法将文件加入到工程中
- Project\STM32F10x_StdPeriph_Template文件中的以下文件加入到User文件夹中
3、工程文件的基本架构、
二、GPIO的基本输入输出
1、GPIO简介
1.1 GPIO基本架构
1.2 GPIO模式
2、各种接线图
2.1 STLINK
2.2、蜂鸣器
2.3 光敏传感器
3、GPIO输入代码
3.1 光敏传感器
#include "stm32f10x.h" // Device header
void LightSensor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
}
uint8_t Get_LightSensor(void)
{
return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}
#include "KEY.h"
#include "LightSensor.h"
#include "Buzzer.h"
int main(void)
{
LightSensor_Init();
Buzzer_Init();
while(1)
{
if (Get_LightSensor() == 0)
{
Buzzer_ON();
}else
{
Buzzer_OFF();
}
}
}
3.2 按键
#include "stm32f10x.h" // Device header
#include "Delay.h"
void KEY_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
uint8_t KEY_GetNum(void)
{
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0)
{
Delay_ms(20);
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0)
{
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) != 1);
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) != 1);
return 1;
}
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == 0)
{
Delay_ms(20);
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == 0)
{
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) != 1);
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) != 1);
return 2;
}
}
return 0;
}
4、GPIO输出
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_ResetBits(GPIOA,GPIO_Pin_0);
//GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
//GPIO_SetBits(GPIOA,GPIO_Pin_0);
int i = 0x0080;
while(1)
{
Delay_ms(200);
GPIO_Write(GPIOA, ~i);
i = i >> 1;
if(i == 0x0000)
{
i = 0x0080;
}
}
}