STM32学习笔记——点亮LED
本人学习STM32是直接通过操作stm32的寄存器,使用的开发板是野火ISO-V2版本;
先简单的介绍一下stm32的GPIO;
stm32的GPIO有多种模式:
1.输入浮空
2.输入上拉
3.输入下拉
4.模拟输入
5.开漏输出
6.推挽式输出
7.推挽式复用功能
8.开漏复用功能
stm32GPIO模式设置相关寄存器设置的介绍
stm32中文参考手册中对GPIO模式设置对应寄存器的详细介绍:
下图为开发板LED的接线图;
根据上面的电路图可知,将GPIOB_0,GPIOF_7和GPIOF_8设置为低电平时,LED将被点亮;
程序代码:
LED.h 文件
1 #ifndef __LED_H 2 #define __LED_H 3 4 void GPIO_Init(void); 5 6 7 8 9 #endif
stm32.h 文件
1 #ifndef __STM32_H 2 #define __STM32_H 3 4 //引脚寄存器定义 5 6 //GPIO对应寄存器起始地址 7 8 #define GPIOA 0x40010800 9 #define GPIOB 0x40010C00 10 #define GPIOC 0x40011000 11 #define GPIOD 0x40011400 12 #define GPIOE 0x40011800 13 #define GPIOF 0x40011C00 14 #define GPIOG 0x40012000 15 16 //寄存器偏移地址 17 18 #define GPIOx_CRL 0x00 19 #define GPIOx_CRH 0x04 20 #define GPIOx_IDR 0x08 21 #define GPIOx_ODR 0x0c 22 #define GPIOx_BSRR 0x10 23 #define GPIOx_BRR 0x14 24 #define GPIOx_LCKR 0x18 25 26 27 28 #define GPIOB_CRL *((volatile unsigned int *)(GPIOB + GPIOx_CRL)) 29 #define GPIOB_CRH *((volatile unsigned int *)(GPIOB + GPIOx_CRH)) 30 #define GPIOB_IDR *((volatile unsigned int *)(GPIOB + GPIOx_IDR)) 31 #define GPIOB_ODR *((volatile unsigned int *)(GPIOB + GPIOx_ODR)) 32 #define GPIOB_BSRR *((volatile unsigned int *)(GPIOB + GPIOx_BSRR)) 33 #define GPIOB_BRR *((volatile unsigned int *)(GPIOB + GPIOx_BRR)) 34 #define GPIOB_LCKR *((volatile unsigned int *)(GPIOB + GPIOx_LCKR)) 35 36 37 38 #define GPIOF_CRL *((volatile unsigned int *)(GPIOF + GPIOx_CRL)) 39 #define GPIOF_CRH *((volatile unsigned int *)(GPIOF + GPIOx_CRH)) 40 #define GPIOF_IDR *((volatile unsigned int *)(GPIOF + GPIOx_IDR)) 41 #define GPIOF_ODR *((volatile unsigned int *)(GPIOF + GPIOx_ODR)) 42 #define GPIOF_BSRR *((volatile unsigned int *)(GPIOF + GPIOx_BSRR)) 43 #define GPIOF_BRR *((volatile unsigned int *)(GPIOF + GPIOx_BRR)) 44 #define GPIOF_LCKR *((volatile unsigned int *)(GPIOF + GPIOx_LCKR)) 45 46 47 #endif
LED.c 文件
1 #include "LED.h" 2 #include "stm32.h" 3 4 void delay(unsigned int x) 5 { 6 volatile int i, j; 7 for(i = 0; i < x; i++) 8 { 9 for(j = 0; j < 1000; j++); 10 } 11 } 12 13 void GPIO_Init(void) 14 { 15 //使能GPIOB和GPIOF的时钟 16 RCC_APB2ENR |= 1 << 3; 17 RCC_APB2ENR |= 1 << 7; 18 19 //清除GPIOB和GPIOF的配置寄存器 20 GPIOB_CRL &= ~(0xf << 0); 21 GPIOF_CRL &= ~(0xf << 0); 22 GPIOF_CRH &= ~(0xf << 0); 23 24 //配置GPIOB和GPIOF的端口为通用推挽输出,输出速度为50HZ 25 GPIOB_CRL |= 3 << 0; 26 GPIOF_CRL |= 3 << 28; 27 GPIOF_CRH |= 3 << 0; 28 29 //设置GPIOB_0和GPIOF_7和GPIOF_8输出为高电平 30 GPIOB_ODR |= 1 << 0; 31 GPIOF_ODR |= 3 << 7; 32 } 33 34 int main(void) 35 { 36 GPIO_Init(); 37 38 GPIOB_ODR &= ~(1 << 0); //设置GPIOB_0端口输出为低电平 39 GPIOF_ODR &= ~(3 << 7); //设置GPIOF_7和GPIOF_8输出为低电平 40 while(1) 41 { 42 GPIOB_ODR &= ~(1 << 0); //设置GPIOB_0端口输出为低电平 43 GPIOF_ODR |= 3 << 7; //设置GPIOF_7和GPIOF_8输出为高电平 44 delay(100); 45 46 GPIOB_ODR |= 1 << 0; //设置GPIOB_0端口输出为高电平 47 GPIOF_ODR &= ~(1 << 7); //设置GPIOF_7输出为低电平 48 delay(100); 49 50 GPIOF_ODR &= ~(1 << 8); //设置GPIOF_7输出为高电平 51 GPIOF_ODR |= 1 << 7; //设置GPIOF_8输出为低电平 52 delay(100); 53 } 54 55 }
程序将实现流水灯的效果;
在使用GPIO时一定要将相应GPIO的时钟开启;
参考资料:
文献:stm32中文参考手册V_10
视频:正点原子战舰stm32f103 V3教学视频
如有错误,请批评指正,小弟将不甚感激
来源:https://www.cnblogs.com/wp2312139418/p/5869181.html