STM32学习笔记

发布于:2025-06-19 ⋅ 阅读:(12) ⋅ 点赞:(0)

学习基于:STM32F103C8T6

外设:

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

  • 外设一览表:
    在这里插入图片描述

  • 引脚定义图
    在这里插入图片描述

基于寄存器开发

点灯代码

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

基于库函数的点灯

#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

//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);
    }
}   

流水灯代码

#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);
    }
}   


网站公告

今日签到

点亮在社区的每一天
去签到