Delay_ms(200);
GPIOB->ODR |=(1<<5); Delay_ms(200); }
1.2按键灯 #include\/* 0--PA0--UP 0--PE4--RIGHT 0--PE3--DOWN 1--PE2--LEFT */
void KEY_Config(void) {
//打开A端口时钟
RCC->APB2ENR |=(1<<2); RCC->APB2ENR |=(1<<6); RCC->APB2ENR |=(1<<6); RCC->APB2ENR |=(1<<6); //配置为浮空输入 GPIOA->CRL &=~(0xf<<0); GPIOA->CRL |=(0x4<<0); GPIOE->CRL &=~(0xf<<16); GPIOE->CRL |=(0x4<<16); GPIOE->CRL &=~(0xf<<12); GPIOE->CRL |=(0x4<<12); GPIOE->CRL &=~(0xf<<8);
//置1灯1灭
24
GPIOE->CRL |=(0x4<<8); }
u8 KEY_Value(void) {
static u8 key=0;
if((!(GPIOE->IDR &(1<<3)))&&key==0) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下 { }
if((!(GPIOE->IDR &(1<<3)))&&key==1) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下 { }
if((!(GPIOE->IDR &(1<<3)))&&key==2) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下
25
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 }
key=1;
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 }
key=2;
{ } }
if((!(GPIOE->IDR &(1<<3)))&&key==3) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下 { }
if((!(GPIOE->IDR &(1<<3)))&&key==4) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下 { }
if((!(GPIOE->IDR &(1<<3)))&&key==5) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下
26
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 key=3;
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 }
key=4;
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 }
key=5;
{ }
if((!(GPIOE->IDR &(1<<3)))&&key==6) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下 { }
if((!(GPIOE->IDR &(1<<3)))&&key==7) {
Delay_ms(20);//消抖
if(!(GPIOE->IDR &(1<<3)))//再次判断按键是否按下 {
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 }
key=8;
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 }
key=7;
while(!(GPIOE->IDR &(1<<3)));//等待按键松开 }
key=6;
} return key; }
1.3呼吸灯 #include\
27
//PB5--LED1
void HXD_Config(void) {
//打开B端口时钟 RCC->APB2ENR |=(1<<3); RCC->APB2ENR |=(1<<2); RCC->APB2ENR |=(1<<6); //配置为通用推挽输出
GPIOB->CRL &=~(0xf<<20);//清零 GPIOB->CRL |=(0x3<<20); GPIOB->ODR |=(1<<5);//灭1灯 GPIOE->CRL &=~(0xf<<20); GPIOE->CRL |=(0x3<<20); GPIOE->ODR |=(1<<5); GPIOA->CRL &=~(0xf<<20); GPIOA->CRL |=(0x3<<20); GPIOA->ODR |=(1<<5); GPIOA->CRL &=~(0xf<<24); GPIOA->CRL |=(0x3<<24); GPIOA->ODR |=(1<<6); }
void HX (void) { u32 i;
for(i=0;i<1000;i++) {
GPIOE->ODR &=~(1<<5);
28