资源描述:
《51单片机基本程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1第一位隔一秒闪烁一次#include#defineuintunsignedintsbitled1=P1^0;uinti;uintj;voidmain(){while(1){led1=0;for(i=1000;i>0;i--)for(j=110;j>0;j--);led1=1;for(i=1000;i>0;i--)for(j=110;j>0;j--);}}2复杂广告灯#include#defineuintunsignedint#defineucharunsignedcharuchardiscode[]={0xFE,0xFD,0
2、xFB,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;i<120;i++);}}voidmain(void){u
3、chari;P0=0xFF;while(1){for(i=0;i<35;i++){P0=discode[i];delayms(250);}}}3拉幕式与闭幕式广告灯#include#defineuintunsignedint#defineucharunsignedcharuchardiscode1[4]={0x18,0x24,0x42,0x81};uchardiscode2[4]={0x7E,0x3C,0x18,0x00};voiddelayms(uintms){uinti;while(ms--){for(i=0;i<120;i++);}}vo
4、idmain(void){uchari,j;P0=0xFF;while(1){for(i=0;i<4;i++){j=discode1[i];P0=~j;delayms(500);}j=0xFF;P0=~j;delayms(500);for(i=0;i<4;i++){j=discode2[i];P0=~j;delayms(500);}}}4流水灯跑马灯左移右移#include#defineuintunsignedint#defineucharunsignedcharvoiddelay(uints){uinti;while(s--){for(i=0
5、;i<120;i++);}}voidmain(void){uchari,j;P0=0xFF;while(1){j=0x01;for(i=0;i<8;i++){P0=~j;delay(500);j=j<<1;}P0=0xFF;delay(1000);j=0x80;for(i=0;i<8;i++){P0=~j;delay(500);j=j>>1;}}}5千位静态显示数字#includesbitSMG_q=P1^0;//定义数码管阳级控制脚(千位)sbitSMG_b=P1^1;//定义数码管阳级控制脚(百位)sbitSMG_s=P1^2;//定义数码
6、管阳级控制脚(十位)sbitSMG_g=P1^3;//定义数码管阳级控制脚(个位)voidmain(){SMG_q=0;P0=0xF8;while(1);}6,4位数码管同时静态显示#include#defineuncharunsignedchar#defineuintunsignedintsbitSMG_q=P1^0;//定义数码管阳级控制脚(千位)sbitSMG_b=P1^1;//定义数码管阳级控制脚(百位)sbitSMG_s=P1^2;//定义数码管阳级控制脚(十位)sbitSMG_g=P1^3;//定义数码管阳级控制脚(个位)uncha
7、rmun;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;mun<16;mun++){P0=table[mun];delay(500);}}}voiddelay(uints){uinti;while(s--)fo
8、r(i=120;i>0;