单片机实验程序 2.docx
《单片机实验程序 2.docx》由会员分享,可在线阅读,更多相关《单片机实验程序 2.docx(23页珍藏版)》请在冰豆网上搜索。
单片机实验程序2
实验一流水灯实验
最终实现:
按任意键反向,只是必须等到一轮结束以后。
程序如下:
#include
#defineucharunsignedchar
sbitH1=P3^6;
sbitH2=P3^7;
sbitL1=P0^5;
sbitL2=P0^6;
sbitL3=P0^7;
tab1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff};
tab2[]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3b,0x37,0x2f,0x1f};
tab3[]={0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
tab4[]={0x1f,0x2f,0x37,0x3b,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
voidDelay1s()//@11.0592MHz
{
unsignedchari,j,k;
for(i=1;i<10;i++)
for(j=1;j<200;j++)
for(k=1;k<250;k++)
}
voidDelay10ms()//@11.0592MHz
{
unsignedchari,j;
for(i=1;i<108;i++)
for(j=1;j<145;j++)
}
voidflicker()
{
uchard;
while(d<=2)
{
P2=0x00;
P3=0xc3;
Delay1s();
P2=0xff;
P3=0xff;
Delay1s();
d++;
}
}
voidmain()
{
ucharb,y,n;
flicker();
while
(1)
{
n=b;
y=(n%2);
if(y==1)
{
ucharx;
for(x=0;x<12;x++)
{
P2=tab3[x];
P3=tab4[x];
Delay1s();
H1=0;
H2=0;
L1=1;
L2=1;
L3=1;
if(!
L1||!
L2||!
L3)
{
Delay10ms();
if(!
L1||!
L2||!
L3)
b+=1;
while(!
L1||!
L2||!
L3);
}
}
}
if(y==0)
{
ucharx;
for(x=0;x<12;x++)
{
P2=tab1[x];
P3=tab2[x];
Delay1s();
H1=0;
H2=0;
L1=1;
L2=1;
L3=1;
if(!
L1||!
L2||!
L3)
{
Delay10ms();
if(!
L1||!
L2||!
L3)
b+=1;
while(!
L1||!
L2||!
L3);
}
}
}
}
}
实验二定时器时钟实验
最终实现:
上电后设置定时时间,按2号键时间+1,按3号键时间+100;按1号键开始工作;时间到偶小灯闪烁3下,回到初始状态。
程序如下:
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitH1=P3^6;
sbitH2=P3^7;
sbitL1=P0^5;
sbitL2=P0^6;
sbitL3=P0^7;
sfrP1M1=0x91;
sfrP1M0=0x92;
tab1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
tab2[]={0xf7,0xfb,0xfd,0xfe};
tab3[4]={0};
uinta=0;
uintb=0;
voidDelay100ms()//@11.0592MHz
{
unsignedchari,j,k;
for(i=1;i<5;i++)
for(j=1;j<52;j++)
for(k=1;k<195;k++)
}
voidDelay5ms()//@11.0592MHz
{
unsignedchari,j;
for(i=1;i<54;i++)
for(j=1;j<199;j++)
}
voidflicker()
{
uchard;
while(d<=2)
{
P2=0x00;
P3=0xc3;
Delay100ms();
P2=0xff;
P3=0xff;
Delay100ms();
d++;
}
}
voidshow()
{
charj;
switch(j)
{
case0:
P0=tab2[j];j++;P1=tab1[tab3[0]];break;
case1:
P0=tab2[j];j++;P1=tab1[tab3[1]];break;
case2:
P0=tab2[j];j++;P1=tab1[tab3[2]];break;
case3:
P0=tab2[j];j=0;P1=tab1[tab3[3]];break;
default:
break;
}
}
voidinterrupt_timer1()interrupt3
{
TH1=0xfc;
TL1=0x67;
a++;
if(a==1000)
{
b++;
a=0;
tab3[0]=b%10;
tab3[1]=b/10%10;
tab3[2]=b/100%10;
tab3[3]=b/1000%10;
}
show();
}
voidmain()
{
ucharm,k;
uintn=1;
P1M1=0x00;
P1M0=0xff;
TMOD=0x10;
TH1=0xfc;
TL1=0x67;
ET1=1;
EA=1;
while
(1)
{
if(!
TR1)
{
k=0;
for(k=0;k<50;k++)
{
H1=0;
H2=0;
L1=1;
L2=1;
L3=1;
if(!
L1)
{
Delay5ms();
if(!
L1)
{
TR1=1;
break;
}
while(!
L1);
}
if(!
L2)
{
Delay5ms();
if(!
L2)
n+=1;
while(!
L2);
}
if(!
L3)
{
Delay5ms();
if(!
L3)
n+=100;
while(!
L3);
}
tab3[0]=n%10;
tab3[1]=n/10%10;
tab3[2]=n/100%10;
tab3[3]=n/1000%10;
for(m=0;m<4;m++)
{
P0=tab2[m];P1=tab1[tab3[m]];
Delay5ms();
}
if(m==4)
m=0;
}
}
if(b==n)
{
TR1=0;
b=0;
n=1;
flicker();
}
}
}
实验三双机通信实验
最终实现:
两机连接后按任意键可在两外的机子上显示该键编号。
程序如下:
#include
#defineucharunsignedchar
#defineuintunsignedint
sfrP1M1=0x91;
sfrP1M0=0x92;
sbitH1=P3^6;
sbitH2=P3^7;
sbitL1=P0^5;
sbitL2=P0^6;
sbitL3=P0^7;
sbitscran=P0^3;
tab1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchara,b,n;
voidDelay5ms()//@11.0592MHz
{
unsignedchari,j;
for(i=1;i<54;i++)
for(j=1;j<199;j++)
}
voidmain()
{
SCON=0xd0;
TMOD=0x20;
PCON=0x00;
TH1=0xf4;
TL1=0xf4;
TR1=1;
scran=0;
while
(1)
{
n=a;
H1=0;
H2=1;
L1=1;
L2=1;
L3=1;
if(!
L1)
{
Delay5ms();
if(!
L1)
a=1;
while(!
L1);
}
if(!
L2)
{
Delay5ms();
if(!
L2)
a=2;
while(!
L2);
}
if(!
L3)
{
Delay5ms();
if(!
L3)
a=3;
while(!
L3);
}
H1=1;
H2=0;
L1=1;
L2=1;
L3=1;
if(!
L1)
{
Delay5ms();
if(!
L1)
a=4;
while(!
L1);
}
if(!
L2)
{
Delay5ms();
if(!
L2)
a=5;
while(!
L2);
}
if(!
L3)
{
Delay5ms();
if(!
L3)
a=6;
while(!
L3);
}
if(a!
=n)
{
SBUF=a;
while(!
TI);
TI=0;
}
if(RI)
{
b=SBUF;
RI=0;
}
P1=tab1[b];
}
}
实验四交通灯实验
最终实现:
上电之后进行初始设置,2号键增加红绿灯时间,3号键增加黄灯时间,初始时间都为零;1号键开始工作;按3、4、5任意键急停,按3、4、5任意键继续(从第一步开始。
程序如下:
#include
#defineucharunsignedchar
#defineuintunsignedint
voidled_show();
ucharm,n;
uinta=0;
uintb=0;
uchark,c,d,f,g;
sfrP1M1=0x91;
sfrP1M0=0x92;
sbitko1=P3^6;
sbitko2=P3^7;
sbitki1=P0^5;
sbitki2=P0^6;
sbitki3=P0^7;
sbitD1=P2^0;
sbitD2=P2^1;
sbitD3=P2^2;
sbitD4=P2^3;
sbitD5=P2^4;
sbitD6=P2^5;
sbitD7=P2^6;
sbitD8=P2^7;
sbitD9=P3^2;
sbitD10=P3^3;
sbitD11=P3^4;
sbitD12=P3^5;
tab1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
tab2[]={0xf7,0xfb,0xfd,0xfe};
tab3[4]={0};
voidshow()
{
charj;
switch(j)
{
case0:
P0=tab2[j];j++;P1=tab1[tab3[0]];break;
case1:
P0=tab2[j];j++;P1=tab1[tab3[1]];break;
case2:
P0=tab2[j];j++;P1=tab1[tab3[2]];break;
case3:
P0=tab2[j];j=0;P1=tab1[tab3[3]];break;
default:
break;
}
}
voidDelay5ms()//@11.0592MHz
{
unsignedchari,j;
for(i=1;i<54;i++)
for(j=1;j<199;j++)
}
voidinterrupt_timer1()interrupt3
{
TH1=0xfc;
TL1=0x67;
a++;
if(a==1000)
{
b++;
a=0;
}
if(b=0)
{
tab3[0]=(m-b)%10;
tab3[1]=(m-b)/10%10;
tab3[2]=(m-b)/100%10;
tab3[3]=(m-b)/1000%10;
}
if(b=m)
{
tab3[0]=(d-b)%10;
tab3[1]=(d-b)/10%10;
tab3[2]=(d-b)/100%10;
tab3[3]=(d-b)/1000%10;
}
if(b=d)
{
tab3[0]=(f-b)%10;
tab3[1]=(f-b)/10%10;
tab3[2]=(f-b)/100%10;
tab3[3]=(f-b)/1000%10;
}
if(b=f)
{
tab3[0]=(g-b)%10;
tab3[1]=(g-b)/10%10;
tab3[2]=(g-b)/100%10;
tab3[3]=(g-b)/1000%10;
}
show();
}
voiddet()
{
H1=1;
H2=0;
L1=1;
L2=1;
L3=1;
if(!
L1||!
L2||!
L3)
{
Delay5ms();
if(!
L1||!
L2||!
L3)
{
P2=0xdb;
D10=1;
D11=1;
D9=0;
D12=0;
while(!
L1||!
L2||!
L3);
while(L1)
{if(!
L1||!
L2||!
L3){while(!
L1||!
L2||!
L3);
b=0;break;
}
}
}
}
}
voidled_show()
{
while
(1)
{
D1=1;
D7=1;
D4=1;
D10=1;
D3=1;
D9=1;
D6=1;
D12=1;
D2=1;
D8=1;
D5=1;
D11=1;
d=(m+n);
f=(2*m+n);
g=(2*m+2*n);
if(b=0)//东西通
{
D1=0;
D7=0;
D4=1;
D10=1;
D3=1;
D9=1;
D6=0;
D12=0;
D2=1;
D8=1;
D5=1;
D11=1;
det();
}
if(b=m)//黄
{
D1=1;
D7=1;
D4=1;
D10=1;
D3=1;
D9=1;
D6=1;
D12=1;
D2=0;
D8=0;
D5=0;
D11=0;
det();
}
if(b=d)//南北通
{
D1=1;
D7=1;
D4=0;
D10=0;
D3=0;
D9=0;
D6=1;
D12=1;
D2=1;
D8=1;
D5=1;
D11=1;
det();
}
if(b=f)//黄
{
D1=1;
D7=1;
D4=1;
D10=1;
D3=1;
D9=1;
D6=1;
D12=1;
D2=0;
D8=0;
D5=0;
D11=0;
det();
}
if(b==g)
b=0;
}
}
voidmain()
{
P1M1=0x00;
P1M0=0xff;
TMOD=0x10;
TH1=0xfc;
TL1=0x67;
ET1=1;
EA=1;
TR1=0;
while
(1)
{
if(!
TR1)
{
k=0;
for(k=0;k<50;k++)
{
ko1=0;
ko2=1;
ki1=1;
ki2=1;
ki3=1;
if(!
ki1)
{
Delay5ms();
if(!
ki1)
{
TR1=1;
led_show();
}
while(!
L1);
}
if(!
L2)
{
Delay5ms();
if(!
L2)
m+=1;
while(!
L2);
}
if(!
L3)
{
Delay5ms();
if(!
L3)
n+=1;
while(!
L3);
}
tab3[0]=m%10;
tab3[1]=m/10%10;
tab3[2]=n%10;
tab3[3]=n/10%10;
for(c=0;c<4;c++)
{
P0=tab2[c];
P1=tab1[tab3[c]];
Delay5ms();
}
if(m==4)
c=0;
}
}
}
}
第一个
#include
#defineucharunsignedchar
sbitH1=P3^6;
sbitH2=P3^7;
sbitL1=P0^5;
sbitL2=P0^6;
sbitL3=P0^7;
tab1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff};
tab2[]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3b,0x37,0x2f,0x1f};
tab3[]={0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
tab4[]={0x1f,0x2f,0x37,0x3b,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
voidDelay1s()//@11.0592MHz
{
unsignedchari,j,k;
for(i=1;i<10;i++)
for(j=1;j<200;j++)
for(k=1;k<250;k++);
}
voidDelay10ms()//@11.0592MHz
{
unsignedchari,j;
for(i=1;i<108;i++)
for(j=1;j<145;j++);
}
voidflicker()
{
uchard;
while(d<=2)
{
P2=0x00;
P3=0xc3;
Delay1s();
P2=0xff;
P3=0xff;
Delay1s();
d++;
}
}
voidmain()
{
ucharb,y,n;
flicker();
while
(1)
{
n=b;
y=(n%2);
if(y==1)
{
ucharx;
for(x=0;x<12;x++)
{
P2=tab3[x];
P3=tab4[x];
Delay1s();
H1=0;
H2=0;
L1=1;
L2=1;
L3=1;
if(!
L1||!
L2||!
L3)
{
Delay10ms();
if(!
L1||!
L2||!
L3)
b+=1;
while(!
L1||!
L2||!
L3);
}
}
}
if(y==0)
{
ucharx;
for(x=0;x<12;x++)
{
P2=tab1[x];
P3=tab2[x];
Delay1s();
H1=0;
H2=0;
L1=1;
L2=1;
L3=1;
if(!
L1||!
L2||!
L3)
{
Delay10ms();
if(!
L1||!
L2||!
L3)
b+=1;
while(!
L1||!
L2||!
L3);
}
}
}
}
}