當前位置:首頁 > 單片機 > 單片機
[導讀] 一、總覽HAL層里的庫函數(shù)和大家熟悉的stm32lib基本上是一致的。一般來說一個cpu外設的操作函數(shù)分成兩部分,分別是init和operation。來看一個cube里的例子:static GPIO_InitTypeDef GPIO_InitStruct;in

一、總覽

HAL層里的庫函數(shù)和大家熟悉的stm32lib基本上是一致的。一般來說一個cpu外設的操作函數(shù)分成兩部分,分別是init和operation。

來看一個cube里的例子:

static GPIO_InitTypeDef GPIO_InitStruct;


int main(void)

{

/* This sample code shows how to use GPIO HAL API to toggle LED2 IO

in an infinite loop. */


/* STM32F103xB HAL library initialization:

- Configure the Flash prefetch

- Systick timer is configured by default as source of time base, but user

can eventually implement his proper time base source (a general purpose

timer for example or other time source), keeping in mind that Time base

duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and

handled in milliseconds basis.

- Set NVIC Group Priority to 4

- Low Level Initialization

*/

HAL_Init();


/* Configure the system clock to 64 MHz */

SystemClock_Config();


/* -1- Enable GPIO Clock (to be able to program the configuration registers) */

LED2_GPIO_CLK_ENABLE();


/* -2- Configure IO in output push-pull mode to drive external LEDs */

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;


GPIO_InitStruct.Pin = LED2_PIN;

HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStruct);


/* -3- Toggle IO in an infinite loop */

while (1)

{

HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);

/* Insert delay 100 ms */

HAL_Delay(100);

}

}

注釋里的內(nèi)容說明了之前一篇文章里的提到的HAL_INIT()函數(shù)的作用。


在使用GPIO前要先初始化GPIO的時鐘,因為許多外設都被設計成時序邏輯電路,所以必須要為外設提供時鐘源,否則外設的電路狀態(tài)就不會改變。


GPIO時鐘源設定后,通過GPIO_initTypeDef結(jié)構(gòu)體,初始化指定Pin腳的模式、速度、初始輸出。


最后調(diào)用 HAL_GPIO_TogglePin()和HAL_Delay()將GPIO定時取反以達到使驅(qū)動的led閃爍的效果。


二、GPIO的初始化


GPIO_InitTypeDef結(jié)構(gòu)體用于對一個或者一組GPIO來進行初始化,它的四個成員分別是,Pin、Mode、Pull、Speed。


GPIO_InitTypeDef原型:

/**

* @brief GPIO Init structure definition

*/

typedef struct

{

uint32_t Pin; /*!< Specifies the GPIO pins to be configured.

This parameter can be any value of @ref GPIO_pins_define */


uint32_t Mode; /*!< Specifies the operating mode for the selected pins.

This parameter can be a value of @ref GPIO_mode_define */


uint32_t Pull; /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.

This parameter can be a value of @ref GPIO_pull_define */


uint32_t Speed; /*!< Specifies the speed for the selected pins.

This parameter can be a value of @ref GPIO_speed_define */

}GPIO_InitTypeDef;


Pin

#define GPIO_PIN_0 ((uint16_t)0x0001) /* Pin 0 selected */

#define GPIO_PIN_1 ((uint16_t)0x0002) /* Pin 1 selected */

#define GPIO_PIN_2 ((uint16_t)0x0004) /* Pin 2 selected */

#define GPIO_PIN_3 ((uint16_t)0x0008) /* Pin 3 selected */

#define GPIO_PIN_4 ((uint16_t)0x0010) /* Pin 4 selected */

#define GPIO_PIN_5 ((uint16_t)0x0020) /* Pin 5 selected */

#define GPIO_PIN_6 ((uint16_t)0x0040) /* Pin 6 selected */

#define GPIO_PIN_7 ((uint16_t)0x0080) /* Pin 7 selected */

