#includereg51.h#definedataportP1#defineucharunsignedchar#defineuintunsignedintsbitlatch1=P2^2;//段锁存sbitlatch2=P2^3;//位锁存sbitkey1=P3^2;sbitkey2=P3^3;unsignedintduanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79};unsignedintweima[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedchardisplay[8];voidsuocun(ucharfirstbit,ucharnum);voiddelay(ucharz)//延时程序{uchari,j,k;for(i=0;i50;i++)for(j=0;j50;j++)for(k=0;kz;k++);}voidmain(void){ucharnum=0;key1=1;key2=1;while(1){if(!key1){delay(5);if(!key1){num++;if(num==10)num=0;}}if(!key2){delay(5);if(!key2){num--;if(num==0)num=9;}}display[0]=duanma[num%10];suocun(2,1);}}voidsuocun(ucharfirstbit,ucharnum){uchari;for(i=0;inum;i++){dataport=0;latch1=1;latch1=0;dataport=weima[firstbit+i];latch2=1;latch2=0;dataport=display[i];latch1=1;latch1=0;}}最近在学习51单片机,因此偶尔也会写写小程序,因此和大家分享一下,希望大家都能学好单片机。