您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > 单片机技能大赛编程大全
一.开关检测器程序编写:ORG00HSTART:MOVA,P1ANLA,#0F0HRRARRARRARRAORlA,#0F0HMOVP1,ASJMPSTARTORG00HSTART:MOVA,P1ANLA,#0F0HRRARRARRARRAORlA,#0F0HMOVP1,ASJMPSTART二:节日彩灯控制器编程:#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitkey=P1^2;ucharcodetable_P0[]={0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};ucharcodetable_P2[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};//延时voiddelay(uintxms){uinti,j;for(i=xms;i0;i--)for(j=110;j0;j--);}//主程序voidmain(){uchari;if(key==0){delay(10);if(key==0){while(!key);while(1){//从数组中读取数据送至P0和P2口显示for(i=0;i136;i++){P0=table_P0[i];P2=table_P2[i];delay(100);}}}}}三.简单的左右循环流水灯的制作#includereg51.h#includeintrins.hunsignedchartemp;//定义字符变量temp,temp左移或右移并给连接LED的P1口赋值inta;//定义延迟函数delay()voiddelay(void);voidmain(){temp=0xfe;//给temp赋初值P1=temp;//temp赋值给P1口,第一个LED(红色)点亮while(1)//主程序,括号中的程序将一直循环{for(a=0;a7;a++)//左移部分,LED从左到右依次点亮{temp=_crol_(temp,1);//_crol_语句控制变量temp左移delay();//每个灯点亮之后延迟一会在点下一个灯P1=temp;}for(a=0;a7;a++)//右移部分,LED从右到左依次点亮{temp=_cror_(temp,1);//第8个灯点亮后开始右移程序,LED从右向左点亮delay();P1=temp;}}}voiddelay()//延时子程序{inti,j;//定义局部变量i,jfor(i=200;i0;i--)//i,j不断计数占用CPU时间,达到延时目的for(j=1;j150;j++);}四.可控的左右循环流水灯的制作ORG0000H;单片机上电后从0000H地址执行AJMPSTART;跳转到主程序存放地址处ORG0030H;设置主程序开始地址START:MOVSP,#60H;设置堆栈起始地址为60HCLRP1.0;P1.0输出低电平,使LED1点亮ACALLDELAY;调用延时子程序SETBP1.0;P1.0输出高电平,使LED1熄灭CLRP1.1;P1.1输出低电平,使LED2点亮ACALLDELAY;调用延时子程序SETBP1.1;P1.1输出高电平,使LED2熄灭CLRP1.2;P1.2输出低电平,使LED3点亮ACALLDELAY;调用延时子程序SETBP1.2;P1.2输出高电平,使LED3熄灭CLRP1.3;P1.3输出低电平,使LED4点亮ACALLDELAY;调用延时子程序SETBP1.3;P1.3输出高电平,使LED4熄灭CLRP1.4;P1.4输出低电平,使LED5点亮ACALLDELAY;调用延时子程序SETBP1.4;P1.4输出高电平,使LED5熄灭CLRP1.5;P1.5输出低电平,使LED6点亮ACALLDELAY;调用延时子程序SETBP1.5;P1.5输出高电平,使LED6熄灭CLRP1.6;P1.6输出低电平,使LED7点亮ACALLDELAY;调用延时子程序SETBP1.6;P1.6输出高电平,使LED7熄灭CLRP1.7;P1.7输出低电平,使LED8点亮ACALLDELAY;调用延时子程序SETBP1.7;P1.7输出高电平,使LED8熄灭ACALLDELAY;调用延时子程序AJMPSTART;8个LED流了一遍后返回到标号START处再循环DELAY:;延时子程序MOVR0,#255;延时一段时间D1:MOVR1,#255DJNZR1,$DJNZR0,D1RET;子程序返回END;程序结束六.花样流水灯制作#includereg52.h#defineucharunsignedchar#defineuintunsignedintucharcodePattern_P0[]={0xfc,0xf9,0xf3,0xe7,0xcf,0x9f};ucharcodePattern_P2[]={0xf5,0xf6,0xfe,0x54,0x56,0x76,0xd7,0x49,0xa9,0xe4,0xc6};voidDelayMS(uintx){uchart;while(x--){for(t=120;t0;t--);}}voidmain(){uchari;while(1){for(i=136;i0;i--){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(150);}}}
本文标题:单片机技能大赛编程大全
链接地址:https://www.777doc.com/doc-4769093 .html