您好,欢迎访问三七文档
单片机实验报告姓名:钟林春班级:0801205学号:2012212963任课教师:张开碧上课地点:S4111实验一流水灯实验一、实验目的及要求1、闪烁的LED;2、从左到右的流水灯;3、8只LED左右来回点亮;4、LED模拟交通灯。要求1:LED按设定的时间间隔闪烁要求2:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果要求3:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果;二、实验原理电路图1、电路图2、2三、源程序要求1:#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitLED=P1^0;//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i120;i++);}}//主程序voidmain(){while(1){LED=~LED;DelayMS(150);}}要求2:#includereg51.h#includeintrins.h//包含程序中的_cror_(P0,y)#defineucharunsignedchar#defineuintunsignedintucharled;voiddelay(unsignedinti){uintj;uchark;for(j=i;j0;j--)for(k=125;k0;k--);}voidmain(){led=0x7f;//只亮第一个delay(1000);while(1){P0=led;delay(500);3led=_cror_(led,1);//P0逐步右移一位。}}要求3:#includereg51.h#includeintrins.h//包含程序中的_crol_(P0,y)#defineucharunsignedchar#defineuintunsignedintsbitled=P0^0;uchari;voiddelay(unsignedinti){uintj;uchark;for(j=i;j0;j--)for(k=125;k0;k--);}voidmain(){P0=0xfe;delay(1000);while(1){for(i=0;i8;i++){P0=led;delay(500);led=_crol_(led,1);//P0逐步左移一位。}for(i=0;i8;i++){P0=led;delay(500);led=_cror_(led,1);//P0逐步右移一位}}}4实验二独立键盘实验一、实验目的1、K1-K4按键状态显示;2、K1-K4控制LED移位;3、单只数码管循环显示0~9;4、K1-K4控制数码管移位显示。要求1:K1、K2按下时LED点亮,松开时熄灭,K3、K4按下并释放时LED点亮,再次按下并释放时熄灭;要求2:按下K1时,P0口LED上移一位;按下K2时,P0口LED下移一位;按下K3时,P2口LED上移一位;按下K4时,P2口LED下移一位;要求3:主程序中的循环语句反复将0~9的段码送至P0口,使数字0~9循环显示;要求4:按下K1时加1计数并增加显示位,按下K2时减1计数并减少显示位,按下K3时清零二、实验原理电路图1、电路图2、电路图3、电路图4、5三、源程序要求1:#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitLED1=P0^0;sbitLED2=P0^1;sbitLED3=P0^2;sbitLED4=P0^3;sbitK1=P1^0;sbitK2=P1^1;sbitK3=P1^2;sbitK4=P1^3;//延时voidDelayMS(uintx){uchari;while(x--)for(i=0;i120;i++);}//主程序voidmain(){P0=0xff;P1=0xff;while(1){LED1=K1;LED2=K2;if(K3==0){while(K3==0);LED3=~LED3;}if(K4==0){while(K4==0);LED4=~LED4;6}DelayMS(10);}}要求2:#includereg51.h#defineucharunsignedchar#defineuintunsignedintucharled1;ucharled2;sbitK1=P1^0;sbitK2=P1^1;sbitK3=P1^2;sbitK4=P1^3;//延时voidDelayMS(uintx){uchari;while(x--)for(i=0;i120;i++);}//主程序voidmain(){led1=0xfe;Led2=0xfe;while(1){if(K1==0){while(K1==0);P0=led1;led=_crol_(led1,1);//P0口LED上移一位。}if(K2==0){while(K2==0);P0=led1;led=_cror_(led1,1);//P0口LED下移一位。}if(K3==0){while(K3==0);7P2=led2;Led1=_crol_(led2,1);//P0口LED上移一位。}if(K4==0){while(K4==0);P2=led2;Led2=_cror_(led2,1);//P0口LED下移一位。}}}要求3:#includereg51.h#defineucharunsignedchar#defineuintunsignedintucharcodedisplay[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//延时VoidDelayMS(uintx){uchari;while(x--)for(i=0;i120;i++);}//主程序voidmain(){uchari;while(1){for(i=0;i10;i++){P0=display[i];DelayMS(500)}i=0;}}要求4:#includereg51.h#defineucharunsignedchar#defineuintunsignedint8sbitK1=P1^0;SbitK2=P1^1;sbitK3=P1^2;ucharcodedisplay[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//延时VoidDelayMS(uintx){uchari;while(x--)for(i=0;i120;i++);}//主程序voidmain(){intnum;uchari;P2=1;while(1){If(K1==0){while(K1==0);num++;If(num==10)Num=0;}If(K2==0){while(K2==0);Num--;If(num==-1)Num=0;}If(K3==0){while(K3==0);Num=0;}P0=display[num];}}9实验三矩阵键盘实验一、实验目的及要求1、4X4矩阵键盘控制条形LED显示;2、数码管显示4X4矩阵键盘按键号;要求1:运行本例时,按下的按键值越大点亮的LED越多要求2:按下任意键时,数码管都会显示其键的序号,扫描程序首先判断按键发生在哪一列,然后根据所发生的行附加不同的值,从而得到按键的序号二、实验原理电路图1、电路图2、10三、源程序要求1:#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint//矩阵键盘按键特征码表ucharcodeKeyCodeTable[]={0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88};//延时voidDelayMS(uintx){uchari;while(x--)for(i=0;i120;i++);}//键盘扫描ucharKeys_Scan(){UcharsCode,kCode,i,k;//低4位置0,放入4行P1=0xf0;//若高4位出现0,则有键按下if((P1&0xf0)!=0xf0){DelayMS(2);if((P1&0xf0)!=0xf0){sCode=0xfe;//行扫描码初值for(k=0;k4;k++)//对4行分别进行扫描{P1=sCode;if((P1&0xf0)!=0xf0){kCode=~P1;for(i=0;i16;i++)//查表得到按键序号并返回if(kCode==KeyCodeTable[i])11return(i);}elsesCode=_crol_(sCode,1);}}}return(-1);}//主程序voidmain(){uchari,P2_LED,P3_LED;ucharKeyNo=-1;//按键序号,-1表示无按键while(1){KeyNo=Keys_Scan();//扫描键盘获取按键序号KeyNoif(KeyNo!=-1){P2_LED=0xff;P3_LED=0xff;for(i=0;i=KeyNo;i++)//键值越大,点亮的LED越多{if(i8)P3_LED=1;elseP2_LED=1;}P3=P3_LED;//点亮条形LEDP2=P2_LED;}}}要求2:#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintunsignedcharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F的码表voidmatrixkeyscan()//矩阵键盘12{P1=0xfe;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;temp=temp&0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xee:key=1,num=0;break;case0xde:key=2,num=1;break;case0xbe:key=3,num=2;break;case0x7e:key=4,num=3;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xfd;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;temp=temp&0xf0;if(temp!=0xf0){temp=P1;switch(temp){13case0xed:key=5,num=4;break;case0xdd:key=6,num=5;break;case0xbd:key=7,num=6;break;case0x7d:key=8,num=7;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xfb;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;temp=temp&0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xeb:key=9,num=8;break;case0xdb:key=9,num=9;break;case0xbb:key=10;break;case0x7b:key=11;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}
本文标题:单片机实验报告
链接地址:https://www.777doc.com/doc-3096866 .html