郭天祥十天学会单片机中的源程序.docx
《郭天祥十天学会单片机中的源程序.docx》由会员分享,可在线阅读,更多相关《郭天祥十天学会单片机中的源程序.docx(55页珍藏版)》请在冰豆网上搜索。
郭天祥十天学会单片机中的源程序
Lesson2-1
#include
sbitp1_1=P1^0
unsignedinta
voidmain()
{
while
(1)
{
a=51000;
p1_1=0;
while(a--);
a=51000;
while(a--);
p1_1=1;
a=51000;
while(a--);
a=51000;
while(a--);
}
}
Lesson2-2
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitD1=P1^0;
//voiddelay();
voiddelay(uint);
voidmain()
{
while
(1)
{
D1=0;
delay(100);
D1=1;
delay(100);
}
}
/*
voiddelay()
{
uintx,y;
for(x=100;x>0;x--)
for(y=600;y>0;y--);
}*/
voiddelay(uintz)
{
uintx;
uchary;
for(x=z;x>0;x--)
for(y=20;y>0;y--);
}
Lesson2-3
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
uchartemp,num,num1;
sbitbeep=P2^3;
voiddelay(uint);
voidmain()
{
temp=0xfe;
while
(1)
{
for(num1=0;num1<3;num1++)
{
for(num=0;num<8;num++)
{
P1=temp;
beep=0;
delay(100);
temp=_crol_(temp,1);
P1=0xff;
beep=1;
delay(100);
}
}
for(num1=0;num1<3;num1++)
{
for(num=0;num<8;num++)
{
P1=temp;
beep=0;
delay(100);
temp=_cror_(temp,1);
P1=0xff;
beep=1;
delay(100);
}
}
while
(1);
}
}
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
Lesson3-1
#include
sbitdula=P2^6;
sbitwela=P2^7;
voidmain()
{
wela=1;
P0=0xc0;
wela=0;
dula=1;
P0=0x06;
dula=0;
while
(1);
}
Lesson3-2
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitdula=P2^6;
sbitwela=P2^7;
ucharnum;
ucharcodetable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
voiddelay(uintz);
voidmain()
{
wela=1;//11101010
P0=0xea;
wela=0;
while
(1)
{
for(num=0;num<16;num++)
{
dula=1;
P0=table[num];
dula=0;
delay(1000);
}
}
}
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
Lesson3-3
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitdula=P2^6;
sbitwela=P2^7;
sbitd1=P1^0;
ucharnum;
ucharcodetable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
voiddelay(uintz);
voidmain()
{
EA=1;//开总中断
EX0=1;//开外部中断0
//IT0=1;
TCON=0x01;
wela=1;//11101010
P0=0xea;
wela=0;
while
(1)
{
for(num=0;num<16;num++)
{
d1=1;
dula=1;
P0=table[num];
dula=0;
delay(1000);
}
}
}
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voidexter0()interrupt0
{
d1=0;
}
Lesson3-4
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitdula=P2^6;
sbitwela=P2^7;
sbitd1=P1^0;
ucharnum,num1;tt;
ucharcodetable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
voiddelay(uintz);
voidmain()
{
num=0;
tt=0;
TMOD=0x11;//设置定时器0为工作方式1
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TH1=(65536-20000)/256;
TL1=(65536-20000)%256;
//EA=1;//开总中断
ET0=1;//开定时器0中断
ET1=1;
TR0=1;//启动定时器0
TR1=1;
wela=1;//11101010
P0=0xea;
wela=0;
dula=1;
P0=0x3f;
dula=0;
while
(1)
{
if(num1==25)
{
num1=0;
P1=~P1;
}
if(tt==100)
{
tt=0;
num++;
if(num==16)
num=0;
dula=1;
P0=table[num];
dula=0;
}
}
}
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voidtime0()interrupt1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
tt++;
}
voidtime1()interrupt3
{
TH1=(65536-20000)/256;
TL1=(65536-20000)%256;
num1++;
}
Lesson4-1
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
uchartemp,aa,num;
sbitdula=P2^6;
sbitwela=P2^7;
ucharcodetable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
voiddelay(uintz);
voidmain()
{
num=0;
aa=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
temp=0xfe;
P1=temp;
dula=1;
P0=table[0];
dula=0;
wela=1;
P0=0xc0;
wela=0;
while
(1)
{
delay(1000);
temp=_crol_(temp,1);
P1=temp;
/*if(aa==40)
{
aa=0;
num++;
if(num==16)
num=0;
dula=1;
P0=table[num];
dula=0;
}*/
}
}
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voidtimer0()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==40)
{
aa=0;
num++;
if(num==16)
num=0;
dula=1;
P0=table[num];
dula=0;
}
}
Lesson4-2
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
uchartemp,aa,numdu,numwe;
sbitdula=P2^6;
sbitwela=P2^7;
ucharcodetable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7