您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 51单片机c语言经典小程序
Lesson3.1#includereg52.h//调用头文件sbitdula=P2^6;//段定义端口sbitwela=P2^7;//位定义端口voidmain(){wela=1;//打开位端口P0=0xc0;//赋值,决定显示位wela=0;//关闭为端口dula=1;//打开断断口P0=0x06;//赋值,决定显示数值dula=0;//关闭断断口while(1);//进入死循环,防止出现乱码}Lesson3.2#includereg52.h#defineuintunsignedint#defineucharunsignedchar//宏定义sbitdula=P2^6;//段定义端口sbitwela=P2^7;//位定义端口ucharnum;//定义全局变量ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数值代码voiddelay(uintz);//延时函数声明voidmain(){wela=1;//11101010//打开为端口P0=0xea;//赋值,决定显示位wela=0;//关闭为端口while(1)//进入死循环{for(num=0;num16;num++){dula=1;//打开位端口P0=table[num];//显示数值dula=0;//关闭为端口delay(1000);//延时}}}voiddelay(uintz)//掩饰函数体{uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}Lesson3.3#includereg52.h#defineuintunsignedint#defineucharunsignedchar//宏定义sbitdula=P2^6;//sbitwela=P2^7;sbitd1=P1^0;//LED控制端口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;wela=0;while(1){for(num=0;num16;num++){d1=1;//LED关闭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;//执行中断,关闭LED}Lesson3.4#includereg52.h#defineuintunsignedint#defineucharunsignedchar//宏定义sbitdula=P2^6;sbitwela=P2^7;sbitd1=P1^0;//LE灯控制端口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=0xf0;wela=0;while(1){for(num=0;num16;num++){d1=1;dula=1;P0=table[num];dula=0;delay(500);}}}voiddelay(uintz)//延时函数{uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voidexter0()interrupt0//中断函数{d1=0;}Lesson4.1#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedcharuchartemp,aa,num;sbitdula=P2^6;sbitwela=P2^7;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};voiddelay(uintz);voidmain(){num=0;aa=0;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;temp=0xfe;P1=temp;dula=1;P0=table[0];dula=0;wela=1;P0=0xc0;wela=0;while(1){delay(1000);temp=_crol_(temp,1);P1=temp;/*if(aa==40){aa=0;num++;if(num==16)num=0;dula=1;P0=table[num];dula=0;}*/}}voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voidtimer0()interrupt1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;aa++;if(aa==40){aa=0;num++;if(num==16)num=0;dula=1;P0=table[num];dula=0;}}Lesson4.2#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedcharuchartemp,aa,numdu,numwe;sbitdula=P2^6;sbitwela=P2^7;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};ucharcodetablewe[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//voiddelay(uintz);voidmain(){numdu=0;numwe=0;aa=0;TMOD=0x01;TH0=(65536-5000)/256;TL0=(65536-5000)%256;EA=1;ET0=1;TR0=1;while(1){//delay(1000);//temp=_crol_(temp,1);//P1=temp;if(aa==1){aa=0;numdu++;if(numdu==7)numdu=1;dula=1;P0=table[numdu];dula=0;wela=1;P0=tablewe[numwe];wela=0;numwe++;if(numwe==6)numwe=0;}}}/*voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}*/voidtimer0()interrupt1{TH0=(65536-5000)/256;TL0=(65536-5000)%256;aa++;}Lesson4.3#includereg52.h#defineuintunsignedint#defineucharunsignedcharuchartemp,aa,bai,shi,ge;sbitdula=P2^6;sbitwela=P2^7;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};voiddisplay(ucharbai,ucharshi,ucharge);voiddelay(uintz);voidinit();voidmain(){init();//初始化子程序while(1){if(aa==20){aa=0;temp++;if(temp==100){temp=0;}bai=temp/100;shi=temp%100/10;ge=temp%10;}display(bai,shi,ge);}}voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voiddisplay(ucharbai,ucharshi,ucharge){dula=1;P0=table[bai];dula=0;P0=0xff;wela=1;P0=0xfe;wela=0;delay(1);dula=1;P0=table[shi];dula=0;P0=0xff;wela=1;P0=0xfd;wela=0;delay(1);dula=1;P0=table[ge];dula=0;P0=0xff;wela=1;P0=0xfb;wela=0;delay(1);}voidinit(){wela=0;dula=0;temp=0;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}voidtimer0()interrupt1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;aa++;}Lesson5.1#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitdula=P2^6;sbitwela=P2^7;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0x79,0x38,0x3f,0};uchartemp,t0,t1,bai,shi,ge,flag,flag1;uintshu;voidinit();voiddisplay(uchar,uchar,uchar,uchar,uchar,uchar);voiddelay(uintz){uintt1,y;for(t1=z;t10;t1--)for(y=110;y0;y--);}voidmain(){init();while(1){if(flag1!=1)display(7,6,5,bai,shi,ge);elsedisplay(16,17,18,18,19,20);}}voidinit(){shu=432;temp=0xfe;P1=temp;TMOD=0x11;TH0=(65536-50000)/256;TL0=(65536-50000)%256;TH1=(65536-50000)/256;TL1=(65536-50000)%256;EA=1;ET0=1;ET1=1;TR0=1;TR1=1;}voidtimer0()interrupt1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;t0++;if(fla
本文标题:51单片机c语言经典小程序
链接地址:https://www.777doc.com/doc-4293453 .html