单片机C语言程序方案实训例——+Proteus仿真(前题)
单片机C语言程序的设计实训100例_基于8051+PROTEUS仿真1
单片机C语言程序设计卖训100例—基于8051+Proteus仿真》案例目录第一篇基础程序设计 (1)01闪烁的LED (1)02 从左到右的流水灯 (1)03 8只LED左右来回点亮 (2)04花样流水灯 (3)05 LED模拟交通灯 (4)06单只数码管循环显示0~9 (5)07 8只数码管滚动显示单个数字 (6)08 8只数码管动态显示多个不同字符 (7)09 8只数码管闪烁显示数字串 (8)108只数码管滚动显示数字串 (9)11K1-K4 控制LED 移位 (10)12K1-K4按键状态显示 (11)13K1-K4 分组控制LED (12)14K1-K4控制数码管移位显示 (13)15K1-K4控制数码管加减演示 (14)164X4矩阵键盘控制条形LED显示 (16)17数码管显示4X4矩阵键盘按键号 (17)18开关控制LED (19)19继电器控制照明设备 (20)20数码管显示拨码开关编码 (21)21开关控制报警器 (22)22按键发音 (22)23播放音乐 (23)24INTO中断计•数 (24)25外部INTO中断控制LED (26)26INTO及INTI中断计数 (26)27定时器控制单只LED (28)28TIMER0控制流水灯 (29)29定时器控制4个LED滚动闪烁 (30)30TO控制LED实现二进制计数 (31)31TIMER0 与TIMER 1 控制条形LED (31)32 10s的秒表 (32)33用计数器中断实现100以的按键计数 (34)34 100 000s以的计时程序 (35)35定时器控制数码管动态显示 (37)36 8X8LED点阵显示数字 (38)37按键控制8X8LED点阵屏显示图形 (39)38用泄时器设计的门铃 (40)39演奏音阶 (41)40按键控制定时器选播多段音乐 (42)41定时器控制交通指示灯 (44)42报警与旋转灯 (46)43串行数据转换为并行数据 (47)44并行数据转换为串行数据 (48)45甲机通过串口控制乙机LED (49)46单片机之间双向通信 (51)47单片机向主机发送字符串 (55)48 单片机与PC通信 (56)第二篇硬件应用 (58)01 74LS138译码器应用 (58)02 74HC154译码器应用 (58)03 74HC595串入并出芯片应用 (59)04 74LS148扩展中断 (60)06 24C04与数码管 (64)07 用6264扩展存 (67)08用8255实现接口扩展(仿真未成功) (67)09 555定时器实验 (69)10BCD译码数码管显示数字 (69)11MAX7221控制数码管动态显示 (70)12LCD 1602字符液晶滚动演示程序 (71)19用ADCO8O8控制PWM输出 (76)20ADC0809数模转换与显示 (77)第一篇基础程序设计01闪烁的LED/*需称:闪烁的LED说明:LED按设定的时间间隔闪烁 */#include<reg51.h>#define uchar unsigned char#define uint unsigned int ;r~sbit LED=P1A O; 丄■//延时void DclayMS(uint x)uchar i;while(x-){ for(i=0;i<120;i++);}}//主程序void main()while(l){LED=~LED;DclayMS(150);02从左到右的流水灯匸/*划称:从左到右的流水灯说明:接在P0 口的8个LED 从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char #define uint unsigned int 〃延时.ioyp.-Z-T"~\ VTAI 1PO.O/ADD>XIALlSZTAI CPO 1/AD1P0.2/AD2P0.3/AD3Al ALzOQTPO 4/ftD4P0.5/AD5PO 6/AD6DA 7/Zi Ti7m IP2.WA6P2.1^9P2.2JA10P3ENALE ETTP2.3JA11P2.4JA12P2.5JA13P2.6.IA14P2.7JA15P1.0 P3.0JRXDP1.1 P3.1/E<DP1.2 P3JJINT0P1.3 P1.4 P3.3JINT1P3.4JT0PI.5 P3.5H-1P1.6 P3B/WRPI.7P3.7JRD1di-t4l=5-10*一—LLdidfdi-II J.和_i_ 匚1•—l—r*iiXTAL2pcmw 町MWP04MC7POVAWM7ZAC7rarMEPIQPI 1PINPl、PI 42P25 7P21<69P22AMDP2SUA11P2M2P2SJAUP3QK>DP31/TXD P〉2询PJ>«7T FP33T0PJ壮瓯曲void DclayMS(uint x)uchar i; while(x-) {for(i=0;i<120;i++); } }//主程序void main() {PO=Oxfe; while(l) {PO=_crol_(POJ);//PO 的值向左循环移动 DclayMS(150);03 8只LED 左右来回点亮/*需称:8只LED 左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*///主程序void main() {uchar i; P2=0x01; while(l)for(i=0:i<7;i++)#include<reg51.h> #include<intrins.h>#define uchar unsigned char #define uint unsigned int//延时void DclayMS(uint x)uchar i; while(x-) { for(i=0;i<120;i++);XTAL1P01'A£?31STP0525斫PZCW P21皿 P22CX10 P23^11ALEAP2WI0 P1.0 P2a«A14 P2TXftl5 P3QQ 口 PI 1 Mimr. PI ? P3.2C1HL Pl 3 P3.3r<n Pt < P 、d/TO”5 PMS PI .6 P3.6VIB PI 7P37.R7Z2QP2=_crol_(P2.1);//P2的值向左循环移动DelayMS(150);for(i=0;i<7;i++){P2=_cror_(P2J);//P2的值向右循坏移动DelayMS(150);04花样流水灯C1torD10□□1D3呦D11吟012XTAURM233R15DI 5沁;D17R3R18 XTALf Fpauxia随Mam冈鬧5S3?/*名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示 */ #include<reg51.h> #define uchar unsigned char #define uint unsigned int uchar code Pattern_PO[]= {0xfct0xf9,0xf3,0xe7,0xcf.0x9f.0x3f.0x7f,0xff,0x(f,0xff0xff.0xff.0xff,0xff,0xff 0xc7・0xdb,0xbd・0x7c・0xbd、0xdb・0xc7,0xff.0xc7・0xc3,0x81,0x00,0x81.0xc3,0xe7,0xff,0xaa,0x55,0x 1 & 0xfT,0xf0,0x0f,0x00,0xfT,0xf8,Oxfl ,0xc3,0xc7,0x8f,0x 1 f.0x3f.0x7f,Ox7f.Ox3f,Ox 1 f,0x8f,0xc7,0xc3,0xf 1,0xf8r0xff,0x00,0x00.0xff,0xff,0x0f,0xf0,0xff0xfe,0xfd,0xfb,0xf7,0xet0xdf?0xbf.0x7f,0xff,0xff0xfL0xff.0xff.0xff.0xff.0xff,0xff,0xff,0xff0xff,0xff0xff.0xff.0xff0x7f.0xbf0xdf,0xeL0xf750xfb.0xfd.0xfe,0xfe,0xfc,0xf8,0xf0,0xe0.0xc0,0x80.0x00,0x00.0x00,0x00.0x00,0x00,0x00.0x00,0x00t0x00,0x00.0x00,0x00,0x00,0x00,0x00.0x00,0x00.0x80.0xc0.0xe0,0xf0,0xf8r0xfc.0xfe, OxOO,Oxff.OxOO.Oxff,OxOO.Oxff.OxOO.Oxff};uchar code Pattern_P2[]={0xff.0xff,0xff,0xff,0xft0xff.0xff.0xfe.0xfc,0xf9.0xf3,0xe7,0xct0x9L0x3f,0xff0xe7,0xdb.0xbd,0x7e,0xbd.0xdb.0xe7,0xff,0xe7.0xc3,0x81,0x00,0x81 ・0xc3,0xc7,0xff.0xaa,0x55,0x 1 & OxfT,OxfO,OxOf,0x00,0xfT,0xf8,Oxfl ,0xc3,0xc7,0x8f,0x 1 f.0x3f.0x7f, 0x7f,0x3f,0x 1 f,0x8f,0xc7,0xe3,0xf 1,0xf8,0xff,0x00,0x00.0xff,0xff,0x0f,0xf0,0xff0xff.0xff,0xff0xff,0xft0xff.0xff.0xff.0xfe,0xfd.0xfb,0xf7,0xef,0xdC0xbf.0x7f0x7f.0xbf0xdf,0xet0xf7.0xfb.0xfd.0xfe.0xff,0xff,0xff,0xff0xffr0xff.0xff.0xff,0xff.0xff,0xff,0xff,0xff.0xff.0xff.0xff.0xfe,0xfc50xf8.0xf0,0xe0.0xc0.0x80.0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xfT,0xff, OxOO,Oxff.OxOO.Oxff,OxOO.Oxff,OxOO.Oxff};03D2D142XR16R17〃延时void DelayMS(uint x){ uchar i;while(x-){for(i=0;i<120;i++);〃主程序void main(){uchar i;while(l){ 〃从数组中读取数据送至P0和P2 口显示for(i=0;i<136;i++){PO=Pattern_PO[i];P2=Pattern_P2[i];DelayMS(lOO);05 LED模拟交通灯/*名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计实训100例基于8051+PROTEUS仿真1
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51、h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51、h>#include<intrins、h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_与_cror_形成来回滚动的效果*/#include<reg51、h>#include<intrins、h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51、h>#define uchar unsigned char#define uint unsigned intuchar 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);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮, 红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计实训 100 例—基于 8051+Proteus仿真
08 8 只数码管动态显示多个不同字符
#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);
for(i=0;i<120;i++);
} } //主程序
void main() { uchar i; P2=0x01; while(1) {
for(i=0;i<7;i++) { P2=_crol_(P2,1); //P2 的值向左循环移动 DelayMS(150); } for(i=0;i<7;i++) { P2=_cror_(P2,1); //P2 的值向右循环移动 DelayMS(150); } } }
break;
case 4: //南北向黄灯闪烁 5 次 DelayMS(300); YELLOW_B=~YELLOW_B;GREEN_B=1;
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
《单片机C说话程序设计实训100例—基于8051+Proteus仿真》案例第 01 篇基本程序设计01 闪耀的LED/* 名称:闪耀的LED解释:LED按设定的时光距离闪耀*/#include<reg51.h>#define uchar unsignedchar#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯解释:接在P0口的8个LED从左到右轮回依次点亮,产生走马灯后果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x)uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左轮回移动DelayMS(150);}}03 8只LED阁下往返点亮/* 名称:8只LED阁下往返点亮解释:程序运用轮回移位函数_crol_和_cror_形成往返滚动的后果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1); //P2的值向左轮回移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右轮回移动DelayMS(150);}}}04 名堂流水灯/* 名称:名堂流水灯解释:16只LED分两组按预设的多种名堂变换显示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar code Pattern_P0[]={0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0 xff,0xff,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0 xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0 x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0 x0f,0xf0,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0 xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0 xfb,0xfd,0xfe,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0 x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0 xf8,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,0 x9f,0x3f,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0 xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0 x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0 x0f,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0 xdf,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0 xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0 xc0,0x80,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0 xff,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);}}}05 LED模仿交通灯/* 名称:LED模仿交通灯解释:器械向绿灯亮若干秒,黄灯闪耀5次后红灯亮, 红灯亮后,南北向由红灯变成绿灯,若干秒后南北向黄灯闪耀5此后变红灯,器械向变绿灯,如斯反复.*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit RED_A=P0^0; //器械向灯sbit YELLOW_A=P0^1;sbit GREEN_A=P0^2;sbit RED_B=P0^3; //南北向灯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();}06 单只数码管轮回显示0~9/* 名称:单只数码管轮回显示0~9解释:主程序中的轮回语句反复将0~9的段码送至P0口,使数字0~9轮回显示*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff };//延时void DelayMS(uint x){uchar t;while(x--)for(t=0;t<120;t++);}//主程序void main(){uchar i=0;P0=0x00;while(1){ /* for(;i<11;i++){ P0=~DSY_CODE[i]; DelayMS(300);} //注:另一计划 */P0=~DSY_CODE[i];i=(i+1)%10;DelayMS(300);}}07 8只数码管滚动显示单个数字/* 名称:8只数码管滚动显示单个数字解释:数码管从左到右依次滚动显示0~7,程序经由过程每次仅轮回选通一只数码管*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar 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);}}}08 8只数码管动态显示多个不合字符电路如上图/* 名称:8只数码管动态显示多个不合字符解释:数码管动态扫描显示0~7.*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar 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;P0=DSY_CODE[i]; //发送段码wei=_crol_(wei,1);P2=wei; //发送位码DelayMS(2);}}}09 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);}}10 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)%11]];m=_crol_(m,1);P2=m; //发送位码DelayMS(2);}}k=(k+1)%11; //环形队列首支针k递增,Num下标规模0~10,故对11取余}}11 K1-K4 掌握LED移位/* 名称:K1-K4 掌握LED移位解释:按下K1时,P0口LED上移一位;按下K2时,P0口LED下移一位;按下K3时,P2口LED上移一位;按下K4时,P2口LED下移一位; */#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--) for(i=0;i<120;i++);}//依据P1口的按键移动LEDvoid Move_LED(){if ((P1&0x10)==0) P0=_cror_(P0,1); //K1else if((P1&0x20)==0) P0=_crol_(P0,1); //K2else if((P1&0x40)==0) P2=_cror_(P2,1); //K3else if((P1&0x80)==0) P2=_crol_(P2,1); //K4 }//主程序void main(){uchar Recent_Key; //比来按键P0=0xfe;P2=0xfe;P1=0xff;Recent_Key=0xff;while(1){if(Recent_Key!=P1){Recent_Key=P1; //保管比来按键Move_LED();DelayMS(10);}}}12 K1-K4 按键状况显示/* 名称:K1-K4 按键状况显示解释:K1.K2按下时LED点亮,松开时熄灭,K3.K4按下并释放时LED点亮,再次按下并释放时熄灭; */#include<reg51.h>#define ucharunsigned char#define uintunsigned intsbit LED1=P0^0;sbit LED2=P0^1;sbit LED3=P0^2;sbit LED4=P0^3;sbit K1=P1^0;sbit K2=P1^1;sbit K3=P1^2;sbit K4=P1^3;//延时void DelayMS(uint x){uchar i;while(x--) for(i=0;i<120;i++);}//主程序void main(){P0=0xff;P1=0xff;while(1){LED1=K1;LED2=K2;if(K3==0){while(K3==0);LED3=~LED3;}if(K4==0){while(K4==0);LED4=~LED4;}DelayMS(10);}}13 K1-K4 分组掌握LED/* 名称:K1-K4 分组掌握LED解释:每次按下K1时递增点亮一只LED,全亮时再次按下则再次轮回开端,K2按下后点亮上面4只LED,K3按下后点亮下面4只LED,K4按下后封闭所有LED*/#include<reg51.h>#define ucharunsigned char#define uintunsigned int//延时void DelayMS(uint x){uchar i;while(x--) for(i=0;i<120;i++);}//主程序void main(){uchar k,t,Key_State;P0=0xff;P1=0xff;while(1){t=P1;if(t!=0xff){DelayMS(10);if(t!=P1) continue;//取得4位按键值,由模式XXXX1111(X中有一位为0,其他均为1)//变成模式0000XXXX(X中有一位为1,其他均为0)Key_State=~t>>4;k=0;//检讨1地点地位,累加获取按键号kwhile(Key_State!=0){k++;Key_State>>=1;}//依据按键号k进行4种处理switch(k){case 1: if(P0==0x00) P0=0xff;P0<<=1;DelayMS(200);break;case 2: P0=0xf0;break;case 3: P0=0x0f;break;case 4: P0=0xff;}}}}14 K1-K4 掌握数码管移位显示/* 名称:K1-K4 掌握数码管移位显示解释:按下K1时加1计数并增长显示位,按下K2时减1计数并削减显示位,按下K3时清零.*/#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,0xff };//位码uchar code DSY_Index[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //待显示到各数码管的数字缓冲(开端仅在0位显示0,其他黑屏)uchar Display_Buffer[]={0,10,10,10,10,10,10,10};//延时void DelayMS(uint x){uchar i;while(x--)for(i=0;i<120;i++);}void Show_Count_ON_DSY(){uchar i;for(i=0;i<8;i++){P0=0xff;P0=DSY_CODE[Display_Buffer[i]];P2=DSY_Index[i];DelayMS(2);}}//主程序void main(){uchar i,Key_NO,Key_Counts=0;P0=0xff;P1=0xff;P2=0x00;while(1){Show_Count_ON_DSY();P1=0xff;Key_NO=P1;//P1口按键状况分离为K1-0xfe,K2-0xfd,K3-0xfbswitch(Key_NO){case 0xfe: Key_Counts++;if(Key_Counts>8) Key_Counts=8;Display_Buffer[Key_Counts-1]=Key_Counts;break;case 0xfd: if(Key_Counts>0)Display_Buffer[--Key_Counts]=10;break;case 0xfb: Display_Buffer[0]=0;for(i=1;i<8;i++)Display_Buffer[i]=10;Key_Counts=0;}//若键未释放则仅刷新显示,不进行键扫描while(P1!=0xff) Show_Count_ON_DSY();}}15 K1-K4 掌握数码管加减演示/* 名称:K1-K4 掌握数码管加减演示解释:按下K1后加1计数,按下K2后减1计数,按下K3后清零.*/#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 };//待显示的3位缓冲uchar Num_Buffer[]={0,0,0};//按键代码,按键计数uchar Key_Code,Key_Counts=0;//延时void DelayMS(uint x){uchar i;while(x--) for(i=0;i<120;i++); }//显示函数void Show_Counts_ON_DSY(){uchar i,j=0x01;Num_Buffer[2]=Key_Counts/100;Num_Buffer[1]=Key_Counts/10%10;Num_Buffer[0]=Key_Counts%10;for(i=0;i<3;i++){j=_cror_(j,1);P0=0xff;P0=DSY_CODE[Num_Buffer[i]];P2=j;DelayMS(1);}}//主程序void main(){uchar i;P0=0xff;P1=0xff;P2=0x00;Key_Code=0xff;while(1){Show_Counts_ON_DSY();P1=0xff;Key_Code=P1;//有键按下时,数码管刷新显示30次,该行代码同时起到延时感化if(Key_Code!=0xff)for(i=0;i<30;i++) Show_Counts_ON_DSY();switch(Key_Code){case 0xfe: if(Key_Counts<255) Key_Counts++;break;case 0xfd: if(Key_Counts>0) Key_Counts--;break;case 0xfb: Key_Counts=0;}Key_Code=0xff;}}16 4X4矩阵键盘掌握条形LED显示/* 名称:4X4矩阵键盘掌握条形LED显示解释:运行本例时,按下的按键值越大点亮的LED越多.*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//矩阵键盘按键特点码表uchar code KeyCodeTable[]={0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88}; //延时void DelayMS(uint x){uchar i;while(x--) for(i=0;i<120;i++);}//键盘扫描uchar Keys_Scan(){uchar sCode,kCode,i,k;//低4地位0,放入4行P1=0xf0;//若高4位消失0,则有键按下if((P1&0xf0)!=0xf0){DelayMS(2);if((P1&0xf0)!=0xf0){sCode=0xfe; //行扫描码初值for(k=0;k<4;k++) //对4行分离进行扫描{P1=sCode;if((P1&0xf0)!=0xf0){kCode=~P1;for(i=0;i<16;i++) //查表得到按键序号并返回if(kCode==KeyCodeTable[i])return(i);}elsesCode=_crol_(sCode,1);}}}return(-1);}//主程序void main(){uchar i,P2_LED,P3_LED;uchar KeyNo=-1; //按键序号,-1暗示无按键while(1){KeyNo=Keys_Scan(); //扫描键盘获取按键序号KeyNoif(KeyNo!=-1){P2_LED=0xff;P3_LED=0xff;for(i=0;i<=KeyNo;i++) //键值越大,点亮的LED越多{if(i<8)P3_LED>>=1;elseP2_LED>>=1;}P3=P3_LED; //点亮条形LEDP2=P2_LED;}}}17 数码管显示4X4矩阵键盘按键号/* 名称:数码管显示4X4矩阵键盘按键号解释:按下随意率性键时,数码管都邑显示其键的序号,扫描程序起首断定按键产生在哪一列,然后依据所产生的行附加不合的值,从而得到按键的序号.*/#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,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00};sbit BEEP=P3^7;//前次按键和当前按键的序号,该矩阵中序号规模0~15,16暗示无按键uchar Pre_KeyNo=16,KeyNo=16;//延时void DelayMS(uint x){uchar i;while(x--) for(i=0;i<120;i++);}//矩阵键盘扫描void Keys_Scan(){uchar Tmp;P1=0x0f; //高4地位0,放入4行DelayMS(1);Tmp=P1^0x0f;//按键后0f变成0000XXXX,X中一个为0,3个仍为1,经由过程异或把3个1变成0,独一的0变成1switch(Tmp) //断定按键产生于0~3列的哪一列{case 1: KeyNo=0;break;case 2: KeyNo=1;break;case 4: KeyNo=2;break;case 8: KeyNo=3;break;default:KeyNo=16; //无键按下}P1=0xf0; //低4地位0,放入4列DelayMS(1);Tmp=P1>>4^0x0f;//按键后f0变成XXXX0000,X中有1个为0,三个仍为1;高4位转移到低4位并异或得到转变的值switch(Tmp) //对0~3行分离附加肇端值0,4,8,12{case 1: KeyNo+=0;break;case 2: KeyNo+=4;break;case 4: KeyNo+=8;break;case 8: KeyNo+=12;}}//蜂鸣器void Beep(){uchar i;for(i=0;i<100;i++){DelayMS(1);BEEP=~BEEP;}BEEP=0;}//主程序void main(){P0=0x00;BEEP=0;while(1){P1=0xf0;if(P1!=0xf0) Keys_Scan(); //获取键序号if(Pre_KeyNo!=KeyNo){P0=~DSY_CODE[KeyNo];Beep();Pre_KeyNo=KeyNo;}DelayMS(100);}}18 开关掌握LED/* 名称:开关掌握LED解释:开关S1和S2分离掌握LED1和LED2. */#include<reg51.h>sbit S1=P1^0;sbit S2=P1^1;sbit LED1=P0^0;sbit LED2=P0^1;//主程序void main(){while(1){LED1=S1;LED2=S2;}}19 继电器掌握照明装备/* 名称:继电器掌握照明装备解释:按下K1灯点亮,再次按下时灯熄灭*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit K1=P1^0;sbit RELAY=P2^4;//延时void DelayMS(uint ms){uchar t;while(ms--)for(t=0;t<120;t++);}//主程序void main(){P1=0xff;RELAY=1;while(1){if(K1==0){while(K1==0);RELAY=~RELAY;DelayMS(20);}}}20 数码管显示拨码开关编码/* 名称:数码管显示拨码开关编码解释:体系显示拨码开关所设置的编码000~255*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//各数字的数码管段码(共阴)uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示缓冲uchar DSY_Buffer[3]={0,0,0};//延时void DelayMS(uint ms){uchar t;while(ms--)for(t=0;t<120;t++);}//主程序void main(){uchar i,m,Num;P0=0xff;P2=0xff;while(1){m=0xfe;Num=P1; //读取拨码开关的值DSY_Buffer[0]=Num/100;DSY_Buffer[1]=Num/10%10;DSY_Buffer[2]=Num%10;for(i=0;i<3;i++)//刷新显示在数码管上{m=_crol_(m,1);P2=m;P0=DSY_CODE[DSY_Buffer[i]];DelayMS(10);}}}21 开关掌握报警器/* 名称:开关掌握报警器解释:用K1开关掌握报警器,程序掌握P1.0输出两种不合频率的声音,模仿很传神的报警后果*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit SPK=P1^0;sbit K1=P1^7;//发声函数void Alarm(uchar t){uchar i,j;for(i=0;i<200;i++){SPK=~SPK;for(j=0;j<t;j++); //由参数t行成不合的频率}}void main(){SPK=0;while(1){if(K1==1){Alarm(90);Alarm(120);}}}22 按键发音/* 名称:按键发音解释:按下不合的按键会是SOUNDER发出不合频率的声音.本例运用延时函数实现不合频率的声音输出,今后也可运用准时器*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3^7;sbit K1=P1^4;sbit K2=P1^5;sbit K3=P1^6;sbit K4=P1^7;//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++); }//按周期t发音void Play(uchar t){uchar i;for(i=0;i<100;i++){BEEP=~BEEP;DelayMS(t);}BEEP=0;}void main(){P1=0xff;BEEP=0;while(1){if(K1==0)Play(1);if(K2==0) Play(2);if(K3==0) Play(3);if(K4==0) Play(4);}}23 播放音乐/* 名称:播放音乐解释:程序运行时播放诞辰快活歌, 未运用准时器中止,所有频率完整用延时实现*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3^7;//诞辰快活歌的音符频率表,不合频率由不合的延时来决议uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,159,169,190,119,119,126,159,142,159,0};//诞辰快活歌节奏表,节奏决议每个音符的吹奏长短uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}//播放函数void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){BEEP=~BEEP;//SONG_TONE延时表决议了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){BEEP=0;while(1){PlayMusic(); //播放诞辰快活DelayMS(500); //播放完后暂停一段时光}}24 INT0中止计数/* 名称:INT0中止计数解释:每次按下计数键时触发INT0中止,中止程序累加计数, 计数值显示在3只数码管上,按下清零键时数码管清零*/#include<reg51.h>#define uchar unsigned char#define uint unsigned int//0~9的段码uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00 };//计数值分化后各个待显示的数位uchar DSY_Buffer[]={0,0,0};uchar Count=0;sbit Clear_Key=P3^6;//数码管上显示计数值void Show_Count_ON_DSY(){DSY_Buffer[2]=Count/100; //获取3个数DSY_Buffer[1]=Count%100/10;DSY_Buffer[0]=Count%10;if(DSY_Buffer[2]==0) //高位为0时不显示{DSY_Buffer[2]=0x0a;if(DSY_Buffer[1]==0) //高位为0,若第二位为0同样不显示DSY_Buffer[1]=0x0a;}P0=DSY_CODE[DSY_Buffer[0]];P1=DSY_CODE[DSY_Buffer[1]];P2=DSY_CODE[DSY_Buffer[2]];}//主程序void main(){P0=0x00;P1=0x00;P2=0x00;IE=0x81; //许可INT0中止IT0=1; //降低沿触发while(1){if(Clear_Key==0) Count=0; //清0Show_Count_ON_DSY();}}//INT0中止函数void EX_INT0() interrupt{Count++; //计数值递增}。
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intvoid DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){for(i=0;i<7;i++)P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar 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);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/*名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h〉#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x——){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/*名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51。
h>#include〈intrins。
h〉#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x—-){for(i=0;i〈120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1);//P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/*名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include〈reg51.h>#include〈intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i〈120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1);//P2的值向左循环移动DelayMS(150);}for(i=0;i〈7;i++){P2=_cror_(P2,1);//P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/*名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include〈reg51。
单片机C语言程序设计方案实训例— Proteus仿真
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(>。
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,
#define uchar unsigned char
#define uint unsigned int
//延时
void DelayMS(uint x>
{
uchar i。
while(x-->
{
for(i=0。i<120。i++>。
}
}
//主程序
void main(>
{
单片机实验及proteus仿真_C语言篇
单片机实验:说明,以下是我整理了一些在学习单片机的过程中所做的一些典型的实验,所有程序均用C 语言编写,为了方便,在仿真时电路能省的都被省掉了(比如省去晶振用了外部时钟)。
程序不一定很优化,也不一定完全没有错误,仅供大家参考。
希望大家一块学习!!!!第一:I/O操作篇实验一.闪烁的灯图:程序:#include "AT89X51.H"void delay02s();void main(void){while(1){P1_0=0; //P1.0=0 led点亮delay02s(); //延时0.2sP1_0=1; //P1.0=1 led熄灭delay02s(); //延时0.2s}}void delay02s(void) //延时0.2s的函数{unsigned char i,j,k;for(j=20;j>0;j--)for(i=20;i=0;i--)for(k=248;k=0;k--);}实验二:模拟开关程序:#include "AT89X51.H"void main(){while(1){if (P1_7==0) P1_0 = 0;//key按下P1_7=0,led点亮else if ( P1_7==1) P1_0 = 1;//key弹起P1_7=1,led熄灭}}实验三: 多路开关程序:#include "AT89X51.H"void main(){while(1){P1_0 = P1_7;P1_1 = P1_6;P1_2 = P1_5;P1_3 = P1_4;}}实验四:跑马灯(一)图:程序://单一灯的左移右移//八个发光二极管L1-L8分别接在单片机的P1.0-P1.7接口上//输出“0”时,发光二极管亮,开始时P1.0→P1.1→P1.2→P1.3→┅→P1.7→P1.6→┅→P1.0亮,重复循环。
#include "AT89X51.H"void delay02s();void main(){unsigned int a;while (1){P1=0xfe;delay02s();for(a=7;a>0;a--)//右移{P1=(P1<<1)+1;delay02s();}for(a=6;a>0;a--)//左移{P1=(P1>>1)+0x80;delay02s();}}}void delay02s(){unsigned int i,j,k;for(i=0;i<20;i++)for(j=0;j<20;j++)for(k=0;k<248;k++);}实验五:跑马灯(二、查表方式) 图:程序:#include "AT89X51.H"void delay02s();void main(){unsigned char table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f, //第一次,右移一行0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f, //第二次,右移一行0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe, //第一次,左移一行0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe, //第二次,左移一行0x00,0xff,0x00,0xff}; //闪烁两次unsigned char a;while(1){ for (a=0;a<35;a++){P1=table[a];delay02s();}}}void delay02s(){unsigned char i,j,k;for(i=20;i>0;i--)for(j=20;j>0;j--)for(k=248;k>0;k--);}实验六:led计数程序:#include <AT89X51.H>void delay02s();void main(){unsigned char code table[]={0x40,0xf9,0x24,0x30,0x19,0x12,0x03,0xf8,0x00,0x18}; unsigned char disp;while(1){for(disp=0;disp<10;disp++){P0=table[disp];delay02s();}}}void delay02s(){ unsigned char i,j,k;for(i=20;i>0;i--)for(j=20;j>0;j--)for(k=248;k>0;k--);}实验七:二进制计数程序://用四个发光二极管模拟计数点击开关次数. #include "AT89X51.H"void delay10ms();void main(){P0=0xff;while(1){/*//////////////////////////////////////////////if(P0_7==1) //按键按下{delay10ms();if(P0_7==0) //按键弹起,此时灯亮.P0=P0-1;}//////////////////////////////////////////////*///////////////////////////////////////////////if(P0_7==0) //按键弹起{delay10ms();if(P0_7==1) //按键按下,此时灯亮.P0=P0-1;}//////////////////////////////////////////////}}void delay10ms(){ unsigned char i,j;for(i=20;i>0;i--)for(j=248;j>0;j--);}验八:一键多用程序:#include "AT89X51.H"void delay10ms();void delay02s();void main(){unsigned char id=0;while(1){ if(P1_7==0) //按键弹起{delay10ms();if(P1_7==0){id++;if(id==4) id=0;while(P1_7==0);}}switch(id){ case 0:P0_0=~P0_0;delay02s();break;case 1:P0_1=~P0_1;delay02s();break;case 2:P0_2=~P0_2;delay02s();break;case 3:P0_3=~P0_3;delay02s();break;}}}void delay10ms() {unsigned char i,k;for(i=20;i>0;i--)for(k=248;k>0;k--);}void delay02s() {unsigned char j;for(j=20;j>0;j--)delay10ms();}实验九:00~99计数程序://00~99计数器#include "AT89X51.H"void delay1s();void main(){unsigned char code table[]={0xf9,0x24,0x30,0x19,0x12,0x03,0xf8,0x00,0x18,0x40}; unsigned int high=9;unsigned int light=9;P0=table[light];P2=table[high];delay1s();while(1){for(light=0;light<10;light++){if(light==9) high++;if(high==10) high=0;P0=table[light];P2=table[high];delay1s();}}}void delay1s(){unsigned char i,j,k;for(i=82;i>0;i--)for(j=20;j>0;j--)for(k=248;k>0;k--);}实验十:可预置数加减计数器程序://K1(P3_7)加计数.K2(P3_6)头减计数. #include "AT89X51.H"void delay02s();void delay10ms();void main(){P0=P2;while(1){if(P3_6==0){delay10ms();if(P3_6==0){P0++;delay02s();}}if(P3_7==0){delay10ms();if(P3_7==0){P0--;delay02s();}}}}void delay02s() {unsigned char j;for(j=20;j>0;j--) delay10ms();}void delay10ms() {unsigned char i,k; for(i=20;i>0;i--)for(k=248;k>0;k--); }实验十一:图:共阴LED程序://开关按下时显示"12345",开关弹起时显示"HELLO".#include <AT89X51.H>void delay01ms();void main(void){unsigned char code table1[]={0xf9,0x24,0x30,0x19,0x12,0xff,0xff,0xff}; unsigned char code table2[]={0x09,0x06,0xc7,0xc7,0x40,0xff,0xff,0xff}; unsigned char i;while(1){P2=0xfe;if(P1_0==0){delay01ms();if(P1_0==0)for(i=0;i<8;i++){P0=table1[i];P2=(P2<<1)+1;}}else if(P1_0==1){delay01ms();if(P1_0==1)for(i=0;i<8;i++){P0=table2[i];P2=(P2<<1)+1;}}}}void delay01ms() {unsigned char a,b;for(a=4;a>0;a--)for(b=248;b>0;b--);}实验十二、4*4键盘图:程序:#include "AT89X51.H"void delay10ms();void main(){unsigned char key;unsigned char table[]={0x40,0xf9,0x24,0x30,0x19,0x12,0x03,0xf8,0x00,0x18,0x08,0x00,0x46,0x40,0x06,0x0e, 0xff}; while(1){P0=0xff;P3=0xff;P3_4=0;key=P3&0x0f;if(key!=0x0f){delay10ms();key=P3&0x0f;if(key!=0x0f){switch(key){case 0x0e: key=0;break;case 0x0d: key=1;break;case 0x0b: key=2;break;case 0x07: key=3;break;}}P0=table[key];key=key&0x0f;while(key!=0x0f){key=P3;key=key&0x0f;}}P3=0xff;P3_5=0;key=P3&0x0f;if(key!=0x0f){delay10ms();key=P3&0x0f;if(key!=0x0f){switch(key){case 0x0e: key=4;break;case 0x0b: key=6;break;case 0x07: key=7;break;}}P0=table[key];key=key&0x0f;while(key!=0x0f){key=P3;key=key&0x0f;}}P3=0xff;P3_6=0;key=P3&0x0f;if(key!=0x0f){delay10ms();key=P3&0x0f;if(key!=0x0f){switch(key){case 0x0e: key=8;break;case 0x0d: key=9;break;case 0x0b: key=10;break;case 0x07: key=11;break;}}P0=table[key];key=key&0x0f;while(key!=0x0f){key=P3;key=key&0x0f;}}P3=0xff;P3_7=0;key=P3&0x0f;if(key!=0x0f){delay10ms();key=P3&0x0f;if(key!=0x0f){switch(key)case 0x0d: key=13;break;case 0x0b: key=14;break;case 0x07: key=15;break;}}P0=table[key];key=key&0x0f;while(key!=0x0f){key=P3;key=key&0x0f;}}}}void delay10ms(){unsigned int i,k;for(i=20;i>0;i--)for(k=20;k>0;k--);}实验十三lcd原理图:程序:#include "AT89X52.H"#define lcd_rs P3_2#define lcd_rw P3_1#define lcd_en P3_0#define lcd_data P1void writecmd(int a) //写控制字{int i;for(i=2000;i>0;i--);lcd_rs=0;lcd_rw=0;lcd_en=1;lcd_data=a;lcd_en=0;}void writedata(char lcddata) //显示字符{int i;for(i=2000;i>0;i--);lcd_rs=1;lcd_rw=0;lcd_en=1;lcd_data=lcddata;lcd_en=0;}void writestring(char addr,char *str) //显示字符串{writecmd(addr);do{ writedata(*str);str++;}while(*str!='\0');}//=====lcd初始化================================void lcdinit(){writecmd(0x38);//8位,两行,5X7字型(控制字"001 d1 d2 d3 x x" , 其中d1:数据长度选择(8位或4位),d2:单双列选择,d3:5X10或5X7字型选择)writecmd(0x08); //关闭显示屏writecmd(0x01); //清除显示屏writecmd(0x0c); //开启显示屏,游标,与闪烁(控制字"00001 d1 d2 d3",其中d1:屏幕开/关, d2:光标开/关, d3:闪烁开/关)writecmd(0x06); //地址递增(AC+1)}//==============================================void main(){char disp1[]={" QQ:393725829 "};char disp2[]={" "};lcdinit();while(1){writestring(0x80,disp1);writestring(0xc0,disp2);}}。
(完整版)单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar 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);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例目录第一篇基础程序设计 (1)01 闪烁的LED (1)02 从左到右的流水灯 (1)03 8只LED左右来回点亮 (2)04 花样流水灯 (3)05 LED模拟交通灯 (4)06 单只数码管循环显示0~9 (5)07 8只数码管滚动显示单个数字 (6)08 8只数码管动态显示多个不同字符 (7)09 8只数码管闪烁显示数字串 (8)10 8只数码管滚动显示数字串 (9)11 K1-K4 控制LED移位 (10)12 K1-K4 按键状态显示 (11)13 K1-K4 分组控制LED (12)14 K1-K4 控制数码管移位显示 (13)15 K1-K4 控制数码管加减演示 (14)16 4X4矩阵键盘控制条形LED显示 (16)17 数码管显示4X4矩阵键盘按键号 (17)18 开关控制LED (19)19 继电器控制照明设备 (20)20 数码管显示拨码开关编码 (21)21 开关控制报警器 (22)22 按键发音 (22)23 播放音乐 (23)24 INT0中断计数 (24)25 外部INT0中断控制LED (26)26 INT0及INT1中断计数 (26)27 定时器控制单只LED (28)28 TIMER0控制流水灯 (29)29 定时器控制4个LED滚动闪烁 (30)30 T0控制LED实现二进制计数 (31)31 TIMER0与TIMER1控制条形LED (31)。
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){{P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar 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);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
(完整版)PROTEUS仿真100实例
(完整版)PROTEUS仿真100实例《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include#include#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include#include#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include#define uchar unsigned char#define uint unsigned intuchar code Pattern_P0[]={0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0 xff,0xff,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x 00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0 x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xf f,0x0f,0xf0,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0 xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xf b,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,0 xf0,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,0x 9f,0x3f,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x 00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0 x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xf f,0x0f,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xd f,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0 xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc 0,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);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;.\r\n");Puts_to_SerialPort("-------------------------------\r\n");DelayMS(50);while(1){Putc_to_SerialPort(c+'A');DelayMS(100);Putc_to_SerialPort(' ');DelayMS(100);if(c==25) With PCB layout now offering automation of both component ", "can often be the most time consuming element of the exercise.","And if you use circuit simulation to develop your ideas, ","you are going to spend even more time working on the schematic."};//显示缓冲(2行)uchar Disp_Buffer[32];//垂直滚动显示void V_Scroll_Display(){uchar i,j,k=0;uchar *p=Msg[0];uchar *q=Msg[Line_Count]+strlen(Msg[Line_Count]); //以下仅使用显示缓冲的前16字节空间while(p<q)< p="">{for(i=0;i<16&&p<q;i++)< p="">{ //消除显示缓冲中待显示行首尾可能出现的空格if((i==0||i==15)&&*p==' ') p++;if(*p!='\0'){}else{if(++k>Line_Count) break;p=Msg[k]; //p指向下一串的首地址Disp_Buffer[i]=*p++;}}//不足16个字符时空格补充for(j=i;j<16;j++) Disp_Buffer[j]=' ';//垂直滚动显示while(F0) DelayMS(5);ShowString(0,0," ");DelayMS(150);while(F0) DelayMS(5);ShowString(0,1,Disp_Buffer);DelayMS(150);while(F0) DelayMS(5);ShowString(0,0,Disp_Buffer);ShowString(0,1," ");DelayMS(150);}//最后清屏ShowString(0,0," ");ShowString(0,1," ");}//水平滚动显示void H_Scroll_Display(){uchar i,j,k=0,L=0;uchar *p=Msg[0];uchar *q=Msg[Line_Count]+strlen(Msg[Line_Count]); //将32个字符的显示缓冲前16个字符设为空格for(i=0;i<16;i++) Disp_Buffer[i]=' ';while(p<q)< p="">{//忽略缓冲中首尾可能出现的空格if((i==16||i==31)&&*p==' ') p++;for(i=16;i<32&&p<q;i++)< p="">{if(*p!='\0'){Disp_Buffer[i]=*p++;}elseif(++k>Line_Count) break;p=Msg[k]; //p指向下一串的首地址Disp_Buffer[i]=*p++;}}//不足32个字符时空格补充for(j=i;j<32;j++) Disp_Buffer[j]=' ';//水平滚动显示for(i=0;i<=16;i++){while(F0) DelayMS(5);ShowString(0,L,Disp_Buffer+i);while(F0) DelayMS(5);DelayMS(20);}L=(L==0)1:0; //行号在0,1间交替DelayMS(300);}//如果显示结束时停留在第0行,则清除第1行的内容if(L==1) ShowString(0,1," ");}//外部中断0,由K3控制暂停与继续显示void EX_INT0() interrupt 0{F0=!F0; //暂停与继续显示控制标志位}//主程序void main(){uint Count=0;IE=0x81; //允许外部中断0IT0=1; //下降沿触发F0=0; //暂停与继续显示控制标志位Initialize_LCD();ShowString(0,0,Prompt);ShowString(0,1,Prompt+16);while(1){if(K1==0){V_Scroll_Display();DelayMS(300);}elseif(K2==0)H_Scroll_Display();DelayMS(300);}}}///* 名称:液晶控制与显示程序说明:本程序是通用的1602液晶控制程序。
单片机C语言程序设计实训100例—基于8051+Proteus仿真
《基于8051+P r o t e u s仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){uchar i;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar 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++);}}//主程序{uchar i;while(1){ //从数组中读取数据送至P0和P2口显示for(i=0;i<136;i++){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(100);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0;//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED 从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char #define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main(){P0=0xfe;while(1){P0=_crol_(P0,1); //P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x){uchar i;while(x--){for(i=0;i<120;i++);}}//主程序void main()uchar i;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1); //P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1); //P2的值向右循环移动DelayMS(150);}}}04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar 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);}}}05 LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计方案实训例— Proteus仿真
《基于8051+Proteus仿真》案例第 01 篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P1^0。
//延时void DelayMS(uint x>{uchar i。
while(x-->{for(i=0。
i<120。
i++>。
}}//主程序void main(>{while(1>{LED=~LED。
DelayMS(150>。
}}02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x>{uchar i。
while(x-->{for(i=0。
i<120。
i++>。
}}//主程序void main(>{P0=0xfe。
while(1>{P0=_crol_(P0,1>。
//P0的值向左循环移动DelayMS(150>。
}}03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x>{uchar i。
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1 (2)
《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#defineucharunsignedchar#defineuintunsignedintsbitLED=P1^0;//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i<120;i++);}}//主程序voidmain(){while(1){LED=~LED;DelayMS(150);}}02从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i<120;i++);}}//主程序voidmain(){P0=0xfe;while(1){P0=_crol_(P0,1);//P0的值向左循环移动DelayMS(150);}}038只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i<120;i++);}}//主程序voidmain(){uchari;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1);//P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1);//P2的值向右循环移动DelayMS(150);}}}04花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.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;i<120;i++);}}//主程序voidmain(){uchari;while(1){ //从数组中读取数据送至P0和P2口显示for(i=0;i<136;i++){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(100);}}}05LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1 (2)
《单片机C语言程序设计实训100例—基于8051+P r o t e u s仿真》案例第01篇基础程序设计01 闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#defineucharunsignedchar#defineuintunsignedintsbitLED=P1^0;//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i<120;i++);}}//主程序voidmain(){while(1){LED=~LED;DelayMS(150);}}02从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i<120;i++);}}//主程序voidmain(){P0=0xfe;while(1){P0=_crol_(P0,1);//P0的值向左循环移动DelayMS(150);}}038只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/#include<reg51.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedint//延时voidDelayMS(uintx){uchari;while(x--){for(i=0;i<120;i++);}}//主程序voidmain(){uchari;P2=0x01;while(1){for(i=0;i<7;i++){P2=_crol_(P2,1);//P2的值向左循环移动DelayMS(150);}for(i=0;i<7;i++){P2=_cror_(P2,1);//P2的值向右循环移动DelayMS(150);}}04花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.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;i<120;i++);}}//主程序{uchari;while(1){ //从数组中读取数据送至P0和P2口显示for(i=0;i<136;i++){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(100);}}}05LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
单片机C语言程序设计实训100例——基于8051+Proteus仿真01 闪烁的LED/*名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit LED=P10。
//延时void DelayMS(uint x>{uchar i。
while(x-->{for(i=0。
i<120。
i++>。
}}//主程序void main(>{while(1>{LED=~LED。
DelayMS(150>。
}}02从左到右的流水灯/*名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x>{uchar i。
while(x-->{for(i=0。
i<120。
i++>。
}}//主程序void main(>{P0=0xfe。
while(1>{P0=_crol_(P0,1>。
//P0的值向左循环移动DelayMS(150>。
}}038只LED左右来回点亮/*名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*/ #include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//延时void DelayMS(uint x>{uchar i。
while(x-->{for(i=0。
i<120。
i++>。
}}//主程序void main(>{uchar i。
P2=0x01。
while(1>{for(i=0。
i<7。
i++>{P2=_crol_(P2,1>。
//P2的值向左循环移动DelayMS(150>。
}for(i=0。
i<7。
i++>{P2=_cror_(P2,1>。
//P2的值向右循环移动DelayMS(150>。
}}}04花样流水灯/*名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar 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>。
}}}05LED模拟交通灯/* 名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。
*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit RED_A=P00。
//东西向灯sbit YELLOW_A=P01。
sbit GREEN_A=P02。
sbit RED_B=P03。
//南北向灯sbit YELLOW_B=P04。
sbit GREEN_B=P05。
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(>。
}06单只数码管循环显示0~9/* 名称:单只数码管循环显示0~9说明:主程序中的循环语句反复将0~9的段码送至P0口,使数字0~9循环显示*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}。
//延时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>%10。
DelayMS(300>。
}}078只数码管滚动显示单个数字/* 名称:8只数码管滚动显示单个数字说明:数码管从左到右依次滚动显示0~7,程序通过每次仅循环选通一只数码管*/ #include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}。