Dhananjay Khairnar @. Powered by Blogger.

28 April 2016

What Is Inside Hex File ?( PIC microcontroller Hex file):

                The program is compiled and assembled and produces a binary .hex file as seen the Figure  This file was opened in a notepad text editor so this is the raw file. It would seem very difficult to understand the code from that strange set of characters but let’s break it down and you’ll see it's not that confusing.

First off, understand that this .hex file is in Intel Hex32 format. This means it can support 32 bit wide address memory devices. But the format is broken up into an upper 16 bits and a lower 16 bits. The upper 16 bits are known as the extended address.

Every new line begins with a colon. Then the numbers and letters that follow are hexadecimal codes that are control characters, address locations or data bytes. Each line in the file has this format:

BB contains the number of data bytes on line.

aaAA is address in memory where the bytes will be stored. This number is actually doubled which I’ll cover in a bit. Also the lower byte is first aa followed by the upper byte AA.

TT is the data type.
 00 - means program data.
 01 - means End of File (EOF).
 04  -means extended address. It indicates the data value is the upper 16 bits of the address.

DD is actual data bytes which contain the machine code that your program created. There can be numerous bytes in one line. The BB value indicates how many bytes are included in the line.

CC is calculated checksum value for error monitoring. It’s a 2s-complement calculation of: BB + AAAA + TT + DD.

So let’s look at the first line.

02 indicates the number of bytes on the line. In this case there are two bytes.

0000 is the memory address to place the bytes but its value was multiplied by 2. So normally we would divide the address by 2 but in this case 0000 / 2 = 0000. So the address is 0000.

04 means this is the extended address. So the data contains the upper 16 bits of the address. Any lower bit addresses that follow this line will use these upper bits until a new 04 line changes the upper bits.

0000 this is the upper 16 bits of the address indicated by the 04 data type.

FA is the 2’s compliment of the sum of the bytes:  02 + 00 + 00 + 04 + 00 + 00.

Note: 2’s compliment is just the binary value, inverted and then 1 added. Then the result converted to .hex format.

 02 + 00+ 00 + 04 + 00 + 00 = 06 = 00000110 binary
 00000110 inverted = 11111001
 11111001 + 1 = 11111010 = FA  hexadecimal

So this first line just says the upper address for all future bytes will have an upper (extended) address of 0000 and it will start at memory address 00000000 in the Microchip PIC.

 The next line breaks down this way:

02 indicates two data bytes.

0000 is the address again.

00 is the data type so the data bytes are program data.

0528 are the two data bytes. But these are reversed since the lower byte is first so this is really 2805 hex.

D1 is the 2’s compliment checksum.

So at address 00000000 (extended and lower word combined) the program data byte 2805 is stored. But what does that byte mean?

For this we have to consult the PIC16F886 data sheet (Figure 3). In the data sheet is the Assembly code instruction set. And for each instruction, the table 15-2 below, shows a 14 bit opcode. This is the binary machine code for the instruction.

So converting 2805 to binary we get 0010100000000101. Since the PIC16F886 uses a 14 bit opcode we can ignore the first two bits which are zeros. This leaves us with 10100000000101. If we then scan the opcode list we will find that code matches the GOTO instruction 10 1kkk kkkk kkkk. The kkk.. portion represents the address to goto. In this case the value after the 101 for goto leaves a value of 00000000101 or hex value 05.

So this would indicate that this line in the .hex file represents a GOTO 0x05 command line.
Published: By: DK - 22:07