求单片机高手帮我逐句解释一下这个程序 谢谢了
#include<reg52.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedintsbi...
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit clock=P1^0;
sbit d_in=P1^1;
sbit d_out=P1^2;
sbit _cs=P1^3;
uchar a1,b1,c1,d1;
float sum,sum1;
double sum_final1;
double sum_final;
uchar duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar wei[]={0xf7,0xfb,0xfd,0xfe};
void delay(unsigned char b) //50us
{
unsigned char a;
for(;b>0;b--)
for(a=22;a>0;a--);
}
void display(uchar a,uchar b,uchar c,uchard)
{
P0=duan[a]|0x80;
P2=wei[0];
delay(5);
P2=0xff;
P0=duan[b];
P2=wei[1];
delay(5);
P2=0xff;
P0=duan[c];
P2=wei[2];
delay(5);
P2=0xff;
P0=duan[d];
P2=wei[3];
delay(5);
P2=0xff;
}
uint read(uchar port)
{
uchar i,al=0,ah=0;
unsignedlong ad;
clock=0;
_cs=0;
port<<=4;
for(i=0;i<4;i++)
{
d_in=port&0x80;
clock=1;
clock=0;
port<<=1;
}
d_in=0;
for(i=0;i<8;i++)
{
clock=1;
clock=0;
}
_cs=1;
delay(5);
_cs=0;
for(i=0;i<4;i++)
{
clock=1;
ah<<=1;
if(d_out)ah|=0x01;
clock=0;
}
for(i=0;i<8;i++)
{
clock=1;
al<<=1;
if(d_out)al|=0x01;
clock=0;
}
_cs=1;
ad=(uint)ah;
ad<<=8;
ad|=al;
return(ad);
}
void main()
{
ucharj;
sum=0;sum1=0;
sum_final=0;
sum_final1=0;
while(1)
{
for(j=0;j<128;j++)
{
sum1+=read(1);
display(a1,b1,c1,d1);
}
sum=sum1/128;
sum1=0;
sum_final1=(sum/4095)*5;
sum_final=sum_final1*1000;
a1=(int)sum_final/1000;
b1=(int)sum_final%1000/100;
c1=(int)sum_final%1000%100/10;
d1=(int)sum_final%10;
display(a1,b1,c1,d1);
} 展开
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit clock=P1^0;
sbit d_in=P1^1;
sbit d_out=P1^2;
sbit _cs=P1^3;
uchar a1,b1,c1,d1;
float sum,sum1;
double sum_final1;
double sum_final;
uchar duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar wei[]={0xf7,0xfb,0xfd,0xfe};
void delay(unsigned char b) //50us
{
unsigned char a;
for(;b>0;b--)
for(a=22;a>0;a--);
}
void display(uchar a,uchar b,uchar c,uchard)
{
P0=duan[a]|0x80;
P2=wei[0];
delay(5);
P2=0xff;
P0=duan[b];
P2=wei[1];
delay(5);
P2=0xff;
P0=duan[c];
P2=wei[2];
delay(5);
P2=0xff;
P0=duan[d];
P2=wei[3];
delay(5);
P2=0xff;
}
uint read(uchar port)
{
uchar i,al=0,ah=0;
unsignedlong ad;
clock=0;
_cs=0;
port<<=4;
for(i=0;i<4;i++)
{
d_in=port&0x80;
clock=1;
clock=0;
port<<=1;
}
d_in=0;
for(i=0;i<8;i++)
{
clock=1;
clock=0;
}
_cs=1;
delay(5);
_cs=0;
for(i=0;i<4;i++)
{
clock=1;
ah<<=1;
if(d_out)ah|=0x01;
clock=0;
}
for(i=0;i<8;i++)
{
clock=1;
al<<=1;
if(d_out)al|=0x01;
clock=0;
}
_cs=1;
ad=(uint)ah;
ad<<=8;
ad|=al;
return(ad);
}
void main()
{
ucharj;
sum=0;sum1=0;
sum_final=0;
sum_final1=0;
while(1)
{
for(j=0;j<128;j++)
{
sum1+=read(1);
display(a1,b1,c1,d1);
}
sum=sum1/128;
sum1=0;
sum_final1=(sum/4095)*5;
sum_final=sum_final1*1000;
a1=(int)sum_final/1000;
b1=(int)sum_final%1000/100;
c1=(int)sum_final%1000%100/10;
d1=(int)sum_final%10;
display(a1,b1,c1,d1);
} 展开
3个回答
展开全部
#include<reg52.h> //字头,表示如果涉及到的函数从这两个后缀名.H的文件里调用。
#include<intrins.h>
#define uchar unsigned char//方便定义函数无符号型应该用UCHAR表示为方便直接用CHAR表示了。
#define uint unsigned int//整形同上解释
sbit clock=P1^0; //定义CLOCK接到单片机P0_1口,请注意这里用P0^1,就是因为包含了REG51.H。如果不包含就不能这么使用。
sbit d_in=P1^1;
sbit d_out=P1^2;
sbit _cs=P1^3;
//以下就是定义几个变量
uchar a1,b1,c1,d1;
float sum,sum1;
double sum_final1;
double sum_final;
//数码管定义段选和位选
uchar duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar wei[]={0xf7,0xfb,0xfd,0xfe};
void delay(unsigned char b) //50us 延时函数,延时50US
{
unsigned char a;
for(;b>0;b--)
for(a=22;a>0;a--);
}
void display(uchar a,uchar b,uchar c,uchard)//定义显示函数,什么时候位选为1,什么时候段选为1.
{
P0=duan[a]|0x80;
P2=wei[0];
delay(5);
P2=0xff;
P0=duan[b];
P2=wei[1];
delay(5);
P2=0xff;
P0=duan[c];
P2=wei[2];
delay(5);
P2=0xff;
P0=duan[d];
P2=wei[3];
delay(5);
P2=0xff;
}
uint read(uchar port)//定义读取端口条件,
{
uchar i,al=0,ah=0;
unsignedlong ad;
clock=0;
_cs=0;
port<<=4;
for(i=0;i<4;i++)
{
d_in=port&0x80;
clock=1;
clock=0;
port<<=1;
}
d_in=0;
for(i=0;i<8;i++)
{
clock=1;
clock=0;
}
_cs=1;
delay(5);
_cs=0;
for(i=0;i<4;i++)
{
clock=1;
ah<<=1;
if(d_out)ah|=0x01;
clock=0;
}
for(i=0;i<8;i++)
{
clock=1;
al<<=1;
if(d_out)al|=0x01;
clock=0;
}
_cs=1;
ad=(uint)ah;
ad<<=8;
ad|=al;
return(ad);
}
void main() //主函数,此函数把前面的函数在这里循环调用,从而实现数码管显示
{
ucharj;
sum=0;sum1=0;
sum_final=0;
sum_final1=0;
while(1)
{
for(j=0;j<128;j++)
{
sum1+=read(1);
display(a1,b1,c1,d1);
}
sum=sum1/128;
sum1=0;
sum_final1=(sum/4095)*5;
sum_final=sum_final1*1000;
a1=(int)sum_final/1000;
b1=(int)sum_final%1000/100;
c1=(int)sum_final%1000%100/10;
d1=(int)sum_final%10;
display(a1,b1,c1,d1);
}
#include<intrins.h>
#define uchar unsigned char//方便定义函数无符号型应该用UCHAR表示为方便直接用CHAR表示了。
#define uint unsigned int//整形同上解释
sbit clock=P1^0; //定义CLOCK接到单片机P0_1口,请注意这里用P0^1,就是因为包含了REG51.H。如果不包含就不能这么使用。
sbit d_in=P1^1;
sbit d_out=P1^2;
sbit _cs=P1^3;
//以下就是定义几个变量
uchar a1,b1,c1,d1;
float sum,sum1;
double sum_final1;
double sum_final;
//数码管定义段选和位选
uchar duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar wei[]={0xf7,0xfb,0xfd,0xfe};
void delay(unsigned char b) //50us 延时函数,延时50US
{
unsigned char a;
for(;b>0;b--)
for(a=22;a>0;a--);
}
void display(uchar a,uchar b,uchar c,uchard)//定义显示函数,什么时候位选为1,什么时候段选为1.
{
P0=duan[a]|0x80;
P2=wei[0];
delay(5);
P2=0xff;
P0=duan[b];
P2=wei[1];
delay(5);
P2=0xff;
P0=duan[c];
P2=wei[2];
delay(5);
P2=0xff;
P0=duan[d];
P2=wei[3];
delay(5);
P2=0xff;
}
uint read(uchar port)//定义读取端口条件,
{
uchar i,al=0,ah=0;
unsignedlong ad;
clock=0;
_cs=0;
port<<=4;
for(i=0;i<4;i++)
{
d_in=port&0x80;
clock=1;
clock=0;
port<<=1;
}
d_in=0;
for(i=0;i<8;i++)
{
clock=1;
clock=0;
}
_cs=1;
delay(5);
_cs=0;
for(i=0;i<4;i++)
{
clock=1;
ah<<=1;
if(d_out)ah|=0x01;
clock=0;
}
for(i=0;i<8;i++)
{
clock=1;
al<<=1;
if(d_out)al|=0x01;
clock=0;
}
_cs=1;
ad=(uint)ah;
ad<<=8;
ad|=al;
return(ad);
}
void main() //主函数,此函数把前面的函数在这里循环调用,从而实现数码管显示
{
ucharj;
sum=0;sum1=0;
sum_final=0;
sum_final1=0;
while(1)
{
for(j=0;j<128;j++)
{
sum1+=read(1);
display(a1,b1,c1,d1);
}
sum=sum1/128;
sum1=0;
sum_final1=(sum/4095)*5;
sum_final=sum_final1*1000;
a1=(int)sum_final/1000;
b1=(int)sum_final%1000/100;
c1=(int)sum_final%1000%100/10;
d1=(int)sum_final%10;
display(a1,b1,c1,d1);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询