STM32学习笔记——点亮LED

STM32学习笔记——点亮LED

本人学习STM32是直接通过操作stm32的寄存器,使用的开发板是野火ISO-V2版本;

先简单的介绍一下stm32的GPIO;

stm32的GPIO有多种模式:

  1.输入浮空

  2.输入上拉

  3.输入下拉

  4.模拟输入

  5.开漏输出

  6.推挽式输出

  7.推挽式复用功能

  8.开漏复用功能

stm32GPIO模式设置相关寄存器设置的介绍

STM32学习笔记——点亮LED

stm32中文参考手册中对GPIO模式设置对应寄存器的详细介绍:

STM32学习笔记——点亮LED

STM32学习笔记——点亮LED

下图为开发板LED的接线图;

STM32学习笔记——点亮LED

根据上面的电路图可知,将GPIOB_0,GPIOF_7和GPIOF_8设置为低电平时,LED将被点亮;

程序代码:

LED.h 文件

 #ifndef __LED_H
#define __LED_H void GPIO_Init(void); #endif

stm32.h 文件

 #ifndef __STM32_H
#define __STM32_H //引脚寄存器定义 //GPIO对应寄存器起始地址 #define GPIOA 0x40010800
#define GPIOB 0x40010C00
#define GPIOC 0x40011000
#define GPIOD 0x40011400
#define GPIOE 0x40011800
#define GPIOF 0x40011C00
#define GPIOG 0x40012000 //寄存器偏移地址 #define GPIOx_CRL 0x00
#define GPIOx_CRH 0x04
#define GPIOx_IDR 0x08
#define GPIOx_ODR 0x0c
#define GPIOx_BSRR 0x10
#define GPIOx_BRR 0x14
#define GPIOx_LCKR 0x18 #define GPIOB_CRL *((volatile unsigned int *)(GPIOB + GPIOx_CRL))
#define GPIOB_CRH *((volatile unsigned int *)(GPIOB + GPIOx_CRH))
#define GPIOB_IDR *((volatile unsigned int *)(GPIOB + GPIOx_IDR))
#define GPIOB_ODR *((volatile unsigned int *)(GPIOB + GPIOx_ODR))
#define GPIOB_BSRR *((volatile unsigned int *)(GPIOB + GPIOx_BSRR))
#define GPIOB_BRR *((volatile unsigned int *)(GPIOB + GPIOx_BRR))
#define GPIOB_LCKR *((volatile unsigned int *)(GPIOB + GPIOx_LCKR)) #define GPIOF_CRL *((volatile unsigned int *)(GPIOF + GPIOx_CRL))
#define GPIOF_CRH *((volatile unsigned int *)(GPIOF + GPIOx_CRH))
#define GPIOF_IDR *((volatile unsigned int *)(GPIOF + GPIOx_IDR))
#define GPIOF_ODR *((volatile unsigned int *)(GPIOF + GPIOx_ODR))
#define GPIOF_BSRR *((volatile unsigned int *)(GPIOF + GPIOx_BSRR))
#define GPIOF_BRR *((volatile unsigned int *)(GPIOF + GPIOx_BRR))
#define GPIOF_LCKR *((volatile unsigned int *)(GPIOF + GPIOx_LCKR)) #endif

LED.c 文件

 #include "LED.h"
#include "stm32.h" void delay(unsigned int x)
{
  volatile int i, j;
  for(i = ; i < x; i++)
  {
    for(j = ; j < ; j++);
  }
} void GPIO_Init(void)
{
  //使能GPIOB和GPIOF的时钟
  RCC_APB2ENR |= << ;
  RCC_APB2ENR |= << ;   //清除GPIOB和GPIOF的配置寄存器
  GPIOB_CRL &= ~(0xf << );
  GPIOF_CRL &= ~(0xf << );
  GPIOF_CRH &= ~(0xf << );   //配置GPIOB和GPIOF的端口为通用推挽输出,输出速度为50HZ
  GPIOB_CRL |= << ;
  GPIOF_CRL |= << ;
  GPIOF_CRH |= << ;   //设置GPIOB_0和GPIOF_7和GPIOF_8输出为高电平
  GPIOB_ODR |= << ;
  GPIOF_ODR |= << ;
} int main(void)
{
  GPIO_Init();   GPIOB_ODR &= ~( << ); //设置GPIOB_0端口输出为低电平
  GPIOF_ODR &= ~( << ); //设置GPIOF_7和GPIOF_8输出为低电平
  while()
  {
    GPIOB_ODR &= ~( << ); //设置GPIOB_0端口输出为低电平
    GPIOF_ODR |= << ; //设置GPIOF_7和GPIOF_8输出为高电平
    delay();     GPIOB_ODR |= << ; //设置GPIOB_0端口输出为高电平
    GPIOF_ODR &= ~( << ); //设置GPIOF_7输出为低电平
    delay();     GPIOF_ODR &= ~( << ); //设置GPIOF_7输出为高电平
    GPIOF_ODR |= << ; //设置GPIOF_8输出为低电平
    delay();
  } }

程序将实现流水灯的效果;

在使用GPIO时一定要将相应GPIO的时钟开启;

参考资料:

文献:stm32中文参考手册V_10

视频:正点原子战舰stm32f103 V3教学视频

如有错误,请批评指正,小弟将不甚感激

上一篇:atitit.java解析sql语言解析器解释器的实现


下一篇:Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力