欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 【51单片机零基础-chapter5:模块化编程】

【51单片机零基础-chapter5:模块化编程】

2025/12/14 20:41:36 来源:https://blog.csdn.net/m0_59385870/article/details/144915521  浏览:    关键词:【51单片机零基础-chapter5:模块化编程】

模块化编程

在这里插入图片描述
在这里插入图片描述
将以往main中泛型的代码,放在与main平级的c文件中,在h中引用.
简化main函数
在这里插入图片描述
在这里插入图片描述
将原来main中的delay抽出
然后将delay放入单独c文件,并单独开一个delay头文件,里面放置函数的声明,相当于收纳delay的c文件里面写的函数的接口.
在这里插入图片描述
注意,单个c文件所有用到的变量需要在该文件里面声明或引用,函数也是.
存放头文件的地址修改处,但是一般用不着
在这里插入图片描述

预编译

在这里插入图片描述

#define AAA#ifdef AAA
fx//会执行
#endif#ifndef AAA
gx//不会执行,因为AAA已经定义
#endif

所以无论头文件定义什么,一般都会包围语句
fx.h:

#ifndef __FX_H__		//加#define  __FX_H__
void fx(...)#endif		//加

#include <.h>是安装目录里面找
#include “.h” 是程序目录里面找
可以软件里add new新建文件
也可也外部已有文件add existing
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
同时在主文件里右键open头文件可以打开则成功
在这里插入图片描述

在这里插入图片描述

模块化编程
main.c``````````````````````````````````
#include <REGX52.H>
#include "Delay.h"
#include "light.h"
void main(){while(1){light(1,9);Delay(1);light(2,8);Delay(1);light(3,5);Delay(1);light(6,2);Delay(1);light(7,1);Delay(1);light(8,1);Delay(1);}
}Dalay.c`````````````````````````````````
#include <REGX52.H>
void Delay(unsigned char x)		//@12.000MHz
{while(x--){unsigned char i, j;i = 2;j = 239;do{while (--j);} while (--i);}
}Delay.h`````````````````````````````````````````
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned char x);
#endiflight.h
#ifndef __LIGHT_H__
#define __LIGHT_H__
void light(unsigned char location,num);
#endiflight.c
#include <REGX52.H>
#include "Delay.h"
unsigned char lednum[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void light(unsigned char location,num){switch(location){case 1:P2_4=1;P2_3=1;P2_2=1; break;case 2:P2_4=1;P2_3=1;P2_2=0; break;case 3:P2_4=1;P2_3=0;P2_2=1; break;case 4:P2_4=1;P2_3=0;P2_2=0; break;case 5:P2_4=0;P2_3=1;P2_2=1; break;case 6:P2_4=0;P2_3=1;P2_2=0; break;case 7:P2_4=0;P2_3=0;P2_2=1; break;case 8:P2_4=0;P2_3=0;P2_2=0; break;}P0=lednum[num];Delay(1);P0=0x00;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词