LED操作

灯上拉

GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_Level_3;
GPIO_Init(GPIOB, &GPIO_InitStruct);

GPIO_SetBits(GPIOB, GPIO_Pin_0);

取反:

#define RE_LED1 GPIO_WriteBit(GPIOB, GPIO_Pin_1,(BitAction)((1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_1))));

置为0或1

#define LED1OFF {GPIOB->BSRR = GPIO_Pin_0;} //B0 = 1
#define LED1ON {GPIOB->BRR = GPIO_Pin_0;} //B0 = 0

/*****************************************************************************
可置任意管脚为 1
******************************************************************************/
void LED_Close(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
{
GPIO_SetBits(GPIOx, GPIO_Pin);

}
/*****************************************************************************
可置任意管脚为 0
******************************************************************************/
void LED_Open(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
{
GPIO_ResetBits(GPIOx, GPIO_Pin);
}

原文地址:https://www.cnblogs.com/luckytimor/p/5403213.html