PIC program size and hex file size confusion

Discussion in 'Embedded Systems and Microcontrollers' started by sonar_abhi, May 17, 2016.

  1. sonar_abhi

    Thread Starter New Member

    Mar 19, 2016
    Hello guys!

    I am a newbie in PIC programming. I have written a simple program and compiled it in mikroC selecting PIC16F877A as the microcontroller. The generated hex file is of 5kB size. But in the mikroC compiler, the compiler shows the following messages:

    Used RAM (bytes): 2 (1%) Free RAM (bytes): 350 (99%)
    Used ROM (program words): 57 (1%) Free ROM (program words): 8135 (99%)

    Essentially my program size on the chip is only 57 words ~ 0.1kB, but when I build it, the generated hex file is 5kB in size.

    The funny part is that I am able to burn the 5kB hex file in PIC16f72 which has a program memory of 2kB

    Can anybody please explain what is going on?


  2. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    The hex file has a lot of overhead. First, each program byte is represented in the hex file by two bytes. Then there is overhead associated with the hex file format. This is the format of a hex file. The number in parens is the number of bytes used for each component. The hex file is organized into records, each of which has the following parts:
    • Start code (1)
    • Byte count (2)
    • Address (4)
    • Record type (2)
    • Data (2n)
    • Checksum (2)
    This is a total of 11 bytes per record of overhead.
    The fact that each byte of your program uses 2 bytes in the hex file is a major contributor to the size difference. This is due to using ASCII to encode binary. To learn more, read this
  3. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    And, you can view the hex file in a text editor to see what it looks like.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    A hex file will always be much larger that the code it contains. You can open one in most any text program such as Notepad and see what it contains.

    Since the "hex file" format was developed for other general things it includes error checking codes and expands numbers into ASCII characters and such.

    So don't worry, be happy. It all works out fine as you can see.