现在遇到了难题 希望广大网友帮助我一下 利用52
单片机利用定时器2中断控制二极管 使其在按键按下后亮两秒后自动熄灭 程序复制如下 现在我可以让他在按键按下之后两秒钟闪烁一次 但是却无法改成如上形式 附代码如下
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
void delay (uint z);
sbit key = P3^7;
sbit led1 = P2^7;
sbit led2 = P2^6;
uchar num1 = 0;
void keyon ();
uchar m = 0,i,n = 0,q = 0,p;
void main()
{
TMOD = 0X11;
T2CON = 0;
TH0 = (65536 - 50000) / 256; //重装初值
TL0 = (65536 - 50000) % 256;
TH1 = (65536 - 50000) / 256; //重装初值
TL1 = (65536 - 50000) % 256;
TH2 = (65536 - 50000) / 256; //重装初值
TL2 = (65536 - 50000) % 256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
while(1)
{
keyon ();
}
}
//设置定时器0
void
timer1() interrupt 1
{
TH0 = (65536 - 50000) / 256; //重装初值
TL0 = (65536 - 50000) % 256;
m++ ;
if (m == 5) //12M晶振 定时5*50MS=250MS
{
m = 0;
P2=~ ( 0X01 << i ); //P1接8个LED
if( ++i == 6 )
i = 0;
}
}
//设置定时器1
void timer2() interrupt 3
{
TH1 = (65536 - 50000) / 256; //重装初值
TL1 = (65536 - 50000) % 256;
led1 = 0;
n++;
if (n == 5)
{
n = 0;
led1 = 1;
}
}
//设置定时器2
void timer3() interrupt 5
{
TF2 = 0;
TH2 = (65536 - 50000) / 256; //重装初值
TL2 = (65536 - 50000) % 256;
led2 = 0;
q++;
if (q == 10)
{
led2 = 1;
q = 0;
}
}
void keyon ()
{
if(key == 0) //检测是否有低电平输入
{
delay(30); //消除抖动
if(key == 0)
{
TR2 = 1;
}
}
}
//延时
void delay (uint z)
{
uint i, j;
for (i = z; i > 0; i--)
for (j = 110; j > 0; j--);
}
一周热门 更多>