一、前言
? ? ? ? 由于程序設(shè)計(jì)開發(fā)具有的不確定性,我們常常需要初始化某些特定的引腳,并讀取引腳電平狀態(tài)或向引腳輸出高低電平。
二、代碼實(shí)現(xiàn)
? ? ? ? 快速找到端口的初始化語句:
? ? ? ? 首先,找到board.c文件,在下圖的位置,我們可以看到關(guān)于LED燈的端口的初始化語句。
? ? ? ? 通過 Go Definition功能或者直接找到gpio.c文件,可以看到具體的實(shí)現(xiàn)方法:
/*
/ _____) _ | |
( (____ _____ ____ _| |_ _____ ____| |__
\____ \| ___ | (_ _) ___ |/ ___) _ \
_____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
(C)2013 Semtech
Description: Generic GPIO driver implementation
Comment: Relies on the specific board GPIO implementation as well as on
IO expander driver implementation if one is available on the target
board.
License: Revised BSD License, see LICENSE.TXT file include in the project
Maintainer: Miguel Luis and Gregory Cristian
*/
#include "board.h"
#include "gpio-board.h"
void GpioInit( Gpio_t *obj, PinNames pin, PinModes mode, PinConfigs config, PinTypes type, uint32_t value )
{
GpioMcuInit( obj, pin, mode, config, type, value );
}
void GpioSetInterrupt( Gpio_t *obj, IrqModes irqMode, IrqPriorities irqPriority, GpioIrqHandler *irqHandler )
{
GpioMcuSetInterrupt( obj, irqMode, irqPriority, irqHandler );
}
void GpioRemoveInterrupt( Gpio_t *obj )
{
GpioMcuRemoveInterrupt( obj );
}
void GpioWrite( Gpio_t *obj, uint32_t value )
{
GpioMcuWrite( obj, value );
}
void GpioToggle( Gpio_t *obj )
{
GpioMcuToggle( obj );
}
uint32_t GpioRead( Gpio_t *obj )
{
return GpioMcuRead( obj );
}
? ? ? ? 本次,我們主要使用的方法為:GpioInit()
? ? ? ? 該方法有五個(gè)參數(shù),我們依次展開來看。
? ? ? ? 參數(shù)一:Gpio_t *obj :我們?cè)谑褂脮r(shí)需要先創(chuàng)建一個(gè)Gpio_t類型的變量,并在初始化時(shí)傳入,方便我們后面去對(duì)其操作。
? ? ? ? 參數(shù)二:PinNames pin:此處我們填寫需要操作的端口,例如PB_2。
? ? ? ? 參數(shù)三:PinModes mode:此處我們可以填常見的兩種,即PIN_INPUT(輸入模式),PIN_OUTPUT(輸出模式),而PIN_ALTERNATE_FCT與PIN_ANALOGIC暫時(shí)不需要我們關(guān)注。
? ? ? ? 參數(shù)四:PinConfigs config:此處我們一般寫PIN_PUSH_PULL(推挽)即可。
? ? ? ? 參數(shù)五:PinTypes type:此處我們根據(jù)需要選擇PIN_NO_PULL,PIN_PULL_UP,PIN_PULL_DOWN即可。
? ? ? ? 參數(shù)六:填0即可。
? ? ? ? 代碼示例:
? ? ? ? 下面展示讀取PB_2電平,低電平點(diǎn)亮LED1的操作:
文章來源:http://www.zghlxwxcb.cn/news/detail-479114.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-479114.html
Gpio_t Device;
int main( void )
{
Init();
GpioInit(&Device,PB_2,PIN_INPUT,PIN_PUSH_PULL,PIN_PULL_UP,0);
while( 1 )
{
if(GpioRead(&Device)==0)
GpioWrite(&Led1,0);
else
GpioWrite(&Led1,1);
}
}
到了這里,關(guān)于物聯(lián)網(wǎng)Lora模塊從入門到精通(四)對(duì)某些端口的初始化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!