第二章任务一:闪烁广告灯的设计利用89c51单片机的端口控制两个LED(D0和D1),编写程序,实现两个LED互闪。#includereg52.h#defineuintunsignedint#defineucharunsignedcharsbitLED1=P0^0;sbitLED2=P0^1;voiddelayms(uintms){uinti;while(ms--){for(i=0;i120;i++);}}voidmain(){while(1){LED1=0;LED2=1;delayms(500);LED1=1;LED2=0;delayms(500);}}任务二:流水广告灯的设计利用89c51单片机的端口控制8个LED(D0~D7)循环点亮,刚开始时D0点亮,延时片刻后,接着D1点亮,然后依次点亮D2-D3-D4-D5-D6-D7,然后再点亮D7-D6-D5-D4-D3-D2-D1-D0,重复循环。#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedcharuinti;uchartemp;uinta[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};voiddelayms(uintms){while(ms--){uintj;for(j=0;j120;j++);}}voidmain(){while(1){P0=0xfe;while(P0!=0x7f){//P1=temp;//delayms(500);P0=_crol_(P0,1);delayms(250);}temp=0x7f;while(P0!=0xfe){P0=temp;delayms(500);temp=_cror_(temp,1);}}}任务三:拉幕式与闭幕式广告灯的设计利用89c51单片机的P0端口实现8个LEDD0~D7的拉幕式与闭幕式广告灯设计。拉幕式:开始D0~D7全灭,延时片刻后首先D3和D4亮,其次是D2和D5亮,再是D1和D6亮,最后是D0和D7亮。闭幕式:开始D0~D7全亮,延时片刻后首先D0和D7灭,其次是D1和D6灭,再是D2和D5灭,最后是D3和D4灭。#includereg52.h#defineuintunsignedint#defineucharunsignedcharucharcodetable[]={0xe7,0xc3,0x81,0x00};voiddelay(uintx){uinti,j;for(i=x;i0;i--)for(j=120;j0;j--);}voidmain(){uinti;while(1){P1=0xff;delay(500);for(i=0;i4;i++){P1=table[i];delay(500);}for(i=3;i0;i--){P1=table[i-1];delay(500);}}}任务四:复杂广告灯的设计利用89c51单片机的端口实现8个LED(D0~D7)复杂广告灯的控制,要求显示规律为:正向流水-反向流水-隔灯闪烁3次-高四盏、低四盏闪烁2次-隔两盏闪烁3次,再重复循环。#includereg52.h#defineuintunsignedint#includeintrins.hvoiddelayms(uintms){uinti;while(ms--){for(i=0;i110;i++);}}voidmain(){uinti,j,xx[2]={0xaa,0xff},yy[2]={0xf0,0x0f},zz[2]={0xb6,0xff};unsignedcharaa;while(1){aa=0xfe;for(i=0;i7;i++){P0=aa;aa=_crol_(aa,1);delayms(500);}for(i=0;i8;i++){P0=aa;aa=_cror_(aa,1);delayms(500);}for(i=0;i3;i++)for(j=0;j2;j++){P0=xx[j];delayms(500);}for(i=0;i2;i++)for(j=0;j2;j++){P0=yy[j];delayms(500);}for(i=0;i3;i++)for(j=0;j2;j++){P0=zz[j];delayms(500);}}}任务五:单个LED数码管显示设计使用89c51的P0口作为输出口,外接LED数码管,编写程序,使数码管循环显示从0~9的加1计数。#includereg52.h#defineuintunsignedint#defineucharunsignedcharuchartable[10]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};uintkey=0;voidint_int(){EA=1;IT0=1;EX0=1;}voidcount_num()interrupt0{if(key==9)key=0;elsekey++;}voidmain(){int_int();while(1){P0=table[key];}}任务六:多个LED数码管显示设计使用单片机P0和P2口作为输出口,外接一个8位LED数码管,编写程序,使数码管显示“872AF635”。#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedchardelay(ms){while(ms--){inti;for(i=0;i110;i++);}}main(){uchara[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};ucharb[8]={0x08,0x07,0x02,0x0a,0x0f,0x06,0x03,0x05},j;uinti;while(1){j=0x01;for(i=0;i8;i++){P2=j;P0=a[b[i]];j=_crol_(j,1);delay(3);}}}任务七:查询式按键设计将8个按键从1~8进行编号,如果其中一个键被按下,则在LED数码管上显示相应的键值。#includereg52.h#defineuintunsignedint#defineucharunsignedcharmain(){uchara[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};P0=0xff;while(P0!=0xff){switch(P0){case0xfe:P2=a[1];break;case0xfd:P2=a[2];break;case0xfb:P2=a[3];break;case0xf7:P2=a[4];break;case0xef:P2=a[5];break;case0xdf:P2=a[6];break;case0xbf:P2=a[7];break;case0x7f:P2=a[8];}}}任务八:LED数码管显示矩阵键盘按键的设计设计一个4*4的矩阵键盘,以P1.0~P1.3作为行线,以P1.4~P1.7作为列线。要求:未按下按键时,LED数码管显示“-”,按下按键时,在数码管上显示相应的键值。#includereg52.h#defineucharunsignedchar#defineuintunsignedintuchartable[17]={0xbf,0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};uintnum=0;voiddelay(uintx){uchari,j;for(i=x;i0;i--)for(j=120;j0;j--);}keyscan(){uinttemp;temp=P2;temp&=0x0f;if(temp==0x0e){P2=0xf0;temp=P2;temp&=0xf0;switch(temp){case0xe0:num=1;break;case0xd0:num=2;break;case0xb0:num=3;break;case0x70:num=4;}}if(temp==0x0d){P2=0xf0;temp=P2;temp&=0xf0;switch(temp){case0xe0:num=5;break;case0xd0:num=6;break;case0xb0:num=7;break;case0x70:num=8;}}if(temp==0x0b){P2=0xf0;temp=P2;temp&=0xf0;switch(temp){case0xe0:num=9;break;case0xd0:num=10;break;case0xb0:num=11;break;case0x70:num=12;}}if(temp==0x07){P2=0xf0;temp=P2;temp&=0xf0;switch(temp){case0xe0:num=13;break;case0xd0:num=14;break;case0xb0:num=15;break;case0x70:num=16;}}returnnum;}voiddisplay(uintnum){if(num==0)P0=0xbf;P0=table[num];}main(){uinttemp;while(1){P2=0x0f;temp=P2;if(temp!=0x0f)keyscan();display(num);}}(2)#includereg52.h#defineucharunsignedchar#defineuintunsignedintsbitLED1=P2^0;sbitLED2=P2^1;ucharnum;ucharcodeLED_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//数码管字形码编码voiddelay(uintx){uchari,j;for(i=x;i0;i--)for(j=120;j0;j--);}//延时函数voiddisplay(ucharnum){P0=LED_code[num];}//数码管显示函数voidkeyscan()//键盘扫描函数{uchartemp;P1=0xfe;//key1-key4第一行按下temp=P1;temp&=0xf0;if(temp!=0xf0){delay(10);//消除抖动temp=P1;temp&=0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xee:num=1;break;case0xde:num=2;break;case0xbe:num=3;break;case0x7e:num=4;break;}}while(temp!=0xf0)//等待按键释放{temp=P1;temp&=0xf0;}}P1=0xfd;//key5-key8第二行按下temp=P1;temp&=0xf0;if(temp!=0xf0){delay(10);//消除抖动temp=P1;temp&=0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xed:num=5;break;case0xdd:num=6;break;case0xbd:num=7;break;case0x7d:num=8;break;}}while(temp!=0xf0)//等待按