HEX Files for Microcontroller 8051

 What are the HEX files

We know an interesting fact of microcontrollers or microprocessor that these understand only one language which is form of numbers in hexadecimal. What the language we use to write a program for any kind of microcontroller, it is ultimately converted into a special format under stable for them. This file contain useful information for the microcontroller operation in specific format is known as as hexfile. It is the duty of our compiler and assembler to make it. 

The Intel HEX file is an ASCII text file having lines of numbers that follow the Intel HEX file format.  In an Intel HEX file each line contains one HEX record. These records mean each line is made up of hexadecimal numbers which presentation of machine language code. The Intel HEX files are used to transfer the program code and data to microcontrollers which is stored in a ROM or EPROM of it. Thus most EPROM programmers or emulators uses Intel HEX files. Now we will discuss the HEX record format.

Record Format

It may be noted that an Intel HEX file may be composed of any number of HEX records. The format Record is such that each record is made up of five fields that are arranged in the following sequence:

 :llaaaatt[dd……..]cc

 Here each group of letters corresponds to a different specific field, and each letter represents a single hexadecimal digit which have some specific meanings. Each field is composed of at least two hexadecimal digits to form a byte as described below:

  • : is the colon,  every Intel HEX record starts with colon means every line in HEX file start with this symbol.
  • ll is the record length field which represents the number of data bytes (dd) in this record.
  • aaaa is the address field which represents the starting address for subsequent data in this record.
  • tt is the field represents the HEX record type, which may be one of the following type:
    00 - data record
    01 - end-of-file record
    02 - extended segment address record
    04 - extended linear address record
  • dd is a data field that represents one byte of data. A record may have multiple data bytes. The number of data bytes in the record must match the number specified in the ll field of this record.
  • cc is the checksum field represents the checksum of a particular record. The checksum is calculated by summing the values of all hexadecimal digit pairs in a record modulo 256 and then taking the two's complement.

Data Records

The Intel HEX file is made up of any number of data records. It may be noted that one record is per line at the end the end of line characters ends the line. As an example the Data records appear as follows:

 :10246200464C5549442050524F46494C4500464C33

 This record is decoded as follows:

:10246200464C5549442050524F46494C4500464C33

where:

  • 10 is the number of data bytes in the record.
  • 2462 is the address where the data are to be located in memory.
  • 00 is the record type 00 (a data record).
  • 464C...464C is the data.
  • 33 is the checksum of the record.

Extended Linear Address Records (HEX386)

Extended linear address records are also known as 32-bit address records and HEX386 records. These records contain the higher 16 bits i.e. bits 16-31 of the data address. The extended linear address record is usually composed of two data bytes and appears as follows:

:02000004FFFFFC

where:

  • 02 is the number of data bytes in the record.
  • 0000 is the address field. For the extended linear address record, this field is always 0000.
  • 04 is the record type 04 (an extended linear address record).
  • FFFF is the upper 16 bits of the address.
  • FC is the checksum of the record and is calculated as
    01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).

When an extended linear address record is read, the extended linear address stored in the data field is is applied to subsequent records of the Intel HEX file. The linear address remains same until changed by another extended address record of the hex file.

The absolute memory address of a data record is obtained by adding the address field in the record and the shifted address data of the extended linear address record. The following example will make it clear.

 

Address from the data record's address field      2462
Extended linear address record data field     FFFF
                                              --------
Absolute-memory address                       FFFF2462

Extended Segment Address Records (HEX86)

The Extended segment address records which is also known as HEX86 records. It is composed of bits 4 - 19 of the data address segment of record. The extended segment address record must have two data bytes and it appears as follows:

 :020000021200EA

where:

  • 02 is the number of data bytes in the record.
  • 0000 is the address field. For the extended segment address record, this field is always 0000.
  • 02 is the record type 02 (an extended segment address record).
  • 1200 is the segment of the address.
  • EA is the checksum of the record and is calculated as
    01h + NOT(02h + 00h + 00h + 02h + 12h + 00h).

When an extended segment address record is read, the extended segment address stored in the data field is applied to subsequent records of the Intel HEX file. The segment address remains same until changed by another extended address record.

The absolute memory address of a data record in hex file is obtained by adding the address field in the record and the shifted-address data of the extended segment address record. The following example will make it more clear this process.

 Address from the data record's address field     2462

Extended segment address record data field      1200
                                             --------
Absolute memory address                      00014462

End-of-File (EOF) Records

An Intel HEX file must end with an end-of-file (EOF) record. The value of this record field must be 01 in the record type field. An EOF record always appears as follows:

 :00000001FF

where:

  • 00 is the number of data bytes in the record.
  • 0000 is the address where the data are to be located in memory. The address in end-of-file records is meaningless and is ignored. An address of 0000h is typical.
  • 01 is the record type 01 (an end-of-file record).
  • FF is the checksum of the record and is calculated as
    01h + NOT(00h + 00h + 00h + 01h).

Example Intel HEX File

Following is a piece of assembly code:

 ORG 0000H

LJMP MAIN

ORG 0030H

MAIN:   MOV SCON,#52H

               MOV TMOD,#20H

               MOV TL1,#(-13) ;BAUD 2400

               MOV TH1,#(-13)

               SETB TR1

INCHAR: JNB RI,INCHAR

               CLR RI

               MOV A,SBUF

               CJNE A,#20H,NEXT

NEXT:   CPL C

        JNC LESS

               CJNE A,#7FH,WAIT

LESS:   MOV A,#2EH     

WAIT:   JNB TI,WAIT

               CLR TI

OUT:     MOV SBUF,A

               SJMP INCHAR

END

Following is the complete HEX file for above code:

:03000000020030CB

:10003000759852758920758BF3758DF3D28E309833

:10004000FDC298E599B42000B35003B47F02742E2A

:090050003099FDC299F59980E593

:00000001FF

No comments:

Post a Comment

Please ask if you have any question regarding the programming of MCU, or have any problem in development of your electronics project. microcontroller51.blogspot.com

Popular Post (All Time)