单片机期末复习程序代码.docx
《单片机期末复习程序代码.docx》由会员分享,可在线阅读,更多相关《单片机期末复习程序代码.docx(30页珍藏版)》请在冰点文库上搜索。
单片机期末复习程序代码
1.流水灯电路图
//定时计算器控制
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
uintcount;
uchartemp;
uchartemp0;
main()
{
count=0;
temp=0xfa;
P1=temp;
temp0=0xfb;
P0=temp0;
//设置TMOD寄存器
TMOD=0x01;//定时器方式1
//计算定时器T0的计数初值
TH0=(65536-50000)/256;//取高8位
TL0=(65536-50000)%256;//取低8位
//设置IE寄存器
EA=1;//总中断允许
ET0=1;//定时器T0中断允许
//启动和停止定时器T0
TR0=1;//启动定时器T0
while
(1)
{
if(count==20)//65535
{
count=0;
temp=_crol_(temp,1);
P1=temp;
temp0=_cror_(temp0,1);
P0=temp0;
}
}
}
//计时器函数
voidtimer0()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}
//延时函数
#include
#defineuintunsignedint
#defineucharunsignedchar
voiddelay(uintz){
uinti,j;
for(i=z;i>0;i--)
for(j=123;j>0;j--){}
}
main(){
uinti;
ucharled_p2[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
ucharled_p3[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00};
while
(1){
for(i=0;i<32;i++){
P1=led_p2[i];
P2=led_p3[i];
delay(500);
}
}
}
2矩阵键盘
//矩阵键盘代码
#include
#defineuintunsignedint
#defineucharunsignedchar
ucharled_dm[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
ucharnum,temp;
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=123;y>0;y--);
}
main()
{
P2=0xff;
while
(1)
{
//扫描第一行代码
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
temp=P3;
switch(temp)
{
case0xee:
num=0;break;
case0xde:
num=1;break;
case0xbe:
num=2;break;
case0x7e:
num=3;break;
}
while(temp!
=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
//扫描第二行代码
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
temp=P3;
switch(temp)
{
case0xed:
num=4;break;
case0xdd:
num=5;break;
case0xbd:
num=6;break;
case0x7d:
num=7;break;
}
while(temp!
=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
//第三行代码
P3=0xfb;
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
temp=P3;
switch(temp)
{
case0xeb:
num=8;break;
case0xdb:
num=9;break;
case0xbb:
num=10;break;
case0x7b:
num=11;break;
}
while(temp!
=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
//扫描第四行代码
P3=0xf7;
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!
=0xf0)
{
temp=P3;
switch(temp)
{
case0xe7:
num=12;break;
case0xd7:
num=13;break;
case0xb7:
num=14;break;
case0x77:
num=15;break;
}
while(temp!
=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
//读取数码管字开码
P0=led_dm[num];
}
}
3数码管
//单个数码管
//延时函数控制的数码管
#include
#defineuintunsignedint
#defineucharunsignedchar
//数码管为共阴极接法
ucharcodeled[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76};
voiddelay(uintz)
{
uintx,y;
for(x=0;xfor(y=0;y<=123;y++);
}
voidmain()
{
uchari;
P0=0x00;
while
(1)
{
for(i=0;i<=16;i++)
{
P0=led[i];
delay(1000);
}
}
}
//定时计数器控制的数码管
#include
#defineucharunsignedchar
#defineuintunsignedint
uintcount;
ucharcodeled[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
main()
{
uinti;
TMOD=0x01;//选用方式1,为16位定时/计数器
TH0=(65536-50000)/256;//设置高8位的初始化值
TL0=(65536-50000)%256;//设置低8位的初始化值
EA=1;//IE总中断开关
ET0=1;//IE定时计算器T0的溢出中断允许位
TR0=1;//TCON计数运行控制位
while
(1)
{
for(i=0;i<16;i++)
{
if(count==20)
{
count=0;
P0=led[i];
}
}
}
}
voidtimer0()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}
//多个数码管
//代码如下
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitdula=P2^0;
sbitwe=P2^1;
//延时函数
voiddelay(uintz)
{
uinti,j;
for(i=z;i>0;i--)
for(j=123;j>0;j--);
}
ucharcodeled[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
main()
{
uinti;
we=1;
P0=0x00;
we=0;
while
(1)
{
for(i=0;i<16;i++)
{
dula=1;
P0=led[i];
dula=0;
delay(1000);
}
}
}
4数码管
//液晶屏代码控制
//编程实现在1602液晶屏上显示一个指定字符
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitlcd_en=P2^2;
sbitlcd_rs=P2^0;
sbitlcd_rw=P2^1;//为1为数据寄存器读为0为写模式
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=123;y>0;y--);
}
voidwrite_com(ucharcom)
{
lcd_en=0;//为0是读使能信号为1为写使能信号
lcd_rs=0;//寄存器选择——1为数据寄存器0——为命令/状态寄存器
P0=com;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
voidwrite_data(uchardat)
{
lcd_en=0;//为0是读使能信号为1为写使能信号
lcd_rs=1;//寄存器选择——1为数据寄存器0——为命令/状态寄存器
P0=dat;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}
voidinit()
{
lcd_rw=0;
write_com(0x38);//显示模式
write_com(0x0f);//设置光标显示方式
write_com(0x06);//设置数据指针移动方式
write_com(0x01);//清除液晶屏
//write_com(0x80);//设备数据指针位置
}
main()
{
uinti;
ucharmessage[]="Mynameis!
";
ucharmessage1[]="Ilovedan!
";
init();
write_com(0x80+0x00);
for(i=0;i<12;i++){
write_data(message[i]);
}
write_com(0x80+0x40);
for(i=0;i<12;i++){
write_data(message1[i]);
}
while
(1);
}
5用液晶屏显示时间
//时间显示代码实现
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitlcdrs=P2^0;
sbitlcdrw=P2^1;
sbitlcden=P2^2;
sbits1=P2^3;
sbits2=P2^4;
sbits3=P2^5;
ucharcodetable1[]="2011-11-25M";
ucharcodetable2[]="23:
59:
45";
ucharnum,count,shi,fen,miao,key_count;
voiddelay_ms(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=123;y>0;y--);
}
voidwrite_com(ucharcom)
{
lcdrs=0;//选中数据积存器
lcden=0;//使能信号为不允许
P0=com;
delay_ms(5);
lcden=1;//使能信号为允许
delay_ms(5);
lcden=0;
}
voidwrite_date(uchardate)
{
lcdrs=1;
lcden=0;
P0=date;
delay_ms(5);
lcden=1;
delay_ms(5);
lcden=0;
}
voidwrite_sfm(ucharadd,ucharsfm)
{
ucharsi,ge;
si=sfm/10;
ge=sfm%10;
write_com(0x80+0x40+add);
write_date(0x30+si);//加1
write_date(0x30+ge);//加1
}
voidkeyscan()
{
if(s1==0)
{
delay_ms(5);
if(s1==0)
{
key_count++;
while(!
s1);
if(key_count==1)
{
TR0=0;//停止计数器
write_com(0x80+0x40+10);//显示开关控制那10位
write_com(0x0f);//控制光标闪锁
}
}
if(key_count==2)
{
write_com(0x80+0x40+7);
}
if(key_count==3)
{
write_com(0x80+0x40+4);
}
if(key_count==4)
{
key_count=0;
write_com(0x0c);//控制光标停止闪所
TR0=1;//开启计数器
}
}
//调整时间递增
if(key_count!
=0)
{
if(s2==0)
{
delay_ms(5);
if(s2==0)
{
while(!
s2);
if(key_count==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(key_count==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(key_count==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
//调整时间递减
if(s3==0)
{
delay_ms(5);
if(s3==0)
{
while(!
s3);
if(key_count==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(key_count==2)
{
}
if(key_count==3)
{
}
}
}
}
}
voidinit()
{
lcden=0;
lcdrw=0;
write_com(0x38);//显示模式
write_com(0x0c);//显示2行首地址
write_com(0x06);//光标自动右移一格
write_com(0x01);//清屏指令,将数据寄存器填入”空白“
write_com(0x80);//显示第1行首地址
for(num=0;num<14;num++)
{
write_date(table1[num]);
delay_ms(20);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table2[num]);
delay_ms(20);
}
TMOD=0x01;//设置定时计数器为方式0为13位定时/计数器
TH0=(65536-50000)/256;//向TH0写入初值高8位
TL0=(65536-50000)%256;//向TL0写入初值的底8位
EA=1;
ET0=1;//定时器T0中断允许
TR0=1;//启动定时器
shi=23;
fen=59;
miao=45;
}
voidmain()
{
init();
while
(1)
{
keyscan();
}
}
voidtimer()interrupt1
{
TH0=(65536-50000)/256;//向TH0写入初值高8位
TL0=(65536-50000)%256;//向TL0写入初值的底8位
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
6画方波
//方波代码
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitD0=P1^0;
uintcount;
voidt1_init();
main()
{
t1_init();
while
(1);
}
voidt1_init()
{
D0=1;
TMOD=0x10;
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
EA=1;
ET1=1;
TR1=1;
}
voidt1_timer()interrupt3
{
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
count++;
if(count==2)
{
D0=~D0;
count=0;
}
}
7小喇叭
//喇叭代码
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitvoice=P2^0;
voiddelay(uintz)
{
uintx,y;
for(x=0;xfor(y=0;y<=123;y++);
}
voidmain()
{
while
(1)
{
voice=0;
delay(50);
voice=1;
delay(50);
}
}