实验目的:
通过寄存器方式点亮LED
实验步骤及原理:
初始化分以下几步:①开时钟
在对应的位写1则开启对应时钟,写0则关闭时钟
②配置GPIO属性,设置输入或者输出
③进行读写操作
实验代码:
(1)初始化
void LED_INIT(void) { RCC->APB2ENR|=9<<2;//开时钟 GPIOA GPIOD GPIOA->CRH &= ~(3<<0); //0xfffffff0;// clear 推挽输出 50MHz PA8 GPIOA->CRH |= (3<<0);//0x00000003 set GPIOD->CRL &= ~(3<<8);//0xfffff0ff; //PD2 GPIOD->CRL |= (3<<8); //0x00000300; }
(2)main函数
int main(void) { LED_INIT(); GPIOA->BRR |=1<<8; //reset GPIOA->BSRR |=1<<8;//set GPIOD->BRR |=1<<2; // reset GPIOD->BSRR |=1<<2;//set GPIOA->ODR &=~(1<<8); //开led GPIOA->ODR |= (1<<8); //关led while(1) { GPIOA->ODR ^= GPIO_Pin_8; //LED_Toggle GPIOD->ODR ^= GPIO_Pin_2; } }