第4讲单片机C51基础及编程分析.ppt

  1. 1、本文档共38页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第4讲单片机C51基础及编程分析

4.3.3 库函数 C51语言的强大功能及其高效率在于提供了丰富的可直接调用的库函数。库函数可以使程序代码简单、结构清晰、易于调试和维护。 下面介绍几类重要的库函数。 (1) 特殊功能寄存器包含文件reg51.h或reg52.h。reg51.h中包含所有的8051的sfr及其位定义。reg52.h中包含所有8052的sfr及其位定义,一般系统都包含reg51.h或reg52.h。 (2) 绝对地址包含文件absacc.h:该文件定义了几个宏,以确定各类存储空间的绝对地址。 (3) 输入/输出流函数位于stdio.h文件中。流函数默认8051的串口来作为数据的输入/输出。如果要修改为用户定义的I/O口读写数据,例如,改为LCD显示,可以修改lib目录中的getkey.c及putchar.c源文件,然后在库中替换它们既可。 (4) 动态内存分配函数,位于stdlib.h中。 (5) 能够对方便地对缓冲区进行处理的缓冲区处理函数位于string.h中。其中包括复制、移动、比较等函数。 * 【例4-1】在AT89C51单片机的P1口上接有8只LED。程序运行后,8只发光二极管流水点亮。 4.4 C51程序设计实例 * 方法一:用数组实现 #includereg51.h unsigned char code play[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void delay(unsigned int i) { unsigned int j; for(;i0;i--) for(j=0;j125;j++); } void main() { unsigned int a; for(;;) { for(a=0;a8;a++) { delay(500); P1=play[a]; } } } #includereg51.h #include intrins.h void delay(unsigned int i) { unsigned int j; for(;i0;i--) for(j=0;j125;j++); } void main() { unsigned char i; for(;;) { P1=0xfe; for(i=0;i8;i++) { delay(500); P1=_crol_(P1,1) ; } } } 方法二:用左移函数_crol_实现 方法三:移位运算符实现 * #includereg51.h void delay(unsigned int i) { unsigned int j; for(;i0;i--) for(j=0;j125;j++); } void main( ) { unsigned char i,temp; for(;;) { temp=0x01; for(i=0;i8;i++) { P1=~temp; delay(500); temp=temp1; } } } * 【例4-2】在AT89C51单片机的P1口上接有8只LED。程序运行后,8只发光二极管流水点亮。按下按钮开关K1,低4位的LED和高4位的LED交替闪烁,闪烁5次后, 8只发光二极管再次流水点亮。 * #includereg51.h sbit K1=P3^2; unsigned char code play[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void delay(unsigned int i) { unsigned int j; for(;i0;i--) for(j=0;j125;j++); } void Alter() { unsigned char n; for(n=0;n5;n++) { P1=0x0f;delay(500); P1=0xf0;delay(500); } } void main() { unsigned int a; for(;;) { for(a=0;a8;a++) { delay(500); P1=play[a]; } if(K1==0) Alter(); } } * #includereg51.h unsigned char code play[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void delay(unsigned int i) { unsigned int j; for(;i0;i--) for(j=0;j125;j

文档评论(0)

wbjsn + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档