废话不多,GPIO配置是在开发单片机常用的。下面就来介绍如何将GPIO配置输入模式。
#define Led1_Pin GPIO_PIN_5
#define Led1_Port GPIOB
#define Led1_High GPIO_WriteBit(Led1_Port, Led1_Pin, Bit_SET)
#define Led1_Low GPIO_WriteBit(Led1_Port, Led1_Pin, Bit_RESET)
void OutPut_IO_Init(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
GPIO_InitStructure.Pin = Led1_Pin;
GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
GPIO_InitStructure.GPIO_Pull = GPIO_No_Pull;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitPeripheral(Led1_Port, &GPIO_InitStructure);
Led1_High;
}
#define Key_Pin GPIO_PIN_4
#define Key_Port GPIOA
#define Read_Key_Port GPIO_ReadInputDataBit(Key_Port, Key_Pin)
void InPut_IO_Init(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
GPIO_InitStructure.Pin = Key_Pin;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;
GPIO_InitPeripheral(Key_Port, &GPIO_InitStructure);
}
int main(void)
{
InPut_IO_Init();
OutPut_IO_Init();
while(1)
{
if(Read_Key_Port == Bit_SET)
Led1_High;
else
Led1_Low;
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/dailin2012/article/details/129726704
|