GPIO 配置之ODR, BSRR, BRR 详解

刚开始学习,记录一下这几个寄存器的理解

//以GPIO_Pin_6为例  
//GPIO_Pin_6==0x0040,pin_num == 0x0040,故 GPIO->BRR 寄存器可以将第7位为1的引脚设置低电平
//BRR低16位有效,“1”设置低电平,‘0’无动作
void Led_on(uint32_t ledidx){
	uint16_t pin_num = (uint16_t) (ledidx & 0xFFFF);
	if(ledidx == LED_1) GPIOG->BRR = pin_num;
	if(ledidx == LED_2) GPIOA->BRR = pin_num;
	if(ledidx == LED_3 || ledidx == LED_4) GPIOB->BRR = pin_num;
	
	

}
//BSRR 高16位与BRR 一致, 低16位可以将‘1’设置对应引脚为低电平,‘0’无动作
void Led_off(uint32_t ledidx){
	uint16_t pin_num = (uint16_t) (ledidx & 0xFFFF);
	if(ledidx == LED_1) GPIOG->BSRR = pin_num;
	if(ledidx == LED_2) GPIOA->BSRR = pin_num;
	if((ledidx == LED_3) || (ledidx == LED_4)) {GPIOB->BRR = pin_num;}

}
//ODR寄存器可读可写:既能控制管脚为高电平,也能控制管脚为低电平。

//管脚对于位写1 gpio 管脚为高电平,写 0 为低电平
//ODR = 0xffbf=ODR(0xffff)^ 0x0040

void Led_toggle(uint32_t ledidx){
	uint16_t Pin_num = (uint16_t) (ledidx & 0xFFFF);
	if(ledidx == LED_1) GPIOG -> ODR ^= Pin_num;
	if(ledidx == LED_2) GPIOA -> ODR ^= Pin_num;
	if(ledidx == LED_3 || ledidx == LED_4) GPIOB -> ODR ^= Pin_num;
}

你可能感兴趣的:(stm32,stm32)