Header file in source code

Discussion in 'Programmer's Corner' started by vead, Sep 12, 2016.

  1. vead

    Thread Starter Active Member

    Nov 24, 2011
    621
    8
    Hello
    I saw header file come with compiler(keil) . Also it contain several lines of code. I saw, We need to include header file for microcontroller (p89v51rd2) device in source code. I saw every program start with header file. I don't understand what is use of header file and why we write at the top of source code?
     
    Last edited: Sep 12, 2016
  2. bertus

    Administrator

    Apr 5, 2008
    15,645
    2,344
    Hello,

    The header file contains information for the compiler.
    One of the things can be the port declarations.

    Here is the content of the P89V51Rx2 header file:

    Code (Text):
    1.  
    2. /*--------------------------------------------------------------------------
    3. P89V51Rx2.H
    4. Registers definition for Philips P89V51Rx2
    5.  
    6. Copyright (c) 2007 Keil Elektronik GmbH and Keil Software, Inc.
    7. All rights reserved.
    8. -------------------------------------------------------------------------- */
    9.  
    10. /* Byte Addresses */
    11. sfr P0 = 0x80;
    12. sfr SP = 0x81;
    13. sfr DPL = 0x82;
    14. sfr DPH = 0x83;
    15. sfr WDTD = 0x85;
    16. sfr SPDAT = 0x86;
    17. sfr PCON = 0x87;
    18. sfr TCON = 0x88;
    19. sfr TMOD = 0x89;
    20. sfr TL0 = 0x8A;
    21. sfr TL1 = 0x8B;
    22. sfr TH0 = 0x8C;
    23. sfr TH1 = 0x8D;
    24. sfr AUXR = 0x8E;
    25. sfr P1 = 0x90;
    26. sfr SCON = 0x98;
    27. sfr SBUF = 0x99;
    28. sfr P2 = 0xA0;
    29. sfr AUXR1 = 0xA2;
    30. sfr IEN0 = 0xA8;
    31. sfr SADDR = 0xA9;
    32. sfr SPCFG = 0xAA;
    33. sfr P3 = 0xB0;
    34. sfr FCF = 0xB1;
    35. sfr FST = 0xB6;
    36. sfr IP0H = 0xB7;
    37. sfr IP0 = 0xB8;
    38. sfr SADEN = 0xB9;
    39. sfr WDTC = 0xC0;
    40. sfr T2CON = 0xC8;
    41. sfr T2MOD = 0xC9;
    42. sfr RCAP2L = 0xCA;
    43. sfr RCAP2H = 0xCB;
    44. sfr TL2 = 0xCC;
    45. sfr TH2 = 0xCD;
    46. sfr PSW = 0xD0;
    47. sfr SPCTL = 0xD5;
    48. sfr CCON = 0xD8;
    49. sfr CMOD = 0xD9;
    50. sfr CCAPM0 = 0xDA;
    51. sfr CCAPM1 = 0xDB;
    52. sfr CCAPM2 = 0xDC;
    53. sfr CCAPM3 = 0xDD;
    54. sfr CCAPM4 = 0xDE;
    55. sfr ACC = 0xE0;
    56. sfr IEN1 = 0xE8;
    57. sfr CL = 0xE9;
    58. sfr CCAP0L = 0xEA;
    59. sfr CCAP1L = 0xEB;
    60. sfr CCAP2L = 0xEC;
    61. sfr CCAP3L = 0xED;
    62. sfr CCAP4L = 0xEE;
    63. sfr B = 0xF0;
    64. sfr IP1H = 0xF7;
    65. sfr IP1 = 0xF8;
    66. sfr CH = 0xF9;
    67. sfr CCAP0H = 0xFA;
    68. sfr CCAP1H = 0xFB;
    69. sfr CCAP2H = 0xFC;
    70. sfr CCAP3H = 0xFD;
    71. sfr CCAP4H = 0xFE;
    72.  
    73. /* Bit Addresses */
    74.  
    75. /* P0 */
    76. sbit AO = P0^0;
    77. sbit EXTRAM = P0^1;
    78.  
    79. /* TCON */
    80. sbit IT0 = TCON^0;
    81. sbit IE0 = TCON^1;
    82. sbit IT1 = TCON^2;
    83. sbit IE1 = TCON^3;
    84. sbit TR0 = TCON^4;
    85. sbit TF0 = TCON^5;
    86. sbit TR1 = TCON^6;
    87. sbit TF1 = TCON^7;
    88.  
    89. /* P1 */
    90. sbit T2EX = P1^1;
    91. sbit ECI = P1^2;
    92. sbit CEX0 = P1^3;
    93. sbit CEX1 = P1^4;
    94. sbit CEX2 = P1^5;
    95. sbit CEX3 = P1^6;
    96. sbit CEX4 = P1^7;
    97.  
    98. /* SCON */
    99. sbit RI = SCON^0;
    100. sbit TI = SCON^1;
    101. sbit RB8 = SCON^2;
    102. sbit TB8 = SCON^3;
    103. sbit REN = SCON^4;
    104. sbit SM3 = SCON^5;
    105. sbit SM1 = SCON^6;
    106. sbit SM0 = SCON^7;
    107.  
    108. /* P2 */
    109. sbit DPS = P2^0;
    110. sbit GF2 = P2^3;
    111.  
    112. /* IEN0 */
    113. sbit EX0 = IEN0^0;
    114. sbit ET0 = IEN0^1;
    115. sbit EX1 = IEN0^2;
    116. sbit ET1 = IEN0^3;
    117. sbit ES0 = IEN0^4;
    118. sbit ET2 = IEN0^5;
    119. sbit EC = IEN0^6;
    120. sbit EA = IEN0^7;
    121.  
    122. /* P3 */
    123. sbit RxD = P3^0;
    124. sbit TxD = P3^1;
    125. sbit INT0 = P3^2;
    126. sbit INT1 = P3^3;
    127. sbit T0 = P3^4;
    128. sbit T1 = P3^5;
    129. sbit WR = P3^6;
    130. sbit RD = P3^7;
    131.  
    132. /* IP0 */
    133. sbit PX0 = IP0^0;
    134. sbit PT0 = IP0^1;
    135. sbit PX1 = IP0^2;
    136. sbit PT1 = IP0^3;
    137. sbit PS = IP0^4;
    138. sbit PT2 = IP0^5;
    139. sbit PPC = IP0^6;
    140.  
    141. /* WDTC */
    142. sbit SWDT = WDTC^0;
    143. sbit WDT = WDTC^1;
    144. sbit WDTS = WDTC^2;
    145. sbit WDRE = WDTC^3;
    146. sbit WDOUT = WDTC^4;
    147.  
    148. /* T2CON */
    149. sbit RL2 = T2CON^0;
    150. sbit T2 = T2CON^1;
    151. sbit TR2 = T2CON^2;
    152. sbit EXEN2 = T2CON^3;
    153. sbit TCLK = T2CON^4;
    154. sbit RCLK = T2CON^5;
    155. sbit EXF2 = T2CON^6;
    156. sbit TF2 = T2CON^7;
    157.  
    158. /* PSW */
    159. sbit P = PSW^0;
    160. sbit F1 = PSW^1;
    161. sbit OV = PSW^2;
    162. sbit RS0 = PSW^3;
    163. sbit RS1 = PSW^4;
    164. sbit F0 = PSW^5;
    165. sbit AC = PSW^6;
    166. sbit CY = PSW^7;
    167.  
    168. /* CCON */
    169. sbit CCF0 = CCON^0;
    170. sbit CCF1 = CCON^1;
    171. sbit CCF2 = CCON^2;
    172. sbit CCF3 = CCON^3;
    173. sbit CCF4 = CCON^4;
    174. sbit CR = CCON^6;
    175. sbit CF = CCON^7;
    176.  
    177. /* IEN1 */
    178. sbit EBO = IEN1^3;
    179.  
    180. /* IP1 */
    181. sbit PBO = IP1^3;
    182.  
    bertus
     
    vead likes this.
  3. vead

    Thread Starter Active Member

    Nov 24, 2011
    621
    8
     
    Last edited: Sep 12, 2016
  4. bertus

    Administrator

    Apr 5, 2008
    15,645
    2,344
  5. vead

    Thread Starter Active Member

    Nov 24, 2011
    621
    8
    If you will see bottom of post I posted the question. may be some tag problem.
    , but my question is why we write heder file in source code. Does it set microcontroller? Means register memory counter,... Etc. Compiler convert c code in to binary number. Does it means compiler compile this line #include<reg51f.h> and send binary logic to microcontroller
     
  6. bertus

    Administrator

    Apr 5, 2008
    15,645
    2,344
  7. vead

    Thread Starter Active Member

    Nov 24, 2011
    621
    8
    Hello
    I have been programed p89v51rd2 microcontroller using keil compiler. I have tested code for led blink project. I understand that header file is store in directory of compiler. If compiler doesn't have header file for device. We can download. I have tested both conditions. In first case, I included header file and another conditions, I added content that you posted in your post in place of header file.
     
  8. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,383
    495
    You have not tested the third condition.

    The third condition: do not include header file.
     
  9. vead

    Thread Starter Active Member

    Nov 24, 2011
    621
    8
    If we don't include header file. Compiler give the error. Target not found
     
  10. MrSoftware

    Member

    Oct 29, 2013
    497
    122
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Header files such as the one quoted import information to the file using them. In this case it is giving the addresses of various registers so your main program can use them. They save you the trouble of looking up all that informant and possible copying it incorrectly.

    I do not use this compiler so I cannot give you exact direction, but every compiler needs to know what target or device the code is intended for. Without this information you may well get a "target not found" error. I would suspect the target is being set to some default value and not the device you use.

    HOW your compiler does this needs to be found. Some use a #pragma line in the main code, some have a setting in the compiler development program, some do both. You should check your manual to see how this vital piece of information is defined.
     
    vead likes this.
  12. vead

    Thread Starter Active Member

    Nov 24, 2011
    621
    8
    Can you help me with this code. What is use of main function?
    Code (Text):
    1. Void main(void)
    2. {
    3. while(1)
    4. {
    5. LED=LED_ON;
    6. delay (500);
    7. LED=LED_OFF;
    8. delay (5000);
    9. LED=LED_OFF;
    10. }
    11. }
     
  13. dl324

    Distinguished Member

    Mar 30, 2015
    3,235
    619
  14. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    In the C language all the code is placed inside functions. One function must be selected to run first at the very beginning of the program.

    That's what the Main() function is. It is the very first thing that runs. All other work must either be inside the Main() function (poor practice for all but the simplest of programs) or from other functions called by Main(), or called by these functions in turn.
     
    vead likes this.
  15. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,907
    2,163
    This is vital because usually there is a C Runtime initialization (like crt0.o) that uses this information and is linked into the executable program that runs before the user main() function.

    https://en.wikipedia.org/wiki/Runtime_library
     
Loading...