您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 单片机C语言课程设计 程序
04花样流水灯/*名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#includereg51.h#defineucharunsignedchar#defineuintunsignedintucharcodePattern_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};ucharcodePattern_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};//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i120;i++);}}//主程序voidmain(){uchari;while(1){//从数组中读取数据送至P0和P2口显示for(i=0;i136;i++){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(100);}}}05LED模拟交通灯/*名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。*/#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitRED_A=P0^0;//东西向灯sbitYELLOW_A=P0^1;sbitGREEN_A=P0^2;sbitRED_B=P0^3;//南北向灯sbitYELLOW_B=P0^4;sbitGREEN_B=P0^5;ucharFlash_Count=0,Operation_Type=1;//闪烁次数,操作类型变量//延时voidDelayMS(uintx){uchari;while(x--)for(i=0;i120;i++);}//交通灯切换voidTraffic_Light(){switch(Operation_Type){case1://东西向绿灯与南北向红灯亮RED_A=1;YELLOW_A=1;GREEN_A=0;RED_B=0;YELLOW_B=1;GREEN_B=1;DelayMS(2000);Operation_Type=2;break;case2://东西向黄灯闪烁,绿灯关闭DelayMS(300);YELLOW_A=~YELLOW_A;GREEN_A=1;if(++Flash_Count!=10)return;//闪烁5次Flash_Count=0;Operation_Type=3;break;case3://东西向红灯,南北向绿灯亮RED_A=0;YELLOW_A=1;GREEN_A=1;RED_B=1;YELLOW_B=1;GREEN_B=0;DelayMS(2000);Operation_Type=4;break;case4://南北向黄灯闪烁5次DelayMS(300);YELLOW_B=~YELLOW_B;GREEN_B=1;if(++Flash_Count!=10)return;Flash_Count=0;Operation_Type=1;}}//主程序voidmain(){while(1)Traffic_Light();}06单只数码管循环显示0~9/*名称:单只数码管循环显示0~9说明:主程序中的循环语句反复将0~9的段码送至P0口,使数字0~9循环显示*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t120;t++);}//主程序voidmain(){uchari=0;P0=0x00;while(1){P0=~DSY_CODE[i];i=(i+1)%10;DelayMS(300);}}078只数码管滚动显示单个数字/*名称:8只数码管滚动显示单个数字说明:数码管从左到右依次滚动显示0~7,程序通过每次仅循环选通一只数码管*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t120;t++);}//主程序voidmain(){uchari,wei=0x80;while(1){for(i=0;i8;i++){P2=0xff;//关闭显示wei=_crol_(wei,1);P0=DSY_CODE[i];//发送数字段码P2=wei;//发送位码DelayMS(300);}}}098只数码管闪烁显示数字串电路如上图/*名称:8只数码管闪烁显示数字串说明:数码管闪烁显示由0~7构成的一串数字本例用动态刷新法显示一串数字,在停止刷新时所有数字显示消失。*/#includereg51.h#defineucharunsignedchar#defineuintunsignedint//段码表ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//位码表ucharcodeDSY_IDX[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t120;t++);}//主程序voidmain(){uchari,j;while(1){for(i=0;i30;i++){for(j=0;j8;j++){P0=0xff;P0=DSY_CODE[j];//发送段码P2=DSY_IDX[j];//发送位码DelayMS(2);}}P2=0x00;//关闭所有数码管并延时DelayMS(1000);}}108只数码管滚动显示数字串电路如上图/*名称:8只数码管滚动显示数字串说明:数码管向左滚动显示3个字符构成的数字串*/#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint//段码表ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//下面数组看作环形队列,显示从某个数开始的8个数(10表示黑屏,即ucharcodeDSY_CODE[10]=0xff)ucharNum[]={10,10,10,10,10,10,10,10,2,9,8};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t120;t++);}//主程序voidmain(){uchari,j,k=0,m=0x80;while(1){//刷新若干次,保持一段时间的稳定显示for(i=0;i15;i++){for(j=0;j8;j++){//发送段码,采用环形取法,从第k个开始取第j个P0=0xff;P0=DSY_CODE[Num[(k+j)%11]];m=_crol_(m,1);P2=m;//发送位码DelayMS(2);}}k=(k+1)%11;//环形队列首支针k递增,Num下标范围0~10,故对11取余}}17数码管显示4X4矩阵键盘按键号/*名称:数码管显示4X4矩阵键盘按键号说明:按下任意键时,数码管都会显示其键的序号,扫描程序首先判断按键发生在哪一列,然后根据所发生的行附加不同的值,从而得到按键的序号。*/#includereg51.h#defineucharunsignedchar#defineuintunsignedint//段码ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00};sbitBEEP=P3^7;//上次按键和当前按键的序号,该矩阵中序号范围0~15,16表示无按键ucharPre_KeyNo=16,KeyNo=16;//延时voidDelayMS(uintx){uchari;while(x--)for(i
本文标题:单片机C语言课程设计 程序
链接地址:https://www.777doc.com/doc-6076502 .html