单片机C语言程序设计实训100例

合集下载

单片机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)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例

(整理)单片机C语言程序设计实训100例

《单片机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此后变红灯,东西向变绿灯,如此重复。

(完整word版)单片机C语言程序设计实训100例

(完整word版)单片机C语言程序设计实训100例

《单片机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语言程序设计实训100例

单片机C语言程序设计实训100例

//恢复定时器 1 初值 //500ms 转换状态
名称:10s 的秒表 说明:首次按键计时开始,再次按键暂停,第三次按键清零。
*/ #include<reg51.h> #define uchar unsigned char #define uint unsigned int sbit K1=P3^7; uchar i,Second_Counts,Key_Flag_Idx; bit Key_State; uchar
34 上海师范大学信息与机电工程学院—倪继锋
《单片机 C 语言程序设计实训 100 例---基于 8051 和 PROTEUS 仿真》案例
34 /*
100 000s 以内的计时程序 名称:100 000s 以内的计时程序 说明:在 6 只数码管上完成 0~99 999.9s。
*/ #include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int //段码 ucha 中断函数 void LED_Flash_and_Scroll() interrupt 1 { if(++k<35) return; //定时中断若干次后执行闪烁 k=0; switch(i) { case 0: B1=~B1;break; case 1: G1=~G1;break; case 2: R1=~R1;break; case 3: Y1=~Y1;break; default:i=0; } if(++j<300) return; //每次闪烁持续一段时间 j=0; P0=0xff; //关闭显示 i++; //切换到下一个 LED } 30 /* T0 控制 LED 实现二进制计数

《单片机C语言程序设计实训100例—基于8051+Proteus仿真

《单片机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#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仿真》案例第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仿真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语言程序设计实训100例--基于某8051+PROTEUS仿真1

