2 zdlhsq zdlhsq 于 2015.05.27 11:30 提问

哪位大神帮我注释这段代码,最好详细一点(AT89s**与霍尔元件测速报警应用)。有附电路的原理图 5C

#include
unsigned char code table[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xBF};
unsigned char code table2[12]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xff};
sbit CS3020=P1^0;
sbit SET=P2^4;
sbit UP=P2^5;
sbit DOWN=P2^6;
sbit OK=P2^7;
sbit SPK=P3^7; //·äÃùÆ÷
/*sbit SET=P1^4;
sbit UP=P1^5;
sbit DOWN=P1^6;
sbit OK=P1^7;*/
unsigned char flag,c,cm=50,k_ok,k_up,k_down,max_speed=60;
unsigned int speed,time;
delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
display(unsigned char wei,unsigned char number)
{
P0=0xff;
switch(wei)
{
case 1:P2=0xfe; break;
case 2:P2=0xfd; break;
case 3:P2=0xfb; break;
case 4:P2=0xf7; break;
case 5:P2=0xef; break;
case 6:P2=0xdf; break;
case 7:P2=0xbf; break;
case 8:P2=0x7f; break;
}
P0=table[number];

}
display2(unsigned char wei,unsigned char number)
{
P0=0xff;
switch(wei)
{
case 1:P2=0xfe; break;
case 2:P2=0xfd; break;
case 3:P2=0xfb; break;
case 4:P2=0xf7; break;
case 5:P2=0xef; break;
case 6:P2=0xdf; break;
case 7:P2=0xbf; break;
case 8:P2=0x7f; break;
}
P0=table2[number];

}
get_cs3020(void)
{
if(CS3020==0)
{
if(flag==1)
{
c++;
flag=0;
}
}
if(CS3020==1)
{
if(flag==0)
{
c++;
flag=1;
}
}
}
cspeed(void) //¼ÆËãËÙ¶È
{
speed=c*cm*1.8; // µ¥Î» *10m/h
}
key_display(unsigned char i)
{
unsigned char j;
for(j=0;j {
display(1,i/100);
delay(5);
display(2,i%100/10);
delay(5);
display(3,i%10);
delay(5);
}
P2=0xff;
delay(50);
}
key_ok(void)
{
k_ok=0;
if(OK!=1)
{
delay(5);
if(OK!=1)
{
while(OK!=1);
k_ok=1;
}
}
}
key_up(void)
{
k_up=0;
if(UP!=1)
{
delay(5);
if(UP!=1)
{
while(UP!=1);
k_up=1;
}
}
}
key_down(void)
{
k_down=0;
if(DOWN!=1)
{
delay(5);
if(DOWN!=1)
{
while(DOWN!=1);
k_down=1;
}
}
}
keyscan(void)
{
unsigned char sd,jl;
sd=max_speed;
jl=cm;
SET=1;
UP=1;
DOWN=1;
OK=1;
k_ok=0;
k_up=0;
k_down=0;
if(SET!=1)
{
delay(5);
if(SET!=1)
{
while(SET!=1) ;
while(k_ok!=1)
{
key_display(sd);
key_ok();
key_up();
key_down();
if(k_up==1) sd++;
else if(k_down==1) sd--;
SET=1;
if(SET!=1)
{
delay(5);
if(SET!=1)
{
while(SET!=1);
while(k_ok!=1)
{
key_display(jl);
key_ok();
key_up();
key_down();
if(k_up==1) jl++;
else if(k_down==1) jl--;
}
cm=jl;
}
}
}
max_speed=sd;
}
}
}
alarm(void)
{
if(speed/100>=max_speed) SPK=0;
else SPK=1;
}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
if(CS3020==0) flag=0;
if(CS3020==1) flag=1;
while(1)
{

keyscan();
alarm();
display(1,speed/10000);
delay(2);
display(2,speed%10000/1000);
delay(2);
display2(3,speed%1000/100);
delay(2);
display(4,speed%100/10);
delay(2);
}

}
void twoscend() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
get_cs3020();
time++;
// c++;
if(time==200)

{

cspeed();
c=0;
time=0;
}
}

2个回答

h966669
h966669   2015.05.27 12:51

