K1、K2分别控制定时器T0、T1的启动停止, T0每2秒beep(蜂鸣器),T1每秒闪烁灯。求C语言单片机程序代码
展开全部
#include<reg51.h>
sbit Beep = P1^0;
sbit Led = P1^1;
sbit K1 = P2^0;
sbit K2 = P2^1;
void ConfigTimer()
{
TMOD = 0x11;
TH1 = 0xFC;
TL1 = 0c18;
TH0 = 0xFC;
TL0 = 0x18;
ET0 = 1;
ET1 = 1;
EA = 1;
}
void delay(int z)
{
int i,j;
for(i=0; i<z; i++)
for(j=0; j<110; j++);
}
void main()
{
ConfigTimer();
while(1)
{
if(K1 == 0)
{
delay(10);
if(K1 == 0)
{
TR0 =~TR0;
while(!K1);
}
}
if(K2 == 0)
{
delay(10);
if(K2 == 0)
{
TR1 =~TR1;
while(!K2);
}
}
}
}
void timer0() interrupt 1
{
static unsigned int t = 0;
TH0 = 0xFC;
TL0 = 0x18;
t++;
if(t < 2000)
{
if(t < 200)
{
Beep = 0;
}
else
{
Beep = 1;
}
}
else
{
t = 0;
}
}
void timer0() interrupt 3
{
static unsigned int t = 0;
TH1 = 0xFC;
TL1 = 0x18;
t++;
if(t >= 1000)
{
Led = ~Led;
t = 0;
}
}
我刚写出来,,没有试验,你编译下,更改引脚定义,试试,,应该是可以的
sbit Beep = P1^0;
sbit Led = P1^1;
sbit K1 = P2^0;
sbit K2 = P2^1;
void ConfigTimer()
{
TMOD = 0x11;
TH1 = 0xFC;
TL1 = 0c18;
TH0 = 0xFC;
TL0 = 0x18;
ET0 = 1;
ET1 = 1;
EA = 1;
}
void delay(int z)
{
int i,j;
for(i=0; i<z; i++)
for(j=0; j<110; j++);
}
void main()
{
ConfigTimer();
while(1)
{
if(K1 == 0)
{
delay(10);
if(K1 == 0)
{
TR0 =~TR0;
while(!K1);
}
}
if(K2 == 0)
{
delay(10);
if(K2 == 0)
{
TR1 =~TR1;
while(!K2);
}
}
}
}
void timer0() interrupt 1
{
static unsigned int t = 0;
TH0 = 0xFC;
TL0 = 0x18;
t++;
if(t < 2000)
{
if(t < 200)
{
Beep = 0;
}
else
{
Beep = 1;
}
}
else
{
t = 0;
}
}
void timer0() interrupt 3
{
static unsigned int t = 0;
TH1 = 0xFC;
TL1 = 0x18;
t++;
if(t >= 1000)
{
Led = ~Led;
t = 0;
}
}
我刚写出来,,没有试验,你编译下,更改引脚定义,试试,,应该是可以的
追问
并不行
追答
#include
sbit Beep = P1^0;
sbit Led = P1^1;
sbit K1 = P2^0;
sbit K2 = P2^1;
void ConfigTimer()
{
TMOD = 0x11;
TH1 = 0xFC;
TL1 = 0x18;
TH0 = 0xFC;
TL0 = 0x18;
ET0 = 1;
ET1 = 1;
TR0 = 0;
TR1 = 0;
EA = 1;
}
void key1_scan()//按键扫描函数
{
static unsigned int uiKeyTimeCnt1 = 0;
static bit ucKeyLock1 = 0;
if(K1==1)
{
ucKeyLock1=0;
uiKeyTimeCnt1=0;
}
else if(ucKeyLock1==0)
{
++uiKeyTimeCnt1;
if(uiKeyTimeCnt1>500)
{
uiKeyTimeCnt1=0;
ucKeyLock1=1;
TR0 =~TR0;
}
}
}
void key2_scan()//按键扫描函数
{
static unsigned int uiKeyTimeCnt2 = 0;
static bit ucKeyLock2 = 0;
if(K2==1)
{
ucKeyLock2=0;
uiKeyTimeCnt2=0;
}
else if(ucKeyLock2==0)
{
++uiKeyTimeCnt2;
if(uiKeyTimeCnt2>500)
{
uiKeyTimeCnt2=0;
ucKeyLock2=2;
TR1 =~TR1;
}
}
}
void main()
{
ConfigTimer();
while(1)
{
key2_scan();
key1_scan();
}
}
仿真通过了,你拿到程序有问题,自己也可以改改啊,又不是大问题。。。 中断服务程序没有变,按键程序更改了一下,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询