模块化编程
将以往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);
- #endif
- light.h
- #ifndef __LIGHT_H__
- #define __LIGHT_H__
- void light(unsigned char location,num);
- #endif
- light.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;
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |