c51单片机程序100例,c51单片机实例题

首页 > 数码 > 作者:YD1662024-05-01 05:23:41

/* 名称:花样流水灯 说明:16 只 LED 分两组按预设的多种花样变换显示 */ #include<reg51.h> #define uchar unsigned char #define uint unsigned int uchar code Pattern_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 }; uchar code Pattern_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}; //延时 void DelayMS(uint x) { uchar i; while(x--) { for(i=0;i<120;i ); } } //主程序 void main() { uchar i; while(1) { //从数组中读取数据送至 P0 和 P2 口显示 for(i=0;i<136;i ) { P0=Pattern_P0[i]; P2=Pattern_P2[i]; DelayMS(100); } }

c51单片机程序100例,c51单片机实例题(5)

5 LED 模拟交通灯

/* 名称:LED 模拟交通灯 说明:东西向绿灯亮若干秒,黄灯闪烁 5 次后红灯亮, 红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁 5 此后变红灯,东西向变绿灯,如此重复。 */ #include<reg51.h> #define uchar unsigned char #define uint unsigned int sbit YELLOW_B=P0^4; sbit GREEN_B=P0^5; uchar Flash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量 //延时 void DelayMS(uint x) { uchar i; while(x--) for(i=0;i<120;i ); } //交通灯切换 void Traffic_Light() { switch(Operation_Type) { case 1: //东西向绿灯与南北向红灯亮 RED_A=1;YELLOW_A=1;GREEN_A=0; RED_B=0;YELLOW_B=1;GREEN_B=1; DelayMS(2000); Operation_Type=2; break; case 2: //东西向黄灯闪烁,绿灯关闭 DelayMS(300); YELLOW_A=~YELLOW_A;GREEN_A=1; if( Flash_Count!=10) return; //闪烁 5 次 Flash_Count=0; Operation_Type=3; break; case 3: //东西向红灯,南北向绿灯亮 RED_A=0;YELLOW_A=1;GREEN_A=1; RED_B=1;YELLOW_B=1;GREEN_B=0; DelayMS(2000); Operation_Type=4; break; case 4: //南北向黄灯闪烁 5 次 DelayMS(300); YELLOW_B=~YELLOW_B;GREEN_B=1; if( Flash_Count!=10) return; Flash_Count=0; Operation_Type=1; } } //主程序 void main() { while(1) Traffic_Light(); } 6 单只数码管循环显示 0~9 /* 名称:单只数码管循环显示 0~9 说明:主程序中的循环语句反复将 0~9 的段码送至 P0 口,使数字 0~9 循环显示 */ #include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; //延时

c51单片机程序100例,c51单片机实例题(6)

void DelayMS(uint x) { uchar t; while(x--) for(t=0;t<120;t ); } //主程序 void main() { uchar i=0; P0=0x00; while(1) { P0=~DSY_CODE[i]; i=(i 1); DelayMS(300);

c51单片机程序100例,c51单片机实例题(7)

}

}

7 . 8 只数码管滚动显示单个数字

/* 名称:8 只数码管滚动显示单个数字 说明:数码管从左到右依次滚动显示0~7,程序通过每次仅循环选通一只数码 管 */ #include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //延时 void DelayMS(uint x) { uchar t; while(x--) for(t=0;t<120;t ); } //主程序 void main() { uchar i,wei=0x80; while(1) { for(i=0;i<8;i ) { P2=0xff; //关闭显示 wei=_crol_(wei,1); P0=DSY_CODE[i]; //发送数字段码 P2=wei; //发送位码 DelayMS(300); } } } 8 8 只数码管动态显示多个不同字符 电路如上图 /* 名称:8 只数码管动态显示多个不同字符 说明:数码管动态扫描显示 0~7。 */ #include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //延时 void DelayMS(uint x) { uchar t; while(x--) for(t=0;t<120;t ); } //主程序 void main() { uchar i,wei=0x80; while(1) { for(i=0;i<8;i ) { P0=0xff; P0=DSY_CODE[i]; //发送段码 wei=_crol_(wei,1); P2=wei; //发送位码 DelayMS(2); } } } 8 8 只数码管闪烁显示数字串

电路如上图

/* 名称:8 只数码管闪烁显示数字串 说明:数码管闪烁显示由 0~7 构成的一串数字 本例用动态刷新法显示一串数字,在停止刷新时所有数字显示消失。 */ #include<reg51.h> #define uchar unsigned char #define uint unsigned int //段码表 uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //位码表 uchar code DSY_IDX[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //延时 void DelayMS(uint x) { uchar t; while(x--) for(t=0;t<120;t ); } //主程序 void main() { uchar i,j; while(1) { for(i=0;i<30;i ) { for(j=0;j<8;j ) { P0=0xff; P0=DSY_CODE[j]; //发送段码 P2=DSY_IDX[j]; //发送位码 DelayMS(2); } } P2=0x00; //关闭所有数码管并延时 DelayMS(1000); } } 9 8 只数码管滚动显示数字串

电路如上图

/* 名称:8 只数码管滚动显示数字串 说明:数码管向左滚动显示 3 个字符构成的数字串 */ #include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int //段码表 uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; //下面数组看作环形队列,显示从某个数开始的 8 个数(10 表示黑屏) uchar Num[]={10,10,10,10,10,10,10,10,2,9,8}; //延时 void DelayMS(uint x) { uchar t; while(x--) for(t=0;t<120;t ); } //主程序 void main() { uchar i,j,k=0,m=0x80; while(1) { //刷新若干次,保持一段时间的稳定显示 for(i=0;i<15;i ) { for(j=0;j<8;j ) { //发送段码,采用环形取法,从第 k 个开始取第 j 个 P0=0xff; P0=DSY_CODE[Num[(k j)]]; m=_crol_(m,1); P2=m; //发送位码 DelayMS(2); } } k=(k 1); //环形队列首支针 k 递增,Num 下标范围 0~10,故对 11 取余 } } 11 K1-K4 控制 LED 移位 /* 名称:K1-K4 控制 LED 移位 说明:按下 K1 时,P0 口 LED 上移一位; 按下 K2 时,P0 口 LED 下移一位; 按下 K3 时,P2 口 LED 上移一位; 按下 K4 时,P2 口 LED 下移一位; */

c51单片机程序100例,c51单片机实例题(8)

上一页12345下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.