按键key0,led0灭,再按key0 led0亮,没按一次key0 led0都取反;key1同key0控制led1;key3则同时控制led0,led1;我把详细图文传上请大侠们看看,十分感谢
#include<stm32f10x_lib.h>
#include"sys.h"
#include"delay.h"
#include"usart.h"
#include"led.h"
#include"key.h"
int main(void)
{
u8 key;
Stm32_Clock_Init(9);//系统时钟72M
delay_init(72); //延时时钟初始化;
KEY_Init();
LED_Init();
while(1)
{
key=KEY_Scan();
if(key)
{
switch (key)
{
case 1: LED0=!LED0;break;
case 2: LED1=!LED1;break;
case 3ED0=!LED0;
LED1=!LED1;
break;
}
}
else
delay_ms(10);
}
}
#include <stm32f10x_lib.h>
#include"led.h"
void LED_Init(void)
{
RCC->APB2ENR|=1<<7; //启动F口时钟;
GPIOF->CRL&=0XF0FFFFFF;//配置PF.6口输出模式
GPIOF->CRL|=0X03000000;
GPIOF->ODR=1<<6;
GPIOF->CRL&=0X0FFFFFFF; //配置pf.7口输出模式
GPIOF->CRL|=0X30000000;
GPIOF->ODR=1<<7;
}
#include<stm32f10x_lib.h>
#include"key.h"
#include"delay.h"
void KEY_Init(void)
{
RCC->APB2ENR|=1<<2; //使能端口PA时钟;
GPIOA->CRL&=0xfffffff0; //PA.0输RU
GPIOA->CRL|=0x00000008;
GPIOA->ODR=1; // PA.0为上拉
GPIOA->CRH&=0xfffffff0;
GPIOA->CRH|=0x00000008;
GPIOA->ODR=1<<8;
RCC->APB2ENR|=1<<4; // //使能端口Pc时钟
GPIOC->CRH&=0xff0fffff;
GPIOC->CRH|=0x00800000;
GPIOC->ODR=1<<13;
/*GPIOD->CRL&=0xffff0fff;
GPIOD->CRL|=0x00008000;
GPIOC->ODR=1<<3; */
}
u8 KEY_Scan(void)
{
static u8 key_flag=1;
if(key_flag&&(key0==0||key1==0||key2==0))
{
delay_ms(10);
key_flag=0;
if (key0==0)
{
return 1;
}
if (key1==0)
{
return 2; }
if (key2==0)
{ return 3; }
}
if(key_flag==0||(key0==1&&key1==1&&key2==1))
;
return 0;
}
具体的请看附件