您好,欢迎访问三七文档
1第一位隔一秒闪烁一次#includereg52.h#defineuintunsignedintsbitled1=P1^0;uinti;uintj;voidmain(){while(1){led1=0;for(i=1000;i0;i--)for(j=110;j0;j--);led1=1;for(i=1000;i0;i--)for(j=110;j0;j--);}}2复杂广告灯#includereg52.h#defineuintunsignedint#defineucharunsignedcharuchardiscode[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,//正向流水灯0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF,//反向流水灯0xAA,0x55,0xAA,0x55,0xAA,0x55,0xFF,//隔灯闪烁3次0xF0,0x0F,0xF0,0x0F,0xFF,//高四盏。低四盏闪烁2次0x33,0xCC,0x33,0xCC,0x33,0xCC,0xFF//隔两盏闪烁3次};voiddelayms(uintms){uinti;while(ms--){for(i=0;i120;i++);}}voidmain(void){uchari;P0=0xFF;while(1){for(i=0;i35;i++){P0=discode[i];delayms(250);}}}3拉幕式与闭幕式广告灯#includereg52.h#defineuintunsignedint#defineucharunsignedcharuchardiscode1[4]={0x18,0x24,0x42,0x81};uchardiscode2[4]={0x7E,0x3C,0x18,0x00};voiddelayms(uintms){uinti;while(ms--){for(i=0;i120;i++);}}voidmain(void){uchari,j;P0=0xFF;while(1){for(i=0;i4;i++){j=discode1[i];P0=~j;delayms(500);}j=0xFF;P0=~j;delayms(500);for(i=0;i4;i++){j=discode2[i];P0=~j;delayms(500);}}}4流水灯跑马灯左移右移#includereg52.h#defineuintunsignedint#defineucharunsignedcharvoiddelay(uints){uinti;while(s--){for(i=0;i120;i++);}}voidmain(void){uchari,j;P0=0xFF;while(1){j=0x01;for(i=0;i8;i++){P0=~j;delay(500);j=j1;}P0=0xFF;delay(1000);j=0x80;for(i=0;i8;i++){P0=~j;delay(500);j=j1;}}}5千位静态显示数字#includereg52.hsbitSMG_q=P1^0;//定义数码管阳级控制脚(千位)sbitSMG_b=P1^1;//定义数码管阳级控制脚(百位)sbitSMG_s=P1^2;//定义数码管阳级控制脚(十位)sbitSMG_g=P1^3;//定义数码管阳级控制脚(个位)voidmain(){SMG_q=0;P0=0xF8;while(1);}6,4位数码管同时静态显示#includereg52.h#defineuncharunsignedchar#defineuintunsignedintsbitSMG_q=P1^0;//定义数码管阳级控制脚(千位)sbitSMG_b=P1^1;//定义数码管阳级控制脚(百位)sbitSMG_s=P1^2;//定义数码管阳级控制脚(十位)sbitSMG_g=P1^3;//定义数码管阳级控制脚(个位)uncharmun;uchar;codetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddelay(uint);voidmain(void){SMG_q=0;SMG_b=0;SMG_s=0;SMG_g=0;//P1=0xf0;两种都可以实现。。。while(1){for(mun=0;mun16;mun++){P0=table[mun];delay(500);}}}voiddelay(uints){uinti;while(s--)for(i=120;i0;i--);}7.数码管动态显示#includereg52.h#defineucharunsignedchar#defineuintunsignedintsbitq=P1^0;//定义数码管阳级控制脚(千位)sbitb=P1^1;//定义数码管阳级控制脚(百位)sbits=P1^2;//定义数码管阳级控制脚(十位)sbitg=P1^3;//定义数码管阳级控制脚(个位)//uncharmun;uchar;codetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddelay(uint);voidmain(void){while(1){q=0;P0=table[1];delay(500);q=1;b=0;P0=table[2];delay(500);b=1;s=0;P0=table[3];delay(500);s=1;g=0;P0=table[4];delay(500);g=1;}}voiddelay(uints){uinti;while(s--)for(i=120;i0;i--);}8.1中断定时器0工作1LED1s闪烁#includereg52.h#defineuintunsignedint#defineucharunsignedcharsbitled1=P1^0;ucharnum;voidmain(){TMOD=0x01;TH0=(65536-45872)/256;TL0=(65536-45872)%256;EA=1;ET0=1;TR0=1;while(1);}voidT0_time()interrupt1{TH0=(65536-45872)/256;TL0=(65536-45872)%256;num++;if(num==50){num=0;led1=~led1;}}8.2中断定时器LED,前两位数码管59s循环计数#includereg52.h#defineucharunsignedchar#defineuintunsignedintsbitled1=P0^0;ucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddelay(uint);voiddisplay(uchar,uchar);ucharnum,num1,num2,shi,ge;voidmain(void){TMOD=0x11;TH0=(65536-45872)/256;TL0=(65536-45872)%256;TH1=(65536-45872)/256;TL1=(65536-45872)%256;EA=1;ET0=1;ET1=1;TR0=1;TR1=1;while(1){display(shi,ge);}}voiddisplay(ucharshi,ucharge){P1=0xff;P1=0xfd;P0=table[shi];delay(10);P1=0x00;P1=0xff;P1=0xfe;P0=table[ge];delay(10);P1=0x00;}voiddelay(uints){uinti,j;for(i=s;i0;i--)for(j=110;j0;j--);}voidT0_time()interrupt1{TH0=(65536-45872)/256;TL0=(65536-45872)%256;num1++;if(num1==50){num1=0;led1=~led1;}}voidT1_time()interrupt3{TH1=(65536-45872)/256;TL1=(65536-45872)%256;num2++;if(num2==20){num2=0;num++;if(num==60)num=0;shi=num/10;ge=num%10;}}//灯的亮灭。。。。9.1独立键盘使用#includereg52.h#defineucharunsignedchar#defineuintunsignedintsbitk1=P3^4;//独立键盘,k1k2k3k4分别接,p34.p35p36p37.sbitk2=P3^5;sbitk3=P3^6;sbitk4=P3^7;sbitd=P1^2;sbitc=P1^3;ucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddelayms(uint);ucharnumt0,num;voiddisplay(ucharnumdis)//显示59s的常用方式{ucharshi,ge;shi=numdis/10;ge=numdis%10;P1=0xff;//控制十位数字P1=0xfd;P0=table[shi];delayms(10);P1=0x00;P1=0xff;//控制个位数字P1=0xfe;P0=table[ge];delayms(10);P1=0x00;}voiddelayms(uintxms)//简单的延时程序{uinti,j;for(i=xms;i0;i--)for(j=110;j0;j--);}voidinit()//初始化函数{TMOD=0x01;TH0=(65536-45872)/256;TL0=(65536-45872)%256;EA=1;ET0=1;}voidkeyscan()//控制键盘的程序{if(k1==0){delayms(10);//去抖动延时if(k1==0){num++;if(num==60)num=0;while(!k1);//等待按键释放}}if(k2==0){delayms(10);if(k2==0){if(num==0)num=60;num--;while(!k2);}}if(k3==0){delayms(10);if(k3==0){num=0;while(!k3);}}if(k4==0){delayms(10);if(k4==0){while(!k4);TR0=~TR0;}}}voidmain()//主函数{init();//1,先初始化数据init()while(1){keyscan();//键盘选择?display(num);//显示两位数字函数}}voidT0_time()interrupt1//中断服务程序{TH0=(65536-45872)/256;TL0=(65536-45872)%256;numt0++;if(numt0==20)//1s的判断{numt0=0;num++;if(num==60)num=0;}}9.2矩阵键盘使用#includereg52.h#defineucharunsignedchar#defineuintunsignedintucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0
本文标题:51单片机基本程序
链接地址:https://www.777doc.com/doc-4627584 .html