STM32學(xué)習(xí)筆記之使用庫函數(shù)驅(qū)動(dòng)LED燈
一、熟悉GPIO結(jié)構(gòu)體
以下這個(gè)結(jié)構(gòu)體是我從官方手冊(cè)中獲取的:
[cpp] view plain copy print?
typedef struct
{
u16 GPIO_Pin;
GPIOSpeed_TypeDef GPIO_Speed;
GPIOMode_TypeDef GPIO_Mode;
} GPIO_InitTypeDef;
二、編寫程序步驟
1、首先定義一個(gè)GPIO_InitTypeDef的結(jié)構(gòu)體,給結(jié)構(gòu)體起一個(gè)名字 GPIO_InitStructure;
[cpp] view plain copy print?
GPIO_initTypeDef GPIO_initStructure ;
2、初始化RCC時(shí)鐘
RCC_APB2PeriphClockCmd 使能或者失能 APB2 外設(shè)時(shí)鐘,從下圖看GPIOA-G均屬于APB2時(shí)鐘總線。

3、對(duì)結(jié)構(gòu)體成員賦值,詳情參考固件庫使用手冊(cè)
//GPIO管腳

u16 GPIO_Pin;
//GPIO口的速率
GPIOSpeed_TypeDef GPIO_Speed;

//GPIO的輸出模式
GPIOMode_TypeDef GPIO_Mode;

4、初始化GPIO
這里要注意,每更新一個(gè)GPIO口就要初始化一次(用到了多組GPIO的情況)
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

5、
GPIO_SetBits 設(shè)置指定的數(shù)據(jù)端口位

GPIO_ResetBits 清除指定的數(shù)據(jù)端口位


編輯:admin 最后修改時(shí)間:2018-05-18