单片机C语言程序设计实例.doc
《单片机C语言程序设计实例.doc》由会员分享,可在线阅读,更多相关《单片机C语言程序设计实例.doc(72页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流单片机C语言程序设计实例.精品文档.单片机 C 语言程序设计实训 100 例-基于8051 和 PROTEUS 仿真案例单片机C语言程序设计实训 100 例基于 8051+Proteus仿真案例第 01篇基础程序设计01 闪烁的 LED/* 名称:闪烁的 LED说明:LED 按设定的时间间隔闪烁#include#define uchar unsigned char#define uint unsigned intsbit LED=P10;/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;
2、i+);/主程序void main()while(1)LED=LED;DelayMS(150);02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在 P0 口的 8 个 LED从左到右循环依次点亮,产生走马灯效果#include#include#define uchar unsigned char#define uint unsigned int上海师范大学信息与机电工程学院倪继锋1/延时void DelayMS(uint x)uchar i;while(x-)单片机 C 语言程序设计实训 100 例-基于8051 和 PROTEUS 仿真案例for(i=0;i120;i+);/主程序
3、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;i120;i+);/主程序void main()uchar i;P2=0x01;whi
4、le(1)上海师范大学信息与机电工程学院倪继锋2单片机 C 语言程序设计实训 100 例-基于for(i=0;i7;i+)P2=_crol_(P2,1); /P2 的值向左循环移动DelayMS(150); for(i=0;i7;i+)P2=_cror_(P2,1); /P2 的值向右循环移动DelayMS(150); 8051 和 PROTEUS 仿真案例04 花样流水灯/* 名称:花样流水灯说明:16 只 LED 分两组按预设的多种花样变换显示#include#define uchar unsigned char#define uint unsigned intuchar code Pat
5、tern_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
6、,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,
7、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
8、,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,0
9、xff, 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,上海师范大学信息与机电工程学院倪继锋3单片机 C 语言程序设计实训 100 例-基于 0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff /延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i12
10、0;i+);/主程序void main()uchar i;while(1)/从数组中读取数据送至 P0 和 P2 口显示for(i=0;i136;i+)P0=Pattern_P0i;P2=Pattern_P2i;DelayMS(100); 05 LED 模拟交通灯/* 名称:LED 模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁 5 次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁 5 此后变红灯,东西向变绿灯,如此重复。#include#define uchar unsigned char#define uint unsigned intsbit RED_A=P00; /东西
11、向灯sbit YELLOW_A=P01;sbit GREEN_A=P02;sbit RED_B=P03; /南北向灯sbit YELLOW_B=P04;sbit GREEN_B=P05;8051 和 PROTEUS 仿真案例uchar Flash_Count=0,Operation_Type=1; /闪烁次数,操作类型变量上海师范大学信息与机电工程学院倪继锋4/延时void DelayMS(uint x)uchar i;单片机 C 语言程序设计实训 100 例-基于8051 和 PROTEUS 仿真案例while(x-) for(i=0;i120;i+);/交通灯切换void Traffic_
12、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:/东西向红灯,南北向
13、绿灯亮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 单只数码管循环显示 09上海师范大学信息与机电工程学院倪继锋5单片机 C
14、语言程序设计实训 100 例-基于/* 名称:单只数码管循环显示 098051 和 PROTEUS 仿真案例说明:主程序中的循环语句反复将 09 的段码送至 P0 口,使数字 09 循环显示#include#include#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;t120;t+);/主程序
15、void main()uchar i=0;P0=0x00;while(1)P0=DSY_CODEi;i=(i+1)%10;DelayMS(300);07 8 只数码管滚动显示单个数字/* 名称:8 只数码管滚动显示单个数字说明:数码管从左到右依次滚动显示07,程序通过每次仅循环选通一只数码管#include#include#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /延时void DelayMS
16、(uint x)uchar t;while(x-) for(t=0;t120;t+);上海师范大学信息与机电工程学院倪继锋6/主程序void main()单片机 C 语言程序设计实训 100 例-基于8051 和 PROTEUS 仿真案例uchar i,wei=0x80;while(1)for(i=0;i8;i+)P2=0xff; /关闭显示wei=_crol_(wei,1);P0=DSY_CODEi;发送数字段码P2=wei; /发送位码DelayMS(300); 08 8 只数码管动态显示多个不同字符/* 名称:8 只数码管动态显示多个不同字符说明:数码管动态扫描显示 07。#includ
17、e#include#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;t120;t+);/主程序void main()uchar i,wei=0x80;while(1)for(i=0;i8;i+)P0=0xff; P0=DSY_CODEi;发送段码wei=_crol_(wei,1);上海师范大学信
18、息与机电工程学院倪继锋7单片机 C 语言程序设计实训 100 例-基于P2=wei; /发送位码DelayMS(2); 8051 和 PROTEUS 仿真案例09 8 只数码管闪烁显示数字串/* 名称:8 只数码管闪烁显示数字串电路如上图说明:数码管闪烁显示由 07 构成的一串数字本例用动态刷新法显示一串数字,在停止刷新时所有数字显示消失。#include#define uchar unsigned char#define uint unsigned int/段码表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
19、0x90; /位码表uchar code DSY_IDX=0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t120;t+);/主程序void main()uchar i,j;while(1)for(i=0;i30;i+)for(j=0;j8;j+)P0=0xff; P0=DSY_CODEj; /发送段码P2=DSY_IDXj; /发送位码DelayMS(2); P2=0x00;/关闭所有数码管并延时DelayMS(1000);上海师范大学信息与机电工程学院倪继锋8单
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单片机 语言程序设计 实例
限制150内