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教學視頻
如有錯誤,請批評指正,小弟將不甚感激