单片机一些程序.docx
《单片机一些程序.docx》由会员分享,可在线阅读,更多相关《单片机一些程序.docx(18页珍藏版)》请在冰点文库上搜索。
单片机一些程序
流水灯从左闪到右又从右闪到左的程序
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
voiddelay(uintz)
{uintx,y;
for(x=0;xfor(y=0;y<114;y++);
}
voidmain()
{uchari,templ,j,tempr;
while
(1)
{
templ=0xfe,tempr=0x7f;
for(i=0;i<7;i++)
{P1=templ;
templ=_crol_(templ,1);
delay(1000);
}
for(j=0;j<7;j++)
{P1=tempr;
tempr=_cror_(tempr,1);
delay(1000);
}
}
}
左右循环两个两个的闪
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
voiddelay(uintz)
{uintx,y;
for(x=0;xfor(y=0;y<114;y++);
}
voidmain()
{uchari,templ,tempr,j;
while
(1)
{
templ=0xfc,tempr=0x3f;
for(i=0;i<7;i++)
{P1=templ;
templ=_crol_(templ,1);
delay(1000);
}
for(j=0;j<5;j++)
{P1=tempr;
tempr=_cror_(tempr,1);
delay(1000);
}
}
}
定时计数器程序
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
voidmain()
{ucharcount,a;
a=0x00;
TMOD=0x10;
TH1=0x4b;
TL1=0xff;
TR1=1;
while
(1)
{
if(TF1==1)
{
TF1=0;
TH1=0x4b;
TL1=0xff;
count++;
}
if(count==20)
{
P1=a;
a=~a;
count=0;
}
}
}
钟表程序
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitwei=P2^7;
sbitduan=P2^6;
ucharshi1,shi2,fen1,fen2,miao1,miao2,count;//外部变量的定义
voiddelay(uintz)//延时函数
{uintx,y;
for(x=0;xfor(y=0;y<114;y++);
}
unsignedcharcodeled[]={
0x3F,//"0"
0x06,//"1"
0x5B,//"2"
0x4F,//"3"
0x66,//"4"
0x6D,//"5"
0x7D,//"6"
0x07,//"7"
0x7F,//"8"
0x6F,//"9"
0x77,//"A"
0x7C,//"B"
0x39,//"C"
0x5E,//"D"
0x79,//"E"
0x71,//"F"
0x76,//"H"
0x38,//"L"
0x37,//"n"
0x3E,//"u"
0x73,//"P"
0x5C,//"o"
0x40,//"-"
0x00,//熄灭
0x00//自定义
};
voidjiange()//钟表中间间隔函数
{wei=1;
P0=0xdf;
wei=0;
duan=1;
P0=led[22];
duan=0;
delay(5);
wei=1;
P0=0xfb;
wei=0;
duan=1;
P0=led[22];
duan=0;
delay(5);
}
voidshi()//钟表的时函数
{
wei=1;
P0=0xfd;
wei=0;
duan=1;
P0=led[shi1];
duan=0;
delay(5);
wei=1;
P0=0xfe;
wei=0;
duan=1;
P0=led[shi2];
duan=0;
delay(5);
}
voidfen()//钟表的分函数
{
wei=1;
P0=0xef;
wei=0;
duan=1;
P0=led[fen1];
duan=0;
delay(5);
wei=1;
P0=0xf7;
wei=0;
duan=1;
P0=led[fen2];
duan=0;
delay(5);
}
voidmiao()//钟表的秒函数
{wei=1;
P0=0x7f;
wei=0;
duan=1;
P0=led[miao1];
duan=0;
delay(5);
wei=1;
P0=0xbf;
wei=0;
duan=1;
P0=led[miao2];
duan=0;
delay(5);
}
voidmain()
{
TMOD=0x10;//定时器1的使用及初始化
TH1=0x4c;
TL1=0x00;//初始化50毫秒
TR1=1;
while
(1)
{jiange();//间隔函数的调用
shi();//时函数的调用
fen();//分函数的调用
miao();//秒函数的调用
if(TF1==1)
{TF1=0;
TH1=0x4c;
TL1=0x00;
count++;
}
if(count==20)
{
wei=1;
P0=0x7f;
wei=0;
duan=1;
P0=led[miao1];
duan=0;
delay(5);
miao1++;
if(miao1==10)
{
miao1=0;
miao2++;
if(miao2==6)
{
miao2=0;
fen1++;
if(fen1==10)
{fen1=0;
fen2++;
if(fen2==6)
{fen2=0;
shi1++;
if(shi1==10)
{shi1=0;
shi2++;
if(shi2==2)
{shi2=0;
}
}
}
}
}
count=0;
}
}
}
矩阵键盘扫描函数
voidkeyscan()
{
inttemp,key;
///////////////第一行扫描///////////////////
P3=0xfe;//11111110让P3.0口输出低
temp=P3;
temp=temp&0xf0;//11110000位与操作屏蔽后四位
if(temp!
=0xf0)
{
delay(10);//消抖
temp=P3;
temp=temp&0xf0;
if(temp!
=0xf0)//检测是否键被按下
{
temp=P3;
switch(temp)
{
case0xee:
key=0;break;//11101110S1被按下
case0xde:
key=1;break;//11011110S2被按下
case0xbe:
key=2;break;//10111110S3被按下
case0x7e:
key=3;break;//01111110S4被按下
}
display(key);
}
}
///////////////第二行扫描///////////////////
P3=0xfd;//11111101让P3.1口输出低电平
temp=P3;
temp=temp&0xf0;//11110000位与操作屏蔽后四位
if(temp!
=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!
=0xf0)
{
temp=P3;
switch(temp)
{
case0xed:
key=4;break;//11101110S5被按下
case0xdd:
key=5;break;//11011110S6被按下
case0xbd:
key=6;break;//10111110S7被按下
case0x7d:
key=7;break;//01111110S8被按下
}
display(key);
}
}
///////////////第三行扫描///////////////////
P3=0xfb;//11111011让P3.2口输出低电平
temp=P3;
temp=temp&0xf0;//11110000位与操作屏蔽后四位
if(temp!
=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!
=0xf0)
{
temp=P3;
switch(temp)
{
case0xeb:
key=8;break;//11101011S9被按下
case0xdb:
key=9;break;//11011011S10被按下
case0xbb:
key=10;break;//10111011S11被按下
case0x7b:
key=11;break;//01111011S12被按下
}
display(key);
}
}
///////////////第四行扫描///////////////////
P3=0xf7;//11110111让P3.3口输出低电平
temp=P3;
temp=temp&0xf0;//11110000位与操作屏蔽后四位
if(temp!
=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!
=0xf0)
{
temp=P3;
switch(temp)
{
case0xe7:
key=12;break;//11100111S13被按下
case0xd7:
key=13;break;//11010111S14被按下
case0xb7:
key=14;break;//10110111S15被按下
case0x77:
key=15;break;//01110111S16被按下
}
display(key);
}
}
}
共阳极数码管断码数组
unsignedcharcodeled[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x89,0xC7,0xC8,0xC1,0x8C,0xA3,0xBF,0xFF,0xFF};
共阴极数码管断码数组
unsignedcharcodeled[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x76,0x38,0x37,0x3E,0x73,0x5C,0x40,0x00,0x00};
数码管位码数组
unsignedcharcodewei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
中断程序
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
voiddelay(uintz)
{uintx,y;
for(x=0;xfor(y=0;y<114;y++);
}
voidint0()interrupt0//外部中断0执行的程序
{uchari,templ,j,tempr;
while
(1)
{
templ=0xfe,tempr=0x7f;
for(i=0;i<8;i++)
{P1=templ;
templ=_crol_(templ,1);
delay(1000);
}
for(j=0;j<7;j++)
{P1=tempr;
tempr=_cror_(tempr,1);
delay(1000);
}
}
}
voidmain()
{
EA=1;//打开总中断
EX0=1;//打开外部中断0
IT0=1;//外部中断负跳变工作方式
while
(1);
}
花样流水灯程序
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodePattern_P1[]=
{
0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,
0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,
0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff
};
ucharcodePattern_P2[]=
{
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,
0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,
0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,
0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,
0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff
};
voidDelay(uintx)
{
uchari;
while(x--)
{
for(i=0;i<120;i++);
}
}
voidmain()
{uchari;
while
(1)
{
for(i=0;i<136;i++)
{
P1=Pattern_P1[i];
P2=Pattern_P2[i];
Delay(100);
}
}
}