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);
    }
}   
相关推荐
IT=>小脑虎30 分钟前
2026版 Python零基础小白学习知识点【基础版详解】
开发语言·python·学习
看见繁华1 小时前
Linux 交叉编译实践笔记
linux·运维·笔记
Y1rong1 小时前
STM32之中断
stm32·单片机·嵌入式硬件
李泽辉_1 小时前
深度学习算法学习(五):手动实现梯度计算、反向传播、优化器Adam
深度学习·学习·算法
星火开发设计1 小时前
C++ set 全面解析与实战指南
开发语言·c++·学习·青少年编程·编程·set·知识
先知后行。1 小时前
STM32F103的启动过程
stm32·单片机·嵌入式硬件
坚持就完事了1 小时前
Linux的学习03:时间没有更新怎么解决
学习
李泽辉_1 小时前
深度学习算法学习(一):梯度下降法和最简单的深度学习核心原理代码
深度学习·学习·算法
im_AMBER2 小时前
Leetcode 99 删除排序链表中的重复元素 | 合并两个链表
数据结构·笔记·学习·算法·leetcode·链表
创作者mateo2 小时前
PyTorch 入门笔记配套【完整练习代码】
人工智能·pytorch·笔记