#include
sbit LED1 = P1^1; //LED1
sbit LED2 = P2^1; //LED2
sbit Key1 = P3^6; //按键
unsigned char Flag = 0; //标志
/*
* 延时1MS子函数
*/
void Delay_1ms(unsigned int Time)
{
unsigned int x,y;
for(x = Time; x > 0; x--)
for(y = 120; y > 0; y--);
}
/*
* 按键子函数
*/
void Key_Scong(void)
{
if(Key1 == 0) //判断按键按下
{
Delay_1ms(10); //延时去抖动
if(Key1 == 0) //确定按键按下
{
Flag = 1; //标志直位
}
while(Key1 == 0); //等待按键松手
}
}
/*
* 主函数
*/
int main(void)
{
while(1)
{
Key_Scong();
if(Flag == 1) //判断标志直位后
{
LED1 = 1; //P1.0 = 1;
Delay_1ms(1000); //延时1s
Delay_1ms(1000); //延时1s
LED2 = 1;
Delay_1ms(1000);
Delay_1ms(1000);
LED1 = 1;
LED2 = 1;
Delay_1ms(1000);
Delay_1ms(1000);
LED1 = 0;
LED2 = 0;
Flag = 0; //关闭标志
}
else
{
LED1 = 0;
LED2 = 0;
}
}
}
很简单啊:
include
sbit key=P3^0;
void main()
{
if(key==0)
{
P1=1;
//(延时自己能写吧)
P1=0;
P2=1;
//(延时2s)
P1=1;
//延时2s
}
}
# include
# define uint unsigned int
# define uchar unsigned char
sbit sw = P1^7; //设定开关连接到P1.7口
sbit led1 = P1^0;
sbit led2 = P2^0;
void delay1() //延时2s函数
{
uint x;
uchar y;
for(x=0; x<2000; x++)
for(y=0; y<124; y++);
}
void delay2() //延时5ms函数
{
uchar x;
uchar y;
for(x=0; x<5; x++)
for(y=0; y<124; y++);
}
void main(void)
{
led1 = 0; //初始化
led2 = 0;
delay1();
while(1)
{
if(sw == 0)
{
delay2(); //按键消抖
if(sw == 0)
{
while(!sw); //按键松手检测
led1 = 1;
delay1();
led1 = 0;
led2 = 1;
delay1();
led2 = 0;
led1 = 1;
led2 = 1;
delay1();
led1 = 0;
led2 = 0;
}
}
}
}
#include
sbit KEY=P3^2;
sbit LED1=P1^0;
sbit LED2=P2^0;
#define uint unsigned int
void delay(uint x);
void main()
{
if(KEY==0)
{
LED1=1;
delay(200);
LED2=1;
delay(200);
LED1=1;
LED2=1;
delay(200);
}
}
void delay(uint x)
{
uint i,j;
for(i=0;i
}