资源描述:
《单片机程序大全.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、独立按键#include#defineucharunsignedchar#defineuintunsignedintsbitP27=P2^7;sbitK5=P1^4;sbitfmq=P3^6;ucharnum;unsignedcharcodes8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddelay1ms(uinti){ucharj;while(i
2、--){for(j=0;j<115;j++)//1ms基准延时程序{;}}}voidbeep()//产生1KHZ频率声音的函数{fmq=0;delay500us();精选范本,供参考!fmq=1;delay500us();}voiddelay500us(){unsignedcharj;for(j=0;j<57;j++)//500us基准延时程序{;}}voidmain(){uchart;P27=0;while(1){if(K5==0){delay1ms(15);if(K5==0){for(t=0;t<
3、100;t++){beep();}num=num+1;if(num==16){num=0;}精选范本,供参考!while(!K5);//等待按键释放}}P0=s8[num];}}#include#defineucharunsignedchar//数据类型的宏定义#defineuintunsignedint//数据类型的宏定义sbitP27=P2^7;unsignedcharcodetable[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x8
4、0,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF};//共阳数码管显示字型码数组10111111"-"voiddelay1ms(uinti){ucharj;while(i--){for(j=0;j<115;j++);//1ms基准延时程序精选范本,供参考!}}voiddisplay(unsignedchari){P27=0;//选通最有端的数码管显示P0=table[i];//显示i参数传来的字型码}voidmain(void){unsignedchartemp;di
5、splay(16);//初始显示"-"while(1){P1=0xff;//先向P1口写1;端口读状态P1=0xf0;temp=P1;if(temp!=0xf0){delay1ms(15);if(temp!=0xf0){P1=0xfe;精选范本,供参考!temp=P1;switch(temp){case(0xee):display(0);break;case(0xde):display(1);break;case(0xbe):display(2);break;case(0x7e):display(3)
6、;break;}P1=0xfd;temp=P1;switch(temp){case(0xed):display(4);break;case(0xdd):display(5);break;case(0xbd):display(6);break;case(0x7d):display(7);break;}P1=0xfb;temp=P1;switch(temp){精选范本,供参考!case(0xeb):display(8);break;case(0xdb):display(9);break;case(0xbb
7、):display(10);break;case(0x7b):display(11);break;}P1=0xf7;temp=P1;switch(temp){case(0xe7):display(12);break;case(0xd7):display(13);break;case(0xb7):display(14);break;case(0x77):display(15);break;}}}}}/*行线接P1.0-P1.3,列线接P1.4-P1.7。根据线反转法扫描键盘返回的键值编码查键值编码表,从
8、而得到键值并送数码管显示。开机时,数码管显示“-”。*/精选范本,供参考!#include#include#defineucharunsignedchar#defineuintunsignedintsbitP27=P2^7;sbitfmq=P3^6;ucharkey;unsignedcharcodedisp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0