STM32学习笔记

学习基于:STM32F103C8T6

外设:

  • RCC

    在STM32中,外设在上电的的情况下默认是没有时钟的,目的是降低功耗;所以操作外设前,需要先使能模块的时钟,RCC来实现这项功能。

  • 外设一览表:

  • 引脚定义图

基于寄存器开发

点灯代码

c 复制代码
#include "Device/Include/stm32f10x.h"   // Device header
int main(void)
{
    RCC->APB2ENR = 0X00000010;
    GPIOC->CRH = 0X00300000;
    GPIOC->ODR = 0X00002000;
    while(1)
    {
        
    }
}

基于库函数的点灯

c 复制代码
#include "Device/Include/stm32f10x.h"   // Device header
int main(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_Init(GPIOC,&GPIO_InitStructure);
    GPIO_ResetBits(GPIOC, GPIO_Pin_13);
    while(1)
    {
        
    }
}   

基于库函数开发,需要移植的文件:

  1. 库函数中的.c文件
  2. 库函数中的.h文件
  3. project中的3个文件:用来配置库函数头文件的包含关系、头文件的参数检查、存放中断函数

新建工程步骤

建立工程文件夹,Keil中新建工程,选择型号

工程文件夹里建立Start、Library、User等文件夹,复制固件库里面的文件到工程文件夹

工程里对应建立Start、Library、User等同名称的分组,然后将文件夹内的文件添加到工程分组里

工程选项,C/C++,Include Paths内声明所有包含头文件的文件夹

工程选项,C/C++,Define内定义USE_STDPERIPH_DRIVER

工程选项,Debug,下拉列表选择对应调试器,Settings,Flash Download里勾选Reset and Run

c 复制代码
//LED灯闪烁代码
#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //一般都用推挽输出,高低电平均有驱动能力;开漏输出只有低电平有驱动能力
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    //GPIO_ResetBits(GPIOA, GPIO_Pin_0);
    
    while(1)
    {
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET); //亮
        Delay_s(1);
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); //灭
        Delay_s(1);
    }
}   

流水灯代码

c 复制代码
#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //一般都用推挽输出,高低电平均有驱动能力
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    //GPIO_ResetBits(GPIOA, GPIO_Pin_0);
    
    while(1)
    {
        GPIO_Write(GPIOA, ~0X0001); //第1个亮 0000 0000 0000 0001
        Delay_s(1);
        GPIO_Write(GPIOA, ~0X0002); //第2个亮 0000 0000 0000 0010
        Delay_s(1);
        GPIO_Write(GPIOA, ~0X0004); //第3个亮 0000 0000 0000 0100
        Delay_s(1);
        GPIO_Write(GPIOA, ~0X0008); //第4个亮 0000 0000 0000 1000
        Delay_s(1);
        GPIO_Write(GPIOA, ~0X0010); //第5个亮 0000 0000 0001 0000
        Delay_s(1);
        GPIO_Write(GPIOA, ~0X0020); //第6个亮 0000 0000 0010 0000
        Delay_s(1);
        GPIO_Write(GPIOA, ~0X0040); //第7个亮 0000 0000 0100 0000
        Delay_s(1);
    }
}   
相关推荐
xiaotao1313 小时前
02-机器学习基础: 监督学习——线性回归
学习·机器学习·线性回归
墨黎芜4 小时前
ArcGIS从入门到精通——地图符号、注记的初步使用
学习·arcgis·信息可视化
小李云雾4 小时前
FastAPI重要知识点---中间件(Middleware)
学习·程序人生·中间件·fastapi·middleware
.Cnn4 小时前
JavaScript 前端基础笔记(网页交互核心)
前端·javascript·笔记·交互
小夏子_riotous4 小时前
Docker学习路径——3、常用命令
linux·运维·服务器·学习·docker·容器·centos
STLearner5 小时前
WSDM 2026 | 时间序列(Time Series)论文总结【预测,表示学习,因果】
大数据·论文阅读·人工智能·深度学习·学习·机器学习·数据挖掘
redaijufeng5 小时前
网络爬虫学习:应用selenium获取Edge浏览器版本号,自动下载对应版本msedgedriver,确保Edge浏览器顺利打开。
爬虫·学习·selenium
九成宫5 小时前
IT项目管理期末复习——Chapter 10 项目沟通管理
笔记·项目管理·软件工程
腾科IT教育5 小时前
零基础快速上岸HCIP,高效学习思路分享
学习·华为认证·hcip·hcip考试·hcip认证
23471021275 小时前
4.14 学习笔记
笔记·python·学习