1.按鍵k2實現一個LED1燈的翻轉 2.按鍵k3按下計時,松開按鍵,單片機串口發送按下的時間(單位ms) 3.LED2閃爍,周期為2s,其中亮1.5秒,滅0.5秒,要求時間準確。 第三個實現不了 # include<reg52.h>
# define uint unsigned int
# define uchar unsigned char
sbit key2=P3^4;
sbit key3=P3^6;
sbit LED1=P1^0;
sbit LED2=P1^1;
uint count1=0;
uint count2=0;
void delay(uint i)
{
while(--i);
}
void init()
{
TMOD=0x21;
TH0=0xfc;
TL0=0x17;
TH1=0xfd;
TL1=0xfd;
TR1=1;
TR0=1;
SM0=0;
SM1=1;
EA=1;
ET0=1;
ET1=1;
ES=1;
}
void main()
{
init();
while(1)
{
if(key2==0)
{
delay(10);
LED1=~LED1;
while(!key2);
}
if(key3==0)
{
while(!key3)
{
TH0=0xfc;
TL0=0x17;
count1++;
}
SBUF=count1;
if(TI==1)
{
TI=0;
count1=0;
}
}
}
}
void timer() interrupt 1
{
TH0=0xfc;
TL0=0x17;
count2++;
TF0=0;
if(count2==500)
LED2=~LED2;
if(count2==2000)
{
LED2=~LED2;
count2=0;
}
}
|