发布网友 发布时间:2022-04-23 12:43
共1个回答
热心网友 时间:2023-10-13 22:01
/////////////////////////////////////////
//STCC52单片机12M晶振
//用P1、P2口来控制流水灯
//天天奕想 3509537@qq.com
/////////////////////////////////////////
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit KEY2 = P3^1;
sbit KEY2 = P3^2;
sbit KEY3 = P3^3;
uchar CountNum;//溢出次数计数
void delay(uint i)
{
while(i--);
}
void main()
{
uchar i;
TMOD = 0X01;//定时器0方式1
TH0 = (65536 - 50000) / 256;
TH0 = (65536 - 50000) % 256; //装初值,50ms溢出一次
TR0 = 0;//先不启动定时器0
ET0 = 1;//开定时器0中断
EA = 1;//开总中断
P1 = 0XFF;
P2 = 0XFF; //初始化,灯全灭
while(1)
{
if(KEY1 == 0)
{
delay(1000); //消抖
if(KEY1 == 0)
{
while(KEY1 == 0); //送手检测
for(i = 0; i < 8; i++)
{
P2 = 0x7f;
P2 >>= 1;
delay(10000);
}
for(i = 0; i < 8; i++)
{
P1 = 0x7f;
P1 >>= 1;
delay(10000);
}
TR0 = 1;//启动定时器0
while(CountNum <= 100); //等待5秒
TR0 = 0;//关定时器0
P1 = 0XFF;
P2 = 0XFF;
}
}
if(KEY2 == 0)
{
delay(1000); //消抖
if(KEY2 == 0)
{
while(KEY2 == 0); //送手检测
for(i = 0; i < 8; i++)
{
P1 = 0xfe;
P1 <<= 1;
delay(10000);
}
for(i = 0; i < 8; i++)
{
P2 = 0xfe;
P2 <<= 1;
delay(10000);
}
TR0 = 1;//启动定时器0
while(CountNum <= 100); //等待5秒
TR0 = 0;//关定时器0
P1 = 0XFF;
P2 = 0XFF;
}
}
if(KEY3 == 0)
{
delay(1000); //消抖
if(KEY3 == 0)
{
while(KEY3 == 0); //送手检测
for(i = 0; i < 8; i++)
{
P1 = 0x7f;
P2 = 0xfe;
P1 >>= 1;
P2 <<= 1;
delay(10000);
}
TR0 = 1;//启动定时器0
while(CountNum <= 100); //等待5秒
TR0 = 0;//关定时器0
P1 = 0XFF;
P2 = 0XFF;
}
}
}
}
void timer0() interrupt 1
{
TH0 = (65536 - 50000) / 256;
TH0 = (65536 - 50000) % 256; //装初值,50ms溢出一次
CountNum++;
}