求写程序!我的是89C52单片机,想用两个按键和一个LED来实现,按下按键1点亮LED,按下按键2熄灭LED,两个按键另一端是接5V,LED另一端也是接5V。最好配上原理图。感激不尽
提醒一下:两个按键另一端接5V地!!!!!
电路参考如下:
代码参考如下:
#include<reg52h>
sbit LED = P2^7;
#define ON 0
#define OFF 1
void main()
{
unsigned char keyVal;
while(1)
{
keyVal=P3^0xFF;
if(keyVal)
{
if(keyVal==0x02) LED=ON;
else if(keyVal == 0x20) LED=OFF;
}
}
}
3说明一下,此按钮不需要去抖动,原因在于每个按钮只负责灯的一种状态,
如果一个按钮要负责两个状态的切换,就需要去抖。
希望能帮上你!!!!!!!
主函数不完整,你这样写主函数运行完还没等到中断程序就退出去了。在主函数最下面加一个while(1);就可以了,酱紫:
void main()
{
t0=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1);
}
另:num最好处理一下,要不然增加到256就会溢出了。
#include <AT89X52H>
#include <intrinsh>
#define REST P3_4
#define SCLK P3_7
#define DATA P3_6
unsigned char code displayCode[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
unsigned char code controladdress[]={0x80,0x82,0x84,0x86,0x88,
0x8a,0x8c,0x8e,0x90,0xbe};
unsigned char hour,minute,second;
unsigned char day,month,week,year;
unsigned char clockhour,clockminute;
unsigned int num;
unsigned char select,oneminute;
bit bdata modeselect;
void delay();
void writetime();
void display(unsigned char temp,unsigned char mode);
void sound()
{
while(num<2000)
{
P3_0=0;
}
P3_0=1;
}
void initial()
{
P0=0;
TMOD=0x22;
IE=0xCF;
IT0=1;
IT1=1;
T2CON=0;
RCAP2L=0xf0;
RCAP2H=0x1f;
TH2=0x1c;
TL2=0xf0;
IP=0X06;
TH0=0x48;
TL0=0x48;
TH1=0X48;
TR0=1;
PCON=0x01;
hour=12;
minute=0;
second=0;
num=0;
select=0;
clockhour=12;
clockminute=0;
modeselect=0;
day=16;
month=8;
week=7;
year=9;
}
void interrupt0()interrupt 0 using 3
{
TR1=1;
select++;
if(select==8)
{
select=0;
}
if(select==1)
ET2=1;
oneminute=second-1;
}
void timer0()interrupt 1 using 3
{
num++;
if(num==5000)
{
num=0;
second++;
}
if(second>=60)
{
minute++;
second=0;
}
if(minute>=60)
{
hour++;
minute=0;
}
if(hour>=24)
hour=0;
}
void interrupt1()interrupt 2 using 3
{
oneminute=second-1;
switch(select)
{
case 4:{
hour++;
if(hour>=24)
hour=0;
break;
}
case 5:{
minute++;
if(minute>=60)
minute=0;
break;
}
case 6:{
second++;
if(second>=60)
second=0;
break;
}
case 2:{
clockhour++;
if(clockhour>=24)
clockhour=0;
break;
}
case 3:{
clockminute++;
if(clockminute>=60)
clockminute=0;
break;
}
default:{
modeselect=0;
ET2=!ET2;
break;
}
}
}
void timer1()interrupt 3 using 3
{
if(select)
{
if(second==oneminute)
{
select=0;
TR1=0;
}
}
}
void timer2()interrupt 5 using 3
{
sound();
TF2=0;
if(minute!=clockminute||hour!=clockhour)
TR2=0;
}
void delay()
{
unsigned char i=255;
while(--i)
{
;
}
}
void display(unsigned char temp,unsigned char mode)
{
if(temp==4||temp==2)
P0=0;
else
P0=1;
if(mode)
P1=displayCode[hour/10];
else
P1=displayCode[clockhour/10];
delay();
if(temp==4||temp==2)
P0=0;
else
P0=2;
if(mode)
P1=displayCode[hour%10];
else
P1=displayCode[clockhour%10];
delay();
if(temp==1||temp==2||temp==3)
P0=0;
else
P0=4;
P1=0xbf;
delay();
if(temp==5||temp==3)
P0=0;
else
P0=8;
if(mode)
P1=displayCode[minute/10];
else
P1=displayCode[clockminute/10];
delay();
if(temp==5||temp==3)
P0=0;
else
P0=16;
if(mode)
P1=displayCode[minute%10];
else
P1=displayCode[clockminute%10];
delay();
if(temp==1||temp==2||temp==3)
P0=0;
else
P0=32;
P1=0xbf;
delay();
if(temp==6)
P0=0;
else
P0=64;
if(mode)
P1=displayCode[second/10];
else
P1=displayCode[0];
delay();
if(temp==6)
P0=0;
else
P0=128;
if(mode)
{
if(ET2)
P1=displayCode[second%10]+128;
else
P1=displayCode[second%10];
}
else
P1=displayCode[0];
delay();
}
void write(unsigned char Bdata)
{
unsigned char i=8;
for(;i>0;i--)
{
if(Bdata&0x01)
DATA=1;
else
DATA=0;
SCLK=1;
SCLK=0;
Bdata>>=1;
}
}
void writedata(unsigned char address,Bdata)
{
REST=0;
SCLK=0;
_nop_();
REST=1;
write(address);
write(Bdata);
REST=0;
}
unsigned char read(unsigned char Bdata)
{
unsigned char tdata=0,i=8;
REST=0;
SCLK=0;
_nop_();
REST=1;
write(Bdata);
for(;i>0;i--)
{
tdata>>=1;
if(DATA)
tdata=tdata|0x80;
else
tdata|=0x00;
SCLK=1;
SCLK=0;
}
return tdata;
}
void writetime()
{
unsigned char i,time,temp[3];
temp[0]=second;
temp[1]=minute;
temp[2]=hour;
for(i=0;i<3;i++)
{
time=temp[i]%10;
time|=(temp[i]/10)<<4;
writedata(controladdress[i],time);
}
}
void writeyear()
{
unsigned char i,year0,temp[4];
temp[0]=day;
temp[1]=month;
temp[2]=week;
temp[3]=year;
for(i=0;i<4;i++)
{
year0=temp[i]%10;
year0|=(temp[i]/10)<<4;
writedata(controladdress[i+3],year0);
}
}
void readtime()
{
unsigned char i,time,temp[3];
for(i=0;i<3;i++)
{
temp[i]=read(1+controladdress[i]);
time=temp[i]&0x0f;
time+=(temp[i]>>4)10;
temp[i]=time;
}
second=temp[0];
minute=temp[1];
hour=temp[2];
}
void readyear()
{
unsigned char i,year0,temp[4];
for(i=0;i<4;i++)
{
temp[i]=read(1+controladdress[i+3]);
year0=temp[i]&0x0f;
year0+=(temp[i]>>4)10;
temp[i]=year0;
}
day=temp[0];
month=temp[1];
week=temp[2];
year=temp[3];
}
void main()
{
initial();
sound();
writedata(0x8e,0x00);
writedata(0x84,0x12);
writedata(0x8f,0x00);
readtime();
writeyear();
readyear();
while(1)
{
switch(select)
{
case 0:{
display(0,1);
break;
}
case 1:{
if(num<=2500)
{
display(1,0);
}
else
{
display(0,0);
}
break;
}
case 2:{
if(num<=2500)
{
display(2,0);
}
else
{
display(0,0);
}
break;
}
case 3:{
if(num<=2500)
{
display(3,0);
}
else
{
display(0,0);
}
break;
}
case 4:{
if(num<=2500)
{
display(4,1);
}
else
{
display(0,1);
}
break;
}
case 5:{
if(num<=2500)
{
display(5,1);
}
else
{
display(0,1);
}
break;
}
case 6:{
if(num<=2500)
{
display(6,1);
}
else
{
display(0,1);
}
break;
}
case 7:{
writetime();
writetime();
writetime();
select=0;
break;
}
default:break;
}
if(second==0)
{
if(minute==0||minute==30)
sound();
}
if(minute==clockminute&&hour==clockhour)
{
TR2=1;
modeselect=1;
}
if(modeselect==1&&hour==clockhour&&!((minute-clockminute)%5))
TR2=1;
}
}
以上就是关于求写程序!89C52单片机,按按键1点亮LED,按按键2熄灭LED,两个按键另一端是接5V,LED另一端也是接5V。全部的内容,包括:求写程序!89C52单片机,按按键1点亮LED,按按键2熄灭LED,两个按键另一端是接5V,LED另一端也是接5V。、89c52单片机T0定时器中断程序问题、求89c52单片机 C语言实现数字时钟程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)