微控制器-C語言-定義和申明

晨鐘_王發表於2018-09-08

以下程式碼是微控制器程式,51微控制器,編譯器為HT-IDE3000,

簡單來說

標頭檔案中只能申明,

變數在標頭檔案中申明時,要加上extern 這個關鍵字用來告訴編譯器,變數在其它的檔案中定義,為什麼要在標頭檔案中申明變數?———->因為想在其它檔案裡的程式碼中使用這些變數,

如在a.c中使用b.c裡定義的變數,

enum 不是變數,是我們自己定義的資料型別,像int char 一樣,只不過是我們自己定義的,所以不用加,

 1 extern u16    g_u16TimeBaseCount; 
 2 extern u8    g_8count_2s;
 3 extern u8    g_8count_8s;
 4 extern u8    g_8count_18s;
 5 extern u8    g_8count_60s;
 6 extern u8    g_8count_200s;
 7 extern u8    g_8count_6h;
 8 extern u8   g_8count_200sw;//6hour wash
 9 extern u8   g_8count_6hw;//6hour wash
10 extern u8   w_6hw_start;
11 extern u8   w_6hw_end;
12 extern u8   g_state;
13 extern u16    make_6h_count;
14 extern u8   g_once;
15 extern u16  s_c1;//中間變數
16 extern u16  s_c2;
17 extern u16  s_c3;
18 extern u16  s_c4;
19 
20 extern u8 UARTReceive;
21 extern u8 AD[6];
22 extern u8 TDS[6];
23 extern u8 count_AD;  
24 extern u8 count_ADS;       
25 extern u16 sumTDS_H,sumTDS_L,sum;
26 extern u16 temer;
27 
28 typedef enum GPIOPin
29 {
30   GPIO_Pin_0    = ((u8)0x01),   /*!< Pin 0 selected */
31   GPIO_Pin_1    = ((u8)0x02),   /*!< Pin 1 selected */
32   GPIO_Pin_2    = ((u8)0x04),   /*!< Pin 2 selected */
33   GPIO_Pin_3    = ((u8)0x08),   /*!< Pin 3 selected */
34   GPIO_Pin_4    = ((u8)0x10),   /*!< Pin 4 selected */
35   GPIO_Pin_5    = ((u8)0x20),   /*!< Pin 5 selected */
36   GPIO_Pin_6    = ((u8)0x40),   /*!< Pin 6 selected */
37   GPIO_Pin_7    = ((u8)0x80),   /*!< Pin 7 selected */
38   GPIO_Pin_LNib = ((u8)0x0F),   /*!< Low nibble pins selected */
39   GPIO_Pin_HNib = ((u8)0xF0),   /*!< High nibble pins selected */
40   GPIO_Pin_All  = ((u8)0xFF)    /*!< All pins selected */
41 }GPIO_Pin_TypeDef;

函式也在標頭檔案中,也只能申明,不能加函式體

u8 UART_Receive();

變數和函式的實現也就是函式體,只能在原始檔中定義

 1 uFlg uF1,uF2,uF3,uF4;
 2 u16    g_u16TimeBaseCount; 
 3 u8    g_8count_2s;
 4 u8    g_8count_8s;
 5 u8    g_8count_18s;
 6 u8    g_8count_60s;
 7 u8    g_8count_200s;
 8 u8    g_8count_6h;
 9 u8   g_8count_200sw;//6hour wash
10 u8   g_8count_6hw;//6hour wash
11 u8   w_6hw_start;
12 u8   w_6hw_end;
13 u8   g_state;
14 u16    make_6h_count;
15 u8   g_once;
16 u16  s_c1;//中間變數
17 u16  s_c2;
18 u16  s_c3;
19 u16  s_c4;
20 u8 UARTReceive=0;
21 u8 AD[6]={0,0,0,0,0,0};
22 u8 TDS[6]={0,0,0,0,0,0};
23 u8 count_AD=0; 
24 u8 count_ADS=0; 
25 u16 sumTDS_H=0,sumTDS_L=0,sum=0;
26 u16 temer;
27 
28 //===========================資料接收=========================================//
29 u8 UART_Receive()
30     {
31        u8 data;
32        if(_rxif==1)//如果接收到資料
33        {
34              data=_txr_rxr;
35              return data;
36            }
37      }

 

相關文章