#include //缺少< reg51.h >头文件
unsigned char code table[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xBF};
unsigned char code table2[12]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xff}; //数码管显示的变量
sbit CS3020=P1^0; //测速定义口
sbit SET=P2^4; //键盘按钮
sbit UP=P2^5;
sbit DOWN=P2^6;
sbit OK=P2^7;
sbit SPK=P3^7; //蜂鸣器
/*sbit SET=P1^4;
sbit UP=P1^5;
sbit DOWN=P1^6;
sbit OK=P1^7;*/
unsigned char flag,c,cm=50,k_ok,k_up,k_down,max_speed=60;
unsigned int speed,time;
delay(unsigned int z)//延时函数
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
display(unsigned char wei,unsigned char number)//数码管显示函数
{
P0=0xff;
switch(wei) //键盘扫描函数
{
case 1:P2=0xfe; break;
case 2:P2=0xfd; break;
case 3:P2=0xfb; break;
case 4:P2=0xf7; break;
case 5:P2=0xef; break;
case 6:P2=0xdf; break;
case 7:P2=0xbf; break;
case 8:P2=0x7f; break;
}
P0=table[number];

}
display2(unsigned char wei,unsigned char number)
{
P0=0xff;
switch(wei)
{
case 1:P2=0xfe; break;
case 2:P2=0xfd; break;
case 3:P2=0xfb; break;
case 4:P2=0xf7; break;
case 5:P2=0xef; break;
case 6:P2=0xdf; break;
case 7:P2=0xbf; break;
case 8:P2=0x7f; break;
}
P0=table2[number];

}
get_cs3020(void)//测速函数对输出的端口电平进行扫描
{
if(CS3020==0)
{
if(flag==1)
{
c++;
flag=0;
}
}
if(CS3020==1)
{
if(flag==0)
{
c++;
flag=1;
}
}
}
cspeed(void) //¼ÆËãËÙ¶È
{
speed=c*cm*1.8; // µ¥Î» *10m/h
}
key_display(unsigned char i)//键盘扫描时的显示函数
{
unsigned char j;
for(j=0;j {
display(1,i/100);
delay(5);
display(2,i%100/10);
delay(5);
display(3,i%10);
delay(5);
}
P2=0xff;
delay(50);
}
key_ok(void)
{
k_ok=0;
if(OK!=1)
{
delay(5);
if(OK!=1)
{
while(OK!=1);
k_ok=1;
}
}
}
key_up(void)
{
k_up=0;
if(UP!=1)
{
delay(5);
if(UP!=1)
{
while(UP!=1);
k_up=1;
}
}
}
key_down(void)
{
k_down=0;
if(DOWN!=1)
{
delay(5);
if(DOWN!=1)
{
while(DOWN!=1);
k_down=1;
}
}
}
keyscan(void)
{
unsigned char sd,jl;
sd=max_speed;
jl=cm;
SET=1;
UP=1;
DOWN=1;
OK=1;
k_ok=0;
k_up=0;
k_down=0;
if(SET!=1)
{
delay(5);
if(SET!=1)
{
while(SET!=1) ;
while(k_ok!=1)//键盘输入结果显示
{
key_display(sd);
key_ok();
key_up();
key_down();
if(k_up==1) sd++;
else if(k_down==1) sd--;
SET=1;
if(SET!=1)
{
delay(5);
if(SET!=1)
{
while(SET!=1);
while(k_ok!=1)
{
key_display(jl);
key_ok();
key_up();
key_down();
if(k_up==1) jl++;
else if(k_down==1) jl--;
}
cm=jl;
}
}
}
max_speed=sd;
}
}
}
alarm(void)//报警触发
{
if(speed/100>=max_speed) SPK=0;
else SPK=1;
}
void main()//主函数
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
if(CS3020==0) flag=0;
if(CS3020==1) flag=1;
while(1)
{

keyscan();
alarm();
display(1,speed/10000);
delay(2);
display(2,speed%10000/1000);
delay(2);
display2(3,speed%1000/100);
delay(2);
display(4,speed%100/10);
delay(2);
}

}
void twoscend() interrupt 1//2秒定时器
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
get_cs3020();//用定时器计算扫描到的霍尔端口输出电平变化的时间
time++;
// c++;
if(time==200)

{

cspeed();
c=0;
time=0;
}
}

u011089608
u011089608 回复zdlhsq: 用来构成数据传递的通道咯。不然你以为无端端它就按照你想的去动了吗。。。
2 年多之前 回复
zdlhsq
zdlhsq 请问一下,按键相关的函数、定义,在这个程序中有什么作用?
2 年多之前 回复
u011089608
u011089608   2015.05.27 14:28

用来构成数据传递的通道咯。不然你以为无端端它就按照你想的去动了吗。。。

Csdn user default icon
上传中...
上传图片
插入图片