/*************************第一个问题***********************************************/
第一个问题:如下图所示,我这里是有五个源文件,然后有四个头文件,因为函数只能在头文件声明而不能定义,所以函数都需要写在对应的源文件中。包含main()函数的主文件再编译的时候会把源文件中include的头文件都原封不动地搬到主源文件中来,但是最后链接的时候因为所有函数定义都是在别的源文件中,所以在链接的时候主源文件也会把其他源文件中定义的函数直接搬运过来吗?我可能表述的不太清楚,下面我就拿我其中一个源文件和头文件举例子吧。
************举例子**********************
以下是头文件Display.h的内容,其中有void Display(uchar firstbit, uchar num)的声明而不是定义
单片机源程序如下:
- #include <reg52.h>
- #ifndef __DISPLAY_H__
- #define __DISPLAY_H__
- #define uchar unsigned char
- #define uint unsigned int
- #define KeyPort P3
- #define DataPort P0
- sbit Bit_Latch = P2^1;
- sbit Seg_Latch = P2^0;
- extern bit ReadTimeFlag;
- extern code uchar DuanMa[10];
- extern code uchar WeiMa[8];
- extern uchar TempData[8];
- void Display(uchar firstbit, uchar num);
- void Init_Timer0();
- #endif
复制代码
以下是Display.c源文件的内容,有void Display(uchar firstbit, uchar num)的定义但是不会被include到主原函数中,那么最后主源文件被链接成功是怎么
实现Display()函数的功能呢?
- #include "Display.h"
- #include "Delay.h"
- uchar code DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar code WeiMa[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- uchar TempData[8];
- void Display(uchar firstbit, uchar num)
- {
- static uchar i = 0;
-
- DataPort=0;
- Seg_Latch = 1;
- Seg_Latch = 0;
-
- DataPort = WeiMa[i+firstbit];
- Bit_Latch = 1;
- Bit_Latch = 0;
-
-
- DataPort = TempData[i];
- Seg_Latch = 1;
- Seg_Latch = 0;
-
-
-
- i++;
- if(i == num)
- {
- i = 0;
- }
- }
- void Init_Timer0()
- {
- TMOD = 0x01;
- TH0 = (65535-2000)/256;
- TL0 = (65535-2000)%256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void Timer0() interrupt 1
- {
-
- static uchar num;
- TH0 = (65535-500)/256;
- TL0 = (65535-500)%256;
- Display(0,8);
- num++;
-
- if(num == 50)
- {
- num = 0;
- ReadTimeFlag = 1;
- }
-
- }
复制代码
/***********************第二个问题*******************************************/
第二个问题:在用C语言编写程序时需要经常用到unsigned char和unsigned int,我一般习惯typedef unsigned char uchar;typedef unsigned int uint;
但是头文件里用typedef会报错,而且因为我把内容分成好几个源文件写了,所以在每一个头文件里都要#define uchar unsigned char #define uint unsigned int,这样会搞的比较麻烦。我想问下有什么好办法能够只写一次#define uchar unsigned char #define uint unsigned int让所有头文件都能够识别使用吗?
|