#define GPIO_PIN_8 ((uint16_t)0x0100) /* Pin 8 selected */

#define GPIO_PIN_9 ((uint16_t)0x0200) /* Pin 9 selected */

#define GPIO_PIN_10 ((uint16_t)0x0400) /* Pin 10 selected */

#define GPIO_PIN_11 ((uint16_t)0x0800) /* Pin 11 selected */

#define GPIO_PIN_12 ((uint16_t)0x1000) /* Pin 12 selected */

#define GPIO_PIN_13 ((uint16_t)0x2000) /* Pin 13 selected */

#define GPIO_PIN_14 ((uint16_t)0x4000) /* Pin 14 selected */

#define GPIO_PIN_15 ((uint16_t)0x8000) /* Pin 15 selected */

#define GPIO_PIN_All ((uint16_t)0xFFFF) /* All pins selected */


選擇多個Pin的時候,可以用位選的宏定義相或得到。

Mode

#define GPIO_MODE_INPUT ((uint32_t)0x00000000) /*!< Input Floating Mode */

#define GPIO_MODE_OUTPUT_PP ((uint32_t)0x00000001) /*!< Output Push Pull Mode */

#define GPIO_MODE_OUTPUT_OD ((uint32_t)0x00000011) /*!< Output Open Drain Mode */

#define GPIO_MODE_AF_PP ((uint32_t)0x00000002) /*!< Alternate Function Push Pull Mode */

#define GPIO_MODE_AF_OD ((uint32_t)0x00000012) /*!< Alternate Function Open Drain Mode */

#define GPIO_MODE_AF_INPUT GPIO_MODE_INPUT /*!< Alternate Function Input Mode */


#define GPIO_MODE_ANALOG ((uint32_t)0x00000003) /*!< Analog Mode */


#define GPIO_MODE_IT_RISING ((uint32_t)0x10110000) /*!< External Interrupt Mode with Rising edge trigger detection */

#define GPIO_MODE_IT_FALLING ((uint32_t)0x10210000) /*!< External Interrupt Mode with Falling edge trigger detection */

#define GPIO_MODE_IT_RISING_FALLING ((uint32_t)0x10310000) /*!< External Interrupt Mode with Rising/Falling edge trigger detection */


#define GPIO_MODE_EVT_RISING ((uint32_t)0x10120000) /*!< External Event Mode with Rising edge trigger detection */

#define GPIO_MODE_EVT_FALLING ((uint32_t)0x10220000) /*!< External Event Mode with Falling edge trigger detection */

#define GPIO_MODE_EVT_RISING_FALLING ((uint32_t)0x10320000) /*!< External Event Mode with Rising/Falling edge trigger detection */


GPIO的模式理解起來有點復雜。

GPIO可以被用作中斷源(GPIO_MODE_IT)和事件源(GPIO_MODE_EVT),這以后再敘述。

GPIO也可以用來接收模擬量(GPIO_MODE_ANALOG)。

如果GPIO原本是一個復用I/O,比如可以被作為串口模塊使用,要用GPIO_MODE_AF_x來設置。

GPIO可以被設置成輸入或者輸出。

當被設置成輸出時,有open-drain和push-pull兩者模式。我覺得理解這兩種模式對于像我一樣的模電知識較薄弱的人來說有點困難。而且此類知識的理解需要做實驗來輔助,在沒有實驗環(huán)境的情況下,還要學習電路仿真軟件的使用,學習成本較高,就不做敘述,直接說結(jié)論了:

push-pull模式,速度快,但功耗大。

open-drain模式,功耗低,同時有線與功能。如果此模式芯片內(nèi)部沒有上拉電路,需要外接上拉電阻。


Pull


#define GPIO_NOPULL ((uint32_t)0x00000000) /*!< No Pull-up or Pull-down activation */

#define GPIO_PULLUP ((uint32_t)0x00000001) /*!< Pull-up activation */

#define GPIO_PULLDOWN

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