单片机程序大全

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1独立按键#includereg52.h#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--){for(j=0;j115;j++)//1ms基准延时程序{;}}}voidbeep()//产生1KHZ频率声音的函数{fmq=0;delay500us();2fmq=1;delay500us();}voiddelay500us(){unsignedcharj;for(j=0;j57;j++)//500us基准延时程序{;}}voidmain(){uchart;P27=0;while(1){if(K5==0){delay1ms(15);if(K5==0){for(t=0;t100;t++){beep();}num=num+1;if(num==16){num=0;}3while(!K5);//等待按键释放}}P0=s8[num];}}#includereg52.h#defineucharunsignedchar//数据类型的宏定义#defineuintunsignedint//数据类型的宏定义sbitP27=P2^7;unsignedcharcodetable[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF};//共阳数码管显示字型码数组10111111-voiddelay1ms(uinti){ucharj;while(i--){for(j=0;j115;j++);//1ms基准延时程序4}}voiddisplay(unsignedchari){P27=0;//选通最有端的数码管显示P0=table[i];//显示i参数传来的字型码}voidmain(void){unsignedchartemp;display(16);//初始显示-while(1){P1=0xff;//先向P1口写1;端口读状态P1=0xf0;temp=P1;if(temp!=0xf0){delay1ms(15);if(temp!=0xf0){P1=0xfe;5temp=P1;switch(temp){case(0xee):display(0);break;case(0xde):display(1);break;case(0xbe):display(2);break;case(0x7e):display(3);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){6case(0xeb):display(8);break;case(0xdb):display(9);break;case(0xbb):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。根据线反转法扫描键盘返回的键值编码查键值编码表,从而得到键值并送数码管显示。开机时,数码管显示“-”。*/7#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitP27=P2^7;sbitfmq=P3^6;ucharkey;unsignedcharcodedisp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};unsignedcharcodekey_code[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};voiddelay1ms(uinti){ucharj;while(i--){for(j=0;j115;j++)//1ms基准延时程序{;}}}8voiddelay500us(){unsignedcharj;for(j=0;j57;j++)//500us基准延时程序{;}}voidbeep()//产生1KHZ频率声音的函数{fmq=0;delay500us();fmq=1;delay500us();}/**********************************************************键盘扫描子函数**********************************************************/ucharkeyscan(){ucharscan1,scan2,keycode,j;P1=0xf0;9scan1=P1;if((scan1&0xf0)!=0xf0)//判键是否按下{delay1ms(20);//延时20msscan1=P1;if((scan1&0xf0)!=0xf0)//二次判键是否按下{P1=0x0f;//线反转法的核心scan2=P1;keycode=scan1|scan2;//组合成键编码for(j=0;j=15;j++){if(keycode==key_code[j])//查表得键值{key=j;return(key);}}}}elseP1=0xff;//P1口写1输入状态10return(16);//-}main(){uchartt;P0=0xbf;P27=0;//数码管显示-P1=0xff;while(1){P1=0xf0;if((P1&0xf0)!=0xf0){keyscan();P0=disp_code[key];for(tt=0;tt200;tt++){beep();}fmq=1;delay1ms(200);}}}11中断#includereg52.h#defineuintunsignedint#defineucharunsignedcharsbitdula=P2^6;sbitwela=P2^7;sbitd1=P1^0;ucharnum;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};voiddelay(uintz);voidmain(){EA=1;//开总中断EX0=1;//开外部中断0//IT0=1;//TCON=0x01;wela=1;//11101010P0=0xea;12wela=0;while(1){for(num=0;num16;num++){d1=1;dula=1;P0=table[num];dula=0;delay(1000);}}}voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voidexter0()interrupt0{d1=0;13}定时器#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedcharucharaa,num;sbitdula=P2^6;sbitwela=P2^7;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};voidmain(){num=0;aa=0;//TMOD=0X00;//设定定时器0为工作方式0TMOD=0X01;//设定定时器0为工作方式1//TH0=(8192-5000)/32;//TL0=(8192-5000)%32;14TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;//开总中断ET0=1;//开定时器0中断TR0=1;//启动定时器0wela=1;P0=0x24;wela=0;dula=1;P0=0x3f;dula=0;while(1){if(aa==20){aa=0;num++;if(num==10)num=0;dula=1;P0=table[num];dula=0;15}}}voidtimer0()interrupt1{//TH0=(8192-5000)/32;//TL0=(8192-5000)%32;TH0=(65536-50000)/256;TL0=(65536-50000)%256;aa++;}中断控制LED//用定时器T0的中断控制1位LED闪烁#includereg51.h//包含51单片机寄存器定义的头文件sbitD0=P0^0;//将D0位定义为P0.0引脚unsignedinti;/**************************************************************函数功能:主函数**************************************************************/voidmain(void)16{i=0;EA=1;//开总中断ET0=1;//定时器T0中断允许TMOD=0x01;//使用定时器T0的模式1TH0=(65536-50000)/256;//定时器T0的高8位赋初值TL0=(65536-50000)%256;//定时器T0的高8位赋初值TR0=1;//启动定时器T0while(1);//无限循环等待中断}/**************************************************************函数功能:定时器T0的中断服务程序**************************************************************/voidTime0(void)interrupt1using0//“interrupt”声明函数为中断服务函数//其后的1为定时器T0的中断编号;0表示使用第0组工作寄存器{17i=i+1;if(i==40){D0=~D0;//按位取反操作,将P0.0引脚输出电平取反i=0;}TH0=(65536-50000)/256;//定时器T0的高8位重新赋初值TL0=(65536-50000)%256;//定时器T0的高8位重新赋初值}中断控制LED2#includereg52.h//包含52单片机寄存器定义的头文件/*******************************************函数功能:主函数******************************************/voidmain(void){EA=1

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功