/*************************第一個問題***********************************************/
第一個問題:如下圖所示,我這里是有五個源文件,然后有四個頭文件,因為函數只能在頭文件聲明而不能定義,所以函數都需要寫在對應的源文件中。包含main()函數的主文件再編譯的時候會把源文件中include的頭文件都原封不動地搬到主源文件中來,但是最后鏈接的時候因為所有函數定義都是在別的源文件中,所以在鏈接的時候主源文件也會把其他源文件中定義的函數直接搬運過來嗎?我可能表述的不太清楚,下面我就拿我其中一個源文件和頭文件舉例子吧。
question1.png (49.29 KB, 下載次數: 38)
下載附件
2019-6-7 06:17 上傳
************舉例子**********************
以下是頭文件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讓所有頭文件都能夠識別使用嗎?
|