单片机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){{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 unsignedchar#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仿真

单片机C语言程序设计实训100例—基于.8051 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 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){{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 unsignedchar#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仿真》

毕业设计_单片机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#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此后变红灯,东西向变绿灯,如此重复。

(完整版)C51单片机C语言程序100例_Keil

(完整版)C51单片机C语言程序100例_Keil

目录目录 (1)函数的使用和熟悉********************************/ (4)实例3:用单片机控制第一个灯亮 (4)实例4:用单片机控制一个灯闪烁:认识单片机的工作频率 (4)实例5:将P1口状态分别送入P0、P2、P3口:认识I/O口的引脚功能 (5)实例6:使用P3口流水点亮8位LED (5)实例7:通过对P3口地址的操作流水点亮8位LED (6)实例8:用不同数据类型控制灯闪烁时间 (7)实例9:用P0口、P1口分别显示加法和减法运算结果 (8)实例10:用P0、P1口显示乘法运算结果 (9)实例11:用P1、P0口显示除法运算结果 (9)实例12:用自增运算控制P0口8位LED流水花样 (10)实例13:用P0口显示逻辑"与"运算结果 (10)实例14:用P0口显示条件运算结果 (11)实例15:用P0口显示按位"异或"运算结果 (11)实例16:用P0显示左移运算结果 (11)实例17:"万能逻辑电路"实验 (11)实例18:用右移运算流水点亮P1口8位LED (12)实例19:用if语句控制P0口8位LED的流水方向 (13)实例20:用swtich语句的控制P0口8位LED的点亮状态 (13)实例21:用for语句控制蜂鸣器鸣笛次数 (14)实例22:用while语句控制LED (16)实例23:用do-while语句控制P0口8位LED流水点亮 (16)实例24:用字符型数组控制P0口8位LED流水点亮 (17)实例25:用P0口显示字符串常量 (18)实例26:用P0口显示指针运算结果 (19)实例27:用指针数组控制P0口8位LED流水点亮 (19)实例28:用数组的指针控制P0口8位LED流水点亮 (20)实例29:用P0、P1口显示整型函数返回值 (21)实例30:用有参函数控制P0口8位LED流水速度 (22)实例31:用数组作函数参数控制流水花样 (23)实例32:用指针作函数参数控制P0口8位LED流水点亮 (23)实例33:用函数型指针控制P1口灯花样 (25)实例34:用指针数组作为函数的参数显示多个字符串 (26)实例35:字符函数ctype.h应用举例 (27)实例36:内部函数intrins.h应用举例 (27)实例37:标准函数stdlib.h应用举例 (28)实例38:字符串函数string.h应用举例 (29)实例39:宏定义应用举例2 (29)1/192实例40:宏定义应用举例2 (30)实例41:宏定义应用举例3 (30)中断、定时器************************************************ (31)实例42:用定时器T0查询方式P2口8位控制LED闪烁 (31)实例43:用定时器T1查询方式控制单片机发出1KHz音频 (31)实例44:将计数器T0计数的结果送P1口8位LED显示 (32)实例45:用定时器T0的中断控制1位LED闪烁 (33)实例46:用定时器T0的中断实现长时间定时 (34)实例47:用定时器T1中断控制两个LED以不同周期闪烁 (34)实例48:用计数器T1的中断控制蜂鸣器发出1KHz音频 (36)实例49:用定时器T0的中断实现"渴望"主题曲的播放 (36)实例50-1:输出50个矩形脉冲 (39)实例50-2:计数器T0统计外部脉冲数 (40)实例51-2:定时器T0的模式2测量正脉冲宽度 (40)实例52:用定时器T0控制输出高低宽度不同的矩形波 (41)实例53:用外中断0的中断方式进行数据采集 (42)实例54-1:输出负脉宽为200微秒的方波 (43)实例54-2:测量负脉冲宽度 (43)实例55:方式0控制流水灯循环点亮 (44)实例56-1:数据发送程序 (45)实例56-2:数据接收程序 (47)实例57-1:数据发送程序 (47)实例57-2:数据接收程序 (49)实例58:单片机向PC发送数据 (50)实例59:单片机接收PC发出的数据 (51)*********************************数码管显示 (52)实例60:用LED数码显示数字5 (52)实例61:用LED数码显示器循环显示数字0~9 (52)实例62:用数码管慢速动态扫描显示数字"1234" (53)实例63:用LED数码显示器伪静态显示数字1234 (54)实例64:用数码管显示动态检测结果 (54)实例65:数码秒表设计 (56)实例66:数码时钟设计 (58)实例67:用LED数码管显示计数器T0的计数值 (62)实例68:静态显示数字“59” (63)*****************************键盘控制2/192*****************************************************/ (63)实例69:无软件消抖的独立式键盘输入实验 (64)实例70:软件消抖的独立式键盘输入实验 (64)实例71:CPU控制的独立式键盘扫描实验 (65)实例72:定时器中断控制的独立式键盘扫描实验 (68)实例73:独立式键盘控制的4级变速流水灯 (71)实例74:独立式键盘的按键功能扩展:"以一当四" (73)实例75:独立式键盘调时的数码时钟实验 (75)实例76:独立式键盘控制步进电机实验 (79)实例77:矩阵式键盘按键值的数码管显示实验 (82)//实例78:矩阵式键盘按键音 (85)实例79:简易电子琴 (86)实例80:矩阵式键盘实现的电子密码锁 (92)**************************************************************************液晶显示LCD*********液晶显示LCD*****液晶显示LCD************************************************************************/ (95)实例81:用LCD显示字符'A' (96)实例82:用LCD循环右移显示"Welcome to China" (99)实例83:用LCD显示适时检测结果 (102)实例84:液晶时钟设计 (106)******************************************一些芯片的使用*****24c02........ DS18B20X5045ADC0832DAC0832DS1302红外遥控**********************************************/ (112)实例85:将数据"0x0f"写入AT24C02再读出送P1口显示 (112)实例86:将按键次数写入AT24C02,再读出并用1602LCD显示 (117)实例87:对I2C总线上挂接多个AT24C02的读写操作 (124)实例88:基于AT24C02的多机通信读取程序 (129)实例88:基于AT24C02的多机通信写入程序 (133)实例90:DS18B20温度检测及其液晶显示 (144)实例91:将数据"0xaa"写入X5045再读出送P1口显示 (153)实例92:将流水灯控制码写入X5045并读出送P1口显示 (157)实例93:对SPI总线上挂接多个X5045的读写操作 (161)实例94:基于ADC0832的数字电压表 (165)实例95:用DAC0832产生锯齿波电压 (171)实例96:用P1口显示红外遥控器的按键值 (171)实例97:用红外遥控器控制继电器 (174)实例98:基于DS1302的日历时钟 (177)实例99:单片机数据发送程序 (185)实例100:电机转速表设计 (186)模拟霍尔脉冲 (192)/********************************************************* ***函数的使用和熟悉***************************************************************///实例3:用单片机控制第一个灯亮#include<reg51.h>//包含51单片机寄存器定义的头文件void main(void){P1=0xfe;//P1=11111110B,即P1.0输出低电平}//4//实例4:用单片机控制一个灯闪烁:认识单片机的工作频率#include<reg51.h>//包含单片机寄存器的头文件/****************************************函数功能:延时一段时间*****************************************/void delay(void)//两个void意思分别为无需返回值,没有参数传递{unsigned int i;//定义无符号整数,最大取值范围65535for(i=0;i<20000;i++)//做20000次空循环;//什么也不做,等待一个机器周期}/*******************************************************函数功能:主函数(C语言规定必须有也只能有1个主函数)********************************************************/void main(void){while(1)//无限循环{P1=0xfe;//P1=11111110B,P1.0输出低电平delay();//延时一段时间P1=0xff;//P1=11111111B,P1.0输出高电平delay();//延时一段时间// 5 P1 P0 P2 P3 I/O //实例 5:将 #include<reg51.h> P1 口状态分别送入 P0、P2、P3 口:认识 I/O口 的引脚功能//包含单片机寄存器的头文件/*******************************************************函数功能:主函数 (C 语言规定必须有也只能有 1个主函数)********************************************************/ void main(void){while(1) //无限循环{P1=0xff; // P1=1111 1111B,熄灭 LEDP0=P1; // 将 P1 口状态送入 P0 口P2=P1; // 将 P1 口状态送入 P2 口P3=P1; // 将 P1 口状态送入 P3口}}//实例 6:使用 P3 口流水点亮 8 位LED #include<reg51.h> //包含单片机寄存器的头文件/****************************************函数功能:延时一段时间*****************************************/void delay(void){unsigned char i,j;for(i=0;i<250;i++)for(j=0;j<250;j++);}/*******************************************************函数功能:主函数********************************************************/ voidmain(void){while(1){P3=0xfe; delay(); P3=0xfd; delay(); P3=0xfb; delay(); P3=0xf7; delay(); P3=0xef; //第一个灯亮//调用延时函数//第二个灯亮//调用延时函数//第三个灯亮//调用延时函数//第四个灯亮//调用延时函数//第五个灯亮delay(); //调用延时函数P3=0xdf; delay(); P3=0xbf;//第六个灯亮//调用延时函数//第七个灯亮delay(); //调用延时函数P3=0x7f; //第八个灯亮} }delay(); //调用延时函数//实例7:通过对P3口地址的操作流水点亮8位LED#include<reg51.h>//包含单片机寄存器的头文件sfr x=0xb0;//P3口在存储器中的地址是b0H,通过sfr可定义8051内核单片机//的所有内部8位特殊功能寄存器,对地址x的操作也就是对P1口的操作/****************************************函数功能:延时一段时间*****************************************/void delay(void){unsigned char i,j;for(i=0;i<250;i++)for(j=0;j<250;j++);//利用循环等待若干机器周期,从而延时一段时间}/*****************************************函数功能:主函数******************************************/void main(void){while(1){x=0xfe;//第一个灯亮delay();//调用延时函数x=0xfd;//第二个灯亮delay();//调用延时函数x=0xfb;//第三个灯亮delay();//调用延时函数x=0xf7;//第四个灯亮delay();//调用延时函数x=0xef;//第五个灯亮delay();//调用延时函数x=0xdf;//第六个灯亮delay();//调用延时函数x=0xbf;//第七个灯亮delay();//调用延时函数x=0x7f;//第八个灯亮delay();//调用延时函数}}//实例8:用不同数据类型控制灯闪烁时间#include<reg51.h>//包含单片机寄存器的头文件/******************************************************函数功能:用整形数据延时一段时间******************************************************/void int_delay(void)//延时一段较长的时间{unsigned int m;//定义无符号整形变量,双字节数据,值域为0~65535 for(m=0;m<36000;m++);//空操作}/******************************************************函数功能:用字符型数据延时一段时间******************************************************/void char_delay(void)//延时一段较短的时间{unsigned char i,j;//定义无符号字符型变量,单字节数据,值域0~255 for(i=0;i<200;i++)for(j=0;j<180;j++);//空操作}/******************************************************函数功能:主函数******************************************************/void main(void){unsigned char i;while(1){for(i=0;i<3;i++){P1=0xfe;//P1.0口的灯点亮int_delay();//延时一段较长的时间P1=0xff;//熄灭int_delay();//延时一段较长的时间}for(i=0;i<3;i++){P1=0xef;//P1.4口的灯点亮char_delay();//延时一段较长的时间P1=0xff;//熄灭char_delay();//延时一段较长的时间}}}//实例9:用P0口、P1口分别显示加法和减法运算结果#include<reg51.h>void main(void){unsigned char m,n;m=43; //即十进制数2x16+11=43n=60;P1=m+n; //即十进制数3x16+12=60//P1=103=01100111B,结果P1.3、P1.4、P1.7 口的灯被点亮}P0=n-m; //P0=17=00010001B,结果P0.0、P0.4的灯被熄灭//实例10:用P0、P1口显示乘法运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){unsigned char m,n;unsigned int s;m=64;n=71;s=m*n;//s=64*71=4544,需要16位二进制数表示,高8位送P1口,低8位送P0口//由于4544=17*256+192=H3*16*16*16+H2*16*16+H1*16+H0//两边同除以256,可得17+192/256=H3*16+H2+H1*16+H0)/256//因此,高8位16进制数H3*16+H2必然等于17,即4544除以256的商//低8位16进制数H1*16+H0必然等于192,即4544除以256的余数P1=s/256;//高8位送P1口,P1=17=11H=00010001B,P1.0和P1.4口灭,其余亮P0=s%256;//低8位送P0口,P3=192=c0H=11000000B,P3.1,P3.6,P3.7口灭,其余亮}//实例11:用P1、P0口显示除法运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P1=36/5;//求整数P0=((36%5)*10)/5;//求小数while(1); //无限循环防止程序“跑飞”}//实例12:用自增运算控制P0口8位LED流水花样#include<reg51.h>//包含单片机寄存器的头文件/******************************************************函数功能:延时一段时间******************************************************/void delay(void){unsigned int i;for(i=0;i<20000;i++);}/******************************************************函数功能:主函数******************************************************/void main(void){unsigned char i;for(i=0;i<255;i++)//注意i的值不能超过255{P0=i;//将i的值送P0口delay();//调用延时函数}}//实例13:用P0口显示逻辑"与"运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=(4>0)&&(9>0xab);//将逻辑运算结果送P0口while(1);//设置无限循环,防止程序“跑飞”}//14P0//实例14:用P0口显示条件运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=(8>4)?8:4;//将条件运算结果送P0口,P0=8=00001000B while(1);//设置无限循环,防止程序“跑飞”}//实例15:用P0口显示按位"异或"运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=0xa2^0x3c;//将条件运算结果送P0口,P0=8=00001000B while(1);//设置无限循环,防止程序“跑飞”}//16P0//实例16:用P0显示左移运算结果#include<reg51.h>//包含单片机寄存器的头文件void main(void){P0=0x3b<<2;//将左移运算结果送P0口,P0=11101100B=0xec while(1);//无限循环,防止程序“跑飞”}#include<reg51.h> //实例17:"万能逻辑电路"实验//包含单片机寄存器的头文件sbit X=P1^5; sbit Y=P1^6; sbit Z=P1^7; void main(void) {while(1){ //将X位定义为//将Y位定义为//将Z位定义为P1.5P1.6P1.7} }F=((~X)&Y)|Z;//将逻辑运算结果赋给F;//实例18:用右移运算流水点亮P1口8位LED#include<reg51.h>//包含单片机寄存器的头文件/*****************************函数功能:延时一段时间*****************************/void delay(void){unsigned int n;for(n=0;n<30000;n++);}/*****************************函数功能:主函数*****************************/void main(void){unsigned char i;while(1){P1=0xff;delay();for(i=0;i<8;i++)//设置循环次数为8{P1=P1>>1;//每次循环P1的各二进位右移1位,高位补0delay();//调用延时函数}}}//19iff P08LED//实例19:用iff语句控制P0口8位LED的流水方向#include<reg51.h>//包含单片机寄存器的头文件sbit S1=P1^4;//将S1位定义为P1.4sbit S2=P1^5;//将S2位定义为P1.5/*****************************函数功能:主函数*****************************/void main(void){while(1){if(S1==0)//如果按键S1按下P0=0x0f;//P0口高四位LED点亮if(S2==0)//如果按键S2按下P0=0xf0;//P0口低四位LED点亮}}//实例20:用swtich语句的控制P0口8位LED的点亮状态#include<reg51.h>//包含单片机寄存器的头文件sbit S1=P1^4;//将S1位定义为P1.4/*****************************函数功能:延时一段时间*****************************/void delay(void){unsigned int n;for(n=0;n<10000;n++);}/*****************************函数功能:主函数*****************************/void main(void){unsigned char i;i=0;while(1){//将i初始化为0if(S1==0) {delay();//如果S1键按下//延时一段时间} if(S1==0)//如果再次检测到S1键按下i++;//i自增1if(i==9)//如果i=9,重新将其置为1 i=1;} switch(i)}{}//使用多分支选择语句case1:P0=0xfe;//第一个LED亮break;case2:P0=0xfd;//第二个LED亮break;case3:P0=0xfb;//第三个LED亮break;case4:P0=0xf7;//第四个LED亮break;case5:P0=0xef;//第五个LED亮break;case6:P0=0xdf;//第六个LED亮break;case7:P0=0xbf;//第七个LED亮break;case8:P0=0x7f;//第八个LED亮break;default://缺省值,关闭所有LEDP0=0xff;//21for//实例21:用for语句控制蜂鸣器鸣笛次数#include<reg51.h>//包含单片机寄存器的头文件sbit sound=P3^7;//将sound位定义为P3.7/**************************************** 函数功能:延时形成1600Hz音频****************************************/ void delay1600(void){unsigned char n;for(n=0;n<100;n++);}/**************************************** 函数功能:延时形成800Hz音频****************************************/ void delay800(void){unsigned char n;for(n=0;n<200;n++);}/**************************************** 函数功能:主函数****************************************/ void main(void){unsigned int i;while(1){for(i=0;i<830;i++){sound=0;//P3.7输出低电平delay1600();sound=1;//P3.7输出高电平delay1600();}for(i=0;i<200;i++){sound=0;//P3.7输出低电平delay800();sound=1;//P3.7输出高电平delay800();}}}//实例22:用whille语句控制LED#include<reg51.h> //包含单片机寄存器的头文件/****************************************函数功能:延时约60ms(3*100*200=60000μs)****************************************/void delay60ms(void){unsigned char m,n;for(m=0;m<100;m++)for(n=0;n<200;n++);}/****************************************函数功能:主函数****************************************/void main(void){unsigned char i;while(1)//无限循环{i=0;//将i初始化为0while(i<0xff)//当i小于0xff(255)时执行循环体{P0=i;//将i送P0口显示delay60ms();//延时i++;//i自增1}}}//实例23:用do-whiile语句控制P0口8位LED流水点亮#include<reg51.h>//包含单片机寄存器的头文件/****************************************函数功能:延时约60ms(3*100*200=60000μs)****************************************/void delay60ms(void){unsigned char m,n;for(m=0;m<100;m++)for(n=0;n<200;n++);}/****************************************函数功能:主函数****************************************/void main(void){do{P0=0xfe;//第一个LED亮delay60ms();P0=0xfd;//第二个LED亮delay60ms();P0=0xfb;//第三个LED亮delay60ms();P0=0xf7;//第四个LED亮delay60ms();P0=0xef;//第五个LED亮delay60ms();P0=0xdf;//第六个LED亮delay60ms();delay60ms();P0=0xbf;//第七个LED亮delay60ms();P0=0x7f;//第八个LED亮delay60ms();}while(1);//无限循环,使8位LED循环流水点亮}//实例24:用字符型数组控制P0口8位LED流水点亮#include<reg51.h>//包含单片机寄存器的头文件/****************************************函数功能:延时约60ms(3*100*200=60000μs)****************************************/void delay60ms(void){unsigned char m,n;for(m=0;m<100;m++)} for(n=0;n<200;n++);/****************************************函数功能:主函数****************************************/void main(void){unsigned char i;unsigned char code Tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//定义无符号字符型数组while(1){for(i=0;i<8;i++){P0=Tab[i];//依次引用数组元素,并将其送P0口显示delay60ms();//调用延时函数}}}//25P0//实例25:用P0口显示字符串常量#include<reg51.h> //包含单片机寄存器的头文件/*************************************************函数功能:延时约150ms(3*200*250=150000μs=150ms*************************************************/void delay150ms(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char str[]={"Now,Temperature is:"};//将字符串赋给字符型全部元素赋值unsigned char i;while(1){i=0; //将i初始化为0,从第一个元素开始显示} }while(str[i]!='\0')//只要没有显示到结束标志'\0'{P0=str[i];//将第i个字符送到P0口显示delay150ms();//调用150ms延时函数i++;//指向下一个待显字符}//实例26:用P0#include<reg51.h>void main(void){口显示指针运算结果unsigned char*p1,*p2; //定义无符号字符型指针变量p1,p2 unsigned char i,j; //定义无符号字符型数据i=25; j=15;p1=&i ;p2=&j ; //给i赋初值25//使指针变量指向i//使指针变量指向j,对指针初始化,对指针初始化P0=*p1+*p2; //*p1+*p2相当于i+j,所以P0=25+15=40=0x28}//则P0=00101000B,结果P0.3、P0.5引脚LED熄灭,其余点亮while(1);//无限循环,防止程序“跑飞”//27P08LED//实例27:用指针数组控制P0口8位LED流水点亮#include<reg51.h>/************************************************* 函数功能:延时约150ms(3*200*250=150000μs=150ms *************************************************/ void delay150ms(void){} for(n=0;n<250;n++) ;/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char code Tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char*p[]={&Tab[0],&Tab[1],&Tab[2],&Tab[3],&Tab[4],&Tab[5],&Tab[6],&Tab[7]};unsigned char i;//定义无符号字符型数据while(1){for(i=0;i<8;i++){P0=*p[i];delay150ms();}}}//28P08LED//实例28:用数组的指针控制P0#include<reg51.h>口8位LED流水点亮/*************************************************函数功能:延时约150ms(3*200*250=150000μs=150ms*************************************************/void delay150ms(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){} unsigned char i;unsigned char Tab[]={0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFE,0xFC,0xFB,0xF0,0xE0,0xC0,0x80,0x00,0xE7,0xDB,0xBD,0x7E,0x3C,0x18,0x00,0x81,0xC3,0xE7,0x7E,0xBD,0xDB,0xE7,0xBD,0xDB};//流水灯控制码unsigned char*p;//定义无符号字符型指针p=Tab;//将数组首地址存入指针pwhile(1){for(i=0;i<32;i++)//共32个流水灯控制码{P0=*(p+i);//*(p+i)的值等于a[i]delay150ms();//调用150ms延时函数}}//29P0P1//实例29:用P0#include<reg51.h>、P1口显示整型函数返回值/*************************************************函数功能:计算两个无符号整数的和*************************************************/unsigned int sum(int a,int b){unsigned int s;s=a+b;return(s);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned z;z=sum(2008,2009);P1=z/256;//取得z的高8位P0=z%256;//取得z的低8位while(1);}//实例30:用有参函数控制P0口8位LED流水速度#include<reg51.h>/*************************************************函数功能:延时一段时间*************************************************/void delay(unsigned char x){unsigned char m,n;for(m=0;m<x;m++)for(n=0;n<200;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char i;unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//流水灯控制码while(1){//快速流水点亮LEDfor(i=0;i<8;i++)//共8个流水灯控制码{P0=Tab[i];delay(100);//延时约60ms,(3*100*200=60000μs)}//慢速流水点亮LEDfor(i=0;i<8;i++)//共8个流水灯控制码{P0=Tab[i];delay(250);//延时约150ms,(3*250*200=150000μs)}}}22/192//31//实例31:用数组作函数参数控制流水花样#include<reg51.h>/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:流水点亮P0口8位LED*************************************************/void led_flow(unsigned char a[8]){unsigned char i;for(i=0;i<8;i++){P0=a[i];delay();}}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//流水灯控制码led_flow(Tab);}//32P08LED//实例32:用指针作函数参数控制P0口8位LED流水点亮/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:流水点亮P0口8位LED*************************************************/void led_flow(unsigned char*p)//形参为无符号字符型指针{unsigned char i;while(1){i=0;//将i置为0,指向数组第一个元素while(*(p+i)!='\0')//只要没有指向数组的结束标志{P0=*(p+i);//取的指针所指变量(数组元素)的值,送P0口delay();//调用延时函数i++;//指向下一个数组元素}}}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE, 0xFF,0xFE,0xFC,0xFB,0xF0,0xE0,0xC0,0x80,0x00,0xE7,0xDB,0xBD,0x7E,0xFF,0xFF,0x3C,0x18,0x0,0x81,0xC3,0xE7,0xFF, 0xFF,0x7E};//流水灯控制码unsigned char*pointer;224/192} pointer=Tab;led_flow(pointer);//33P1//实例33:用函数型指针控制P1口灯花样#include<reg51.h>//包含51单片机寄存器定义的头文件unsigned char code Tab[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F}; //流水灯控制码,该数组被定义为全局变量/************************************************************** 函数功能:延时约150ms**************************************************************/ void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/************************************************************** 函数功能:流水灯左移**************************************************************/ void led_flow(void){unsigned char i;for(i=0;i<8;i++)//8位控制码{P0=Tab[i];delay();}}/************************************************************** 函数功能:主函数**************************************************************/ void main(void){void(*p)(void);//定义函数型指针,所指函数无参数,无返回值p=led_flow;//将函数的入口地址赋给函数型指针pwhile(1)(*p)(); //通过函数的指针p调用函数led_flow()}//34//实例34:用指针数组作为函数的参数显示多个字符串#include<reg51.h>//包含51单片机寄存器定义的头文件unsigned char code str1[]="Temperature is tested by DS18B20";//C语言中,字符串是作为字符数组来处理的unsigned char code str2[]="Now temperature is:";//所以,字符串的名字就是字符串的首地址unsigned char code str3[]="The Systerm is designed by Zhang San";unsigned char code str4[]="The date is2008-9-30";unsigned char*p[]={str1,str2,str3,str4};//定义p[4]为指向4个字符串的字符型指针数组/**************************************************************函数功能:延时约150ms**************************************************************/ void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/**************************************************************函数功能:流水点亮P0口8位LED**************************************************************/ void led_display(unsigned char*x[])//形参必须为指针数组{unsigned char i,j;for(i=0;i<4;i++)//有4个字符串要显示{j=0;//指向待显字符串的第0号元素while(*(x[i]+j)!='\0')//只要第i个字符串的第j号元素不是结束标志{P0=*(x[i]+j);//取得该元素值送到P0口显示delay();//调用延时函数j++;//指向下一个元素}}}/************************************************************** 函数功能:主函数**************************************************************/ void main(void){unsigned char i;while(1){for(i=0;i<4;i++)led_display(p);//将指针数组名作实际参数传递}}//实例35:字符函数ctype.h应用举例#include<reg51.h>//包含51单片机寄存器定义的头文件#include<ctype.h>void main(void){while(1){P3=isalpha('_')?0xf0:0x0f;//条件运算,若'_'是英文字母,P3=0xf0 }}//实例36:内部函数intrins..h应用举例#include<reg51.h>//包含51单片机寄存器定义的头文件#include<intrins.h>//包含函数isalpha()声明的头文件/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}227/192/*************************************************函数功能:主函数*************************************************/void main(void){P3=0xfe;//P3=11111110Bwhile(1){P3=_crol_(P3,1);//将P3的二进制位循环左移1位后再赋给P3 delay();//调用延时函数}}//37stdlib.h//实例37:标准函数stdliib.h应用举例#include<reg51.h>//包含51单片机寄存器定义的头文件#include<stdlib.h>//包含函数isalpha()声明的头文件/*************************************************函数功能:延时约150ms*************************************************/void delay(void){unsigned char m,n;for(m=0;m<200;m++)for(n=0;n<250;n++);}/*************************************************函数功能:主函数*************************************************/void main(void){unsigned char i;while(1){for(i=0;i<10;i++)//产生10个随机数{P3=rand()/160;//将产生的随机数缩小160倍后送P3显示delay();}}}//实例 38:字符串函数 striing.h应用举例 #include<reg51.h> //包含 51 单片机寄存器定义的头文件 #include<string.h> //包含函数 isalpha ()声明的头文件 void main(void){unsigned char str1[ ]="Now, The temperature is :";unsigned char str2[ ]="Now, The temperature is 36 Centgrade:"; unsigned char i;i=strcmp(str1,str2); //比较两个字符串,并将结果存入i if(i==0) //str1=str2P3=0x00;elseif(i<0) //str1<str2P3=0xf0;else //str1>str2P3=0x0f;while(1); //防止程序“跑飞”}// 39 2 #include<reg51.h> //实例 39:宏定义应用举例2 //包含 51 单片机寄存器定义的头文件 # define F(a,b) (a)+(a)*(b)/256+(b) void main(void){unsigned char i,j,k;i=40;j=30;k=20;//带参数的宏定义,a 和 b 为形参 参} P3=F(i,j+k); while(1);//i 和 j+k分别为实参,宏展开时,实参将替代宏定义中的形//实例40:宏定义应用举例2 #include<AT89X51.h>#include<ctype.h>void main(void){P3_0=0;//将P3.0引脚置低电平,LED点亮P3_1=0;//将P3.0引脚置低电平,LED点亮P3_2=0;//将P3.0引脚置低电平,LED点亮P3_3=0;//将P3.0引脚置低电平,LED点亮P3_4=1;//将P3.4引脚置高电平,LED熄灭P3_5=1;//将P3.5引脚置高电平,LED熄灭P3_6=1;//将P3.7引脚置高电平,LED熄灭P3_7=1;//将P3.7引脚置高电平,LED熄灭while(1);}//实例41:宏定义应用举例3#include<reg51.h >#define MAX100 void main(void) {#if MAX>80P3=0xf0;#elseP3=0x0f;#endif}//包含51单片机寄存器定义的头文件//将MAX宏定义为字符串100//如果字符串100大于80//P3口低四位LED点亮//否则,P3口高四位LED点亮//结束本次编译/***************************************************** ************中断、定时器********中断、定时器*********************中断、定时器*********中断、定时器****************************************************************** **///实例42:用定时器T0查询方式P2口8位控制LED闪烁#include<reg51.h>//包含51单片机寄存器定义的头文件/**************************************************************函数功能:主函数**************************************************************/void main(void){//EA=1;//开总中断//ET0=1;//定时器T0中断允许TMOD=0x01;//使用定时器T0的模式1TH0=(65536-46083)/256;//定时器T0的高8位赋初值TL0=(65536-46083)%256;//定时器T0的高8位赋初值TR0=1;//启动定时器T0TF0=0;P2=0xff;while(1)//无限循环等待查询{while(TF0==0);TF0=0;P2=~P2;TH0=(65536-46083)/256;//定时器T0的高8位赋初值TL0=(65536-46083)%256;//定时器T0的高8位赋初值}}//实例43:用定时器T1查询方式控制单片机发出1KHz音频/************************************************************** 函数功能:主函数**************************************************************/ void main(void){//EA=1;//开总中断//ET0=1;//定时器T0中断允许TMOD=0x10;//使用定时器T1的模式1TH1=(65536-921)/256;//定时器T1的高8位赋初值TL1=(65536-921)%256;//定时器T1的高8位赋初值TR1=1;//启动定时器T1TF1=0;while(1)//无限循环等待查询{while(TF1==0);TF1=0;sound=~sound;//将P3.7引脚输出电平取反TH1=(65536-921)/256;//定时器T0的高8位赋初值TL1=(65536-921)%256;//定时器T0的高8位赋初值}}//44T0P18LED //实例44:将计数器T0计数的结果送P1口8位LED显示#include<reg51.h>//包含51单片机寄存器定义的头文件sbit S=P3^4;//将S位定义为P3.4引脚/************************************************************** 函数功能:主函数**************************************************************/ void main(void){//EA=1;//开总中断//ET0=1;//定时器T0中断允许TMOD=0x02;//使用定时器T0的模式2TH0=256-156;//定时器T0的高8位赋初值TL0=256-156;//定时器T0的高8位赋初值TR0=1;//启动定时器T0while(1)//无限循环等待查询{while(TF0==0)//如果未计满就等待。

单片机C语言程序设计实训100例--基于8051+PROTEUS仿真1

单片机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仿真1

单片机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仿真》案例第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仿真

单片机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仿真1

单片机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仿真毕业论文

(完整版)单片机C语言程序设计实训100例-基于8051+PROTEUS仿真毕业论文

《单片机C语言程序设计实训100例—基于8051+Proteus仿真》案例第01 篇基础程序设计01 闪烁的LED* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*#include<reg51.(){while(1){LED=~LED;DelayMS(150);}}02 从左到右的流水灯* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*#include<reg51.(){P0=0xfe;while(1){P0=_crol_(P0,1); P0的值向左循环移动DelayMS(150);}}03 8只LED左右来回点亮* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_cror_形成来回滚动的效果*#include<reg51.(){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._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

创作编号:GB8878185555334563BT9125XW创作者:凤呜大王*《单片机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);创作编号:GB8878185555334563BT9125XW创作者:凤呜大王*}}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,创作编号:GB8878185555334563BT9125XW创作者:凤呜大王*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此后变红灯,东西向变绿灯,如此重复。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《单片机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此后变红灯,东西向变绿灯,如此重复。

*/#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。

相关文档
最新文档