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);
    }
}   
相关推荐
和平宇宙2 小时前
AI笔记005. hermes-DeepSeek V4 Pro, 128K上下文引发的探索
前端·人工智能·笔记
十月的皮皮2 小时前
C语言学习笔记20260606- 求月份天数三种写法
c语言·笔记·学习
cmes_love3 小时前
Level 2逐笔成交历史数据下载方法笔记
数据库·笔记·oracle
马士兵教育3 小时前
Java还有前景吗?Java+AI大模型学习路线及项目?
java·人工智能·python·学习·机器学习
Cloud_Shy6183 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
lizhihai_994 小时前
股市学习心得-AI 产业链核心标的梳理清单
大数据·服务器·人工智能·科技·学习
kebidaixu4 小时前
FreeRTOS 移植到 STM32F407VETX 记录(一)
stm32·单片机·嵌入式硬件
问心无愧05134 小时前
ctf show web入门110
前端·笔记
吃好睡好便好4 小时前
说说科学爬山
学习·生活