请大神简要帮我分析下这个程序,为什么仿真时按键没反应?
时间:10-02
整理:3721RD
点击:
这是一个信号发生器的程序,采用的时stc89c52单片机做的,用仿真软件仿真,幅值,频率没有变化为初始值,按键扫描,工作不正常,由于本程序并不是本人编写,所以读起来有些困难,希望贴友帮我解决一下,谢谢了。
#include <REGX52.H>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P3^0;
sbit rw=P3^1;
sbit lcden=P3^2;
uchar code table[]={"Volt"};
uchar code table2[]={'0','1','2','3','4','5','6','7','8','9'};
uchar code tosin[256]={0x80,0x83,0x86,0x89,0x8d,0x90,0x93,0x96,0x99,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,
0xac,0xb1,0xb4,0xb7,0xba,0xbc,0xbf,0xc2,0xc5,0xc7,0xca,0xcc,0xcf,0xd1,0xd4,
0xd6,0xd8,0xda,0xdd,0xdf,0xc1,0xc3,0xc5,0xc7,0xc9,0xca,0xcc,0xcc,0xcf,0xf1,
0xf2,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfd,0xfc,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfc,0xfd,0xfd,0xfc,0xfb,
0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,0xcf,0xcc,0xca,0xc9,0xc7,0xc5,
0xc3,0xc1,0xdc,0xdd,0xda,0xd8,0xd6,0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc5,0xc2,
0xbf,0xbc,0xba,0xb7,0xb4,0xb1,0xac,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,0x96,
0x93,0x90,0x8d,0x89,0x86,0x83,0x80,0x80,0x7c,0x79,0x76,0x72,0x6f,0x6c,0x69,
0x66,0x63,0x60,0x5d,0x5a,0x57,0x55,0x51,0x4c,0x4c,0x48,0x45,0x43,0x40,0x3d,
0x3a,0x38,0x35,0x33,0x30,0x2c,0x2b,0x29,0x27,0x25,0x22,0x20,0x1c,0x1c,0x1a,
0x18,0x16,0x15,0x13,0x11,0x10,0x0c,0x0d,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,
0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0d,0x0c,
0x10,0x11,0x13,0x15,0x16,0x18,0x1a,0x1c,0x1c,0x20,0x22,0x25,0x27,0x29,0x2b,
0x2c,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,0x48,0x4c,0x4c,0x51,0x55,
0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x72,0x76,0x79,0x7c,0x80};
uchar b=0,c=0,d=0,e=0,i,k,tl,th;
int ww=0,qw=0,bw=9,sw=3,gw=3,n=20,chh=50;
int t,f,m,choice=1,zk=50;
void delay(unsigned int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;i++);
}
void delay1(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void write_com(uchar com)
{
P2=com;
rs=0;
lcden=0;
delay1(10);
lcden=1;
delay1(10);
lcden=0;
}
void write_date(uchar date)
{
P2=date;
rs=1;
lcden=0;
delay1(10);
lcden=1;
delay1(10);
lcden=0;
}
void init()
{
write_com(0x38);
delay1(20);
write_com(0x0c);
delay1(20);
write_com(0x06);
delay1(20);
write_com(0x80+0x00);
delay1(20);
}
//??ê?oˉêy
void display()
{
int i;
for(i=0;i<3;i++)
write_date(table[i]);
write_com(0x80+0x05);
write_date(table2[chh/10]);
write_com(0x80+0x06);
write_date('.');
write_com(0x80+0x07);
write_date(table2[chh%10]);
write_com(0x80+0x08);
write_date('V');
write_com(0x02);
write_com(0x80+0x40);
write_date('F');
write_com(0x80+0x42);
write_date('c');
write_com(0x80+0x43);
write_date(table2[ww]);
write_com(0x80+0x44);
write_date(table2[qw]);
write_com(0x80+0x45);
write_date(table2[bw]);
write_com(0x80+0x46);
write_date(table2[sw]);
write_com(0x80+0x47);
write_date(table2[gw]);
write_com(0x80+0x48);
write_date('H');
write_com(0x80+0x49);
write_date('z');
write_com(0x80+0x50);
}
void key1(void)
{
if(choice<4)
choice=choice+1;
else
choice=1;
}
void key2(void)
{
if(chh!=50)
chh++;
else chh=chh;
}
void key3(void)
{
if(chh!=0)
chh--;
}
void key4(void)
{
}
void key5(void)
{
if(n<=3000)
n=n+100;
}
void key6(void)
{
if(n!=20)
n=n-100;
}
void key7(void)
{
if(zk<100)
zk=zk+1;
else
zk=0;
}
void key8(void)
{
if(zk>=1)
zk=zk-1;
else
zk=0;
}
void jisuan(void)
{
TR0=0;
f=n;
t=1000000/f;
th=(65536-t)/256;
tl=(65536-t)%256;
ww=f/10000;
f=f%10000;
qw=f/1000;
f=f%1000;
bw=f/100;
f=f%100;
sw=f/10;
gw=f%10;
TR0=1;
}
//?ü?ìé¨?è
void judge(void)
{
uchar temp;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x7e:key1();
break;
case 0xbe:key2();
break;
case 0xde:key3();
break;
case 0xee:key4();
break;
}
}
}
P1=0xfd;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x7d:key5();
break;
case 0xbd:key6();
break;
case 0xdd:key7();
break;
case 0xed:key8();
break;
}
}
}
}
void main(void)
{
init();
TMOD=0x01;
TR0=1;
th=-t/256;
tl=-t%156;
TH0=th;
TL0=tl;
ET0=1;
EA=1;
while(1)
{
jisuan();
display();
judge();
}
}
void time0_int(void) interrupt 1
{
TR0=0;
if(choice==1)
{
P0=tosin[b]*chh/50;
b++;
}
else if(choice==2)
{
if(c<=128)P0=c*chh/50;
else P0=(255-c)*chh/50;
c++;
}
else if(choice==3)
{
k=zk*256/100;
d++;
if(d<=k)P0=0x00;
else P0=0xff*chh/50;
}
TH0=th;
TL0=tl;
TR0=1;
}
#include <REGX52.H>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P3^0;
sbit rw=P3^1;
sbit lcden=P3^2;
uchar code table[]={"Volt"};
uchar code table2[]={'0','1','2','3','4','5','6','7','8','9'};
uchar code tosin[256]={0x80,0x83,0x86,0x89,0x8d,0x90,0x93,0x96,0x99,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,
0xac,0xb1,0xb4,0xb7,0xba,0xbc,0xbf,0xc2,0xc5,0xc7,0xca,0xcc,0xcf,0xd1,0xd4,
0xd6,0xd8,0xda,0xdd,0xdf,0xc1,0xc3,0xc5,0xc7,0xc9,0xca,0xcc,0xcc,0xcf,0xf1,
0xf2,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfd,0xfc,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfc,0xfd,0xfd,0xfc,0xfb,
0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,0xcf,0xcc,0xca,0xc9,0xc7,0xc5,
0xc3,0xc1,0xdc,0xdd,0xda,0xd8,0xd6,0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc5,0xc2,
0xbf,0xbc,0xba,0xb7,0xb4,0xb1,0xac,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,0x96,
0x93,0x90,0x8d,0x89,0x86,0x83,0x80,0x80,0x7c,0x79,0x76,0x72,0x6f,0x6c,0x69,
0x66,0x63,0x60,0x5d,0x5a,0x57,0x55,0x51,0x4c,0x4c,0x48,0x45,0x43,0x40,0x3d,
0x3a,0x38,0x35,0x33,0x30,0x2c,0x2b,0x29,0x27,0x25,0x22,0x20,0x1c,0x1c,0x1a,
0x18,0x16,0x15,0x13,0x11,0x10,0x0c,0x0d,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,
0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0d,0x0c,
0x10,0x11,0x13,0x15,0x16,0x18,0x1a,0x1c,0x1c,0x20,0x22,0x25,0x27,0x29,0x2b,
0x2c,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,0x48,0x4c,0x4c,0x51,0x55,
0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x72,0x76,0x79,0x7c,0x80};
uchar b=0,c=0,d=0,e=0,i,k,tl,th;
int ww=0,qw=0,bw=9,sw=3,gw=3,n=20,chh=50;
int t,f,m,choice=1,zk=50;
void delay(unsigned int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;i++);
}
void delay1(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void write_com(uchar com)
{
P2=com;
rs=0;
lcden=0;
delay1(10);
lcden=1;
delay1(10);
lcden=0;
}
void write_date(uchar date)
{
P2=date;
rs=1;
lcden=0;
delay1(10);
lcden=1;
delay1(10);
lcden=0;
}
void init()
{
write_com(0x38);
delay1(20);
write_com(0x0c);
delay1(20);
write_com(0x06);
delay1(20);
write_com(0x80+0x00);
delay1(20);
}
//??ê?oˉêy
void display()
{
int i;
for(i=0;i<3;i++)
write_date(table[i]);
write_com(0x80+0x05);
write_date(table2[chh/10]);
write_com(0x80+0x06);
write_date('.');
write_com(0x80+0x07);
write_date(table2[chh%10]);
write_com(0x80+0x08);
write_date('V');
write_com(0x02);
write_com(0x80+0x40);
write_date('F');
write_com(0x80+0x42);
write_date('c');
write_com(0x80+0x43);
write_date(table2[ww]);
write_com(0x80+0x44);
write_date(table2[qw]);
write_com(0x80+0x45);
write_date(table2[bw]);
write_com(0x80+0x46);
write_date(table2[sw]);
write_com(0x80+0x47);
write_date(table2[gw]);
write_com(0x80+0x48);
write_date('H');
write_com(0x80+0x49);
write_date('z');
write_com(0x80+0x50);
}
void key1(void)
{
if(choice<4)
choice=choice+1;
else
choice=1;
}
void key2(void)
{
if(chh!=50)
chh++;
else chh=chh;
}
void key3(void)
{
if(chh!=0)
chh--;
}
void key4(void)
{
}
void key5(void)
{
if(n<=3000)
n=n+100;
}
void key6(void)
{
if(n!=20)
n=n-100;
}
void key7(void)
{
if(zk<100)
zk=zk+1;
else
zk=0;
}
void key8(void)
{
if(zk>=1)
zk=zk-1;
else
zk=0;
}
void jisuan(void)
{
TR0=0;
f=n;
t=1000000/f;
th=(65536-t)/256;
tl=(65536-t)%256;
ww=f/10000;
f=f%10000;
qw=f/1000;
f=f%1000;
bw=f/100;
f=f%100;
sw=f/10;
gw=f%10;
TR0=1;
}
//?ü?ìé¨?è
void judge(void)
{
uchar temp;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x7e:key1();
break;
case 0xbe:key2();
break;
case 0xde:key3();
break;
case 0xee:key4();
break;
}
}
}
P1=0xfd;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x7d:key5();
break;
case 0xbd:key6();
break;
case 0xdd:key7();
break;
case 0xed:key8();
break;
}
}
}
}
void main(void)
{
init();
TMOD=0x01;
TR0=1;
th=-t/256;
tl=-t%156;
TH0=th;
TL0=tl;
ET0=1;
EA=1;
while(1)
{
jisuan();
display();
judge();
}
}
void time0_int(void) interrupt 1
{
TR0=0;
if(choice==1)
{
P0=tosin[b]*chh/50;
b++;
}
else if(choice==2)
{
if(c<=128)P0=c*chh/50;
else P0=(255-c)*chh/50;
c++;
}
else if(choice==3)
{
k=zk*256/100;
d++;
if(d<=k)P0=0x00;
else P0=0xff*chh/50;
}
TH0=th;
TL0=tl;
TR0=1;
}
扫描电路不正常工作
你8个按键既没有定义I/O口,按键程序中也没有对响应电平作判断的语句。
多谢
你用的什么仿真软件,如果用Multisim的话,前面引用的头文件就有问题,应该引用htc.h