
本课程使用的stm32型号
引脚定义,有FT能接5v,没有FT能接3.3v

启动配置
第二种启动模式中,系统存储器中存放了一部分Bootloader程序,该程序可以接收串口的数据,然后刷新到主闪存中,这样就可以使用串口下载程序。
最小系统电路
能工作起来的最基本的电路,晶振 8M 赫兹通过锁相环倍频得到 72 hz;复位模块的 NRST 是低电平复位,当电路刚上电时,电容开始充电,此时 nrst 为低电平,而当电容充满电,由于电容的隔直功能,使得 nrst 电平逐渐升高。启动配置就是芯片上的跳线帽。

软件安装
新建工程

1. 找到启动文件
D:\Users\Desktop\embedded\STM32入门教程资料\固件库\STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm

stm32f10x.h:描述芯片外围设备有哪些寄存器以及对应的地址。两个system文件主要用来配置时钟。

2. 找到内核寄存器描述文件以及内核配置函数
D:\Users\Desktop\embedded\STM32入门教程资料\固件库\STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\CoreSupport

3. 添加启动文件
启动文件的选择:


4. 添加头文件路径

5. 添加 main 函数

main.c 内容:
#include "stm32f10x.h"
int main(void)
{
while(1)
{
}
}

勾上 reset and run,这样下载程序后会立马复位并执行:

6. 使用寄存器点灯
使能 GPIOC 的时钟:


RCC->APB2ENR = 0x00000010;
配置 pC13 口的模式(pC13 即为 led 的端口),CNF13 配置为通用推挽输出模式,MODE13 配置为输出模式。



GPIOC->CRH = 0x00300000;
接下来就可以给 pC13 口输出数据了,ODR13 为 1,则 pC13 为高电平,反之则为低电平,因为我们需要 led 亮起,所以要使得 pC13 为低电平。

GPIOC->ODR = 0x00000000;
整体代码如下,烧录后可以看到 led2 亮起:
#include "stm32f10x.h"
int main(void)
{
RCC->APB2ENR = 0x00000010;
GPIOC->CRH = 0x00300000;
GPIOC->ODR = 0x00000000;
while(1)
{
}
}
7. 添加库函数
将库函数源码和头文件复制粘贴到 Library 目录下,然后在 keil 中导入:



还需要三个文件如下,conf 文件来配置函数库头文件的包含关系,两个 it 文件用来存放中断函数,粘贴到 User 目录下:

添加宏定义 USE_STDPERIPH_DRIVER:
8. 使用库函数进行点灯
代码如下:
#include "stm32f10x.h"
int main(void)
{
//RCC->APB2ENR = 0x00000010;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
//GPIOC->CRH = 0x00300000;
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
//GPIOC->ODR = 0x00000000;
GPIO_SetBits(GPIOC, GPIO_Pin_13); // 设置pC13高电平
//GPIO_ResetBits(GPIOC, GPIO_Pin_13); // 设置pC13低电平
while(1)
{
}
}
9. 工程架构

复位中断,先调用 SystemInit,再调用 __main。
