日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]STM32中GPIO的配置風(fēng)格和以往研究的MCU有很大的不同,研究了好一段時間才搞通。typedef enum{ GPIO_Mode_AIN = 0x0,GPIO_Mode_IN_FLOATING = 0x04,GPIO_Mode_IPD = 0x28,GPIO_Mode_IPU = 0x48,GPIO_Mode_Out_OD = 0x

STM32中GPIO的配置風(fēng)格和以往研究的MCU有很大的不同,研究了好一段時間才搞通。

typedef enum
{ GPIO_Mode_AIN = 0x0,
GPIO_Mode_IN_FLOATING = 0x04,
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
GPIO_Mode_Out_OD = 0x14,
GPIO_Mode_Out_PP = 0x10,
GPIO_Mode_AF_OD = 0x1C,
GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;

配置一個引腳只需要4位寄存器,而上面卻定義了8位,仔細(xì)研究GPIO_Init()函數(shù)后,確定為ST開發(fā)人員加上去的標(biāo)識位。0x1_ 的是輸出標(biāo)識,其他則為輸入模式。

下面看一下GPIO_Init()這個函數(shù):

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;
/* Check the parameters
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));

/*---------------------------- GPIO Mode Configuration -----------------------
currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);//屏蔽高半個字節(jié)的標(biāo)識位
if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)//判斷是否為輸出,0x1_
{
/* Check the parameters
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
/* Output mode
currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;//如果是輸出,則加上相關(guān)的速度標(biāo)志
}
/*---------------------------- GPIO CRL Configuration ------------------------
/* Configure the eight low port pins
if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)//判斷引腳是否有效
{
tmpreg = GPIOx->CRL;//讀出CRL寄存器中的值,并保存
for (pinpos = 0x00; pinpos < 0x08; pinpos++)
{
pos = ((uint32_t)0x01) << pinpos;
/* Get the port pins position找出引腳的位置
currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
if (currentpin == pos)
{
pos = pinpos << 2;//pos*4,因為每個引腳配置占4位
/* Clear the corresponding low control register bits
pinmask = ((uint32_t)0x0F) << pos;
tmpreg &= ~pinmask;//把需要配置引腳的4位清0,其位不變
/* Write the mode configuration in the corresponding bits
tmpreg |= (currentmode << pos);//把配置數(shù)據(jù)寫入tmpreg
/* Reset the corresponding ODR bit//如果是下拉輸入或者上拉輸入,則還需要配置PxODR位
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
{
GPIOx->BRR = (((uint32_t)0x01) << pinpos);//如果下拉,清除對應(yīng)ODRy為0
}
else
{
/* Set the corresponding ODR bit
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
{
GPIOx->BSRR = (((uint32_t)0x01) << pinpos);//如果上拉,設(shè)置對應(yīng)的ODRy為1
}
}
}
}
GPIOx->CRL = tmpreg;//把配置好的數(shù)值寫入寄存器
}


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