一个按键控制LED亮灭!

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit d1=P1^0;
sbit key1=P3^4;
void delay(uint z)     
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}


void main()
{
   P3=0xff;
   while(1)
   {
   	  if(key1==0)
	   { delay(5);
	  if(key1==0)
	    d1=~d1;
	 while(!key1);	 
   }
   }

}
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于STM32F1系列的两个按键控制LED亮灭的程序,其中PA0和PA1分别连接到按键,PA8连接到LED灯。 ```c #include "stm32f10x.h" void GPIO_Configuration(void); void delay(uint32_t count); int main(void) { GPIO_Configuration(); while(1) { if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET) //按下第一个按键 { GPIO_SetBits(GPIOA, GPIO_Pin_8); //LED } else if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == RESET) //按下第二个按键 { GPIO_ResetBits(GPIOA, GPIO_Pin_8); //LED } } } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void delay(uint32_t count) { for(uint32_t i = 0; i < count; i++); } ``` 在该程序中,我们使用了GPIO_InitTypeDef结构体对GPIO进行初始化配置,PA0和PA1配置为输入上拉模式,PA8配置为推挽输出模式。在主函数中,我们通过GPIO_ReadInputDataBit函数读取PA0和PA1的状态,如果按下了其中一个按键,则分别判断是按下了哪一个按键,然后控制LED灯的亮灭。 需要注意的是,我们在主函数中没有使用延时函数来消抖,这可能会导致按键的误触发。建议在实际应用中添加相应的消抖程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值