Vous êtes sur la page 1sur 5

HEX File for Microcontroller

Microcontroller: PIC16F877

Purpose of program: to blink 2 LEDs at PortA.0, PortA.2

;Address 0x0000 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009

Opcode 0x2803 0x1683 0x1303 0x0185 0x3005 0x1283 0x0085 0x2809

ASM GOTO BSF BCF CLRF MOVLW BCF 3 03h, 5 03h, 6 85h 5
From Assembly to HEX file .

83h, 5 05h $+0


:020000000328D3 :0E000600831603138501053083128500092837 :00000001FF

First of all I will explain the construction of intel HEX code : :0E000600831603138501053083128500092837 Any Code line start with : 1- 0E : means the number of data bytes which is colored in green

Try it : Byte = 2 HEX digits , so if we count them we have 28 HEX digits which equal to 14 Byte , If we convert 14 to hexadecimal it will give us 0E 2- 0006 is the first address of your program. If you observe that I wrote the first address, but if you have long program you can divide it into many lines and each line will contain group of codes, so for each group you will write the first address of that group. You can start from 0001 also or any address deals with Bank-0. 3- 00 , 2 HEX digits for the data type , it will be 00 for data and 01 for end line . for more information about that check the below file.

4- 8316031385010530831285000928 if you look at the above assembly code you will find that we collect all opcodes and put them in one line which is called data (in green) , ok thats good but what is the Opcode and how you can write yourself by means of using the datasheet of the microcontroller ? . Opcode is the operation code it consists of the instruction command or instruction plus data , when you write any program it will be registered into ROM but if you check the ROM for PIC16F877 will find that it is 14 bit width only and we have only 3 standards for storing the data (8 , 16 , 32 bit) , so it is sutiable to deal with this ROM as 16 bit ROM . our data will be 16 bit or 4 HEX digits because as you know each HEX digit equal to 4-bit , lets us take example :

Example : 8316031385010530831285000928 , this is our data I will divide it into 4 HEX digits which equal to 16-bit , I will explain one instruction only and you can check other in the data sheet of PIC : 8316 0313 8501 0530 8312 8500 0928 BSF 03h, 5

- if you look at the binary code for this instruction (BSF) you will find that it contains only 14-bit as we explained , so we will convert it to 16-bit by adding 2bit at the left side . (0001 01bb bfff ffff) fffffff : which file register you want to select . bbb : which bit you want to set it . Register (Status and its address 03H) : we will convert 03 to binary by calculator it will give us 00000011 but we need only 7-bit, so it will be 0000011 . - As you know that each register in RAM consists of 8-bit and to determine the address for each bit you want 3-bit address selector as the following : 000 (0), 001(1), 010(2), 011(3), 100(4), 101(5), 110(6), 111(7) So, bit-5 = 101=bbb - Now our binary number will be: 0001011010000011 and if we convert it to Hexadecimal it will be 1683 but when we put in Opcode we will put LSB first from

left side then HSB. Try the remaining and by this coding you can do your own assembler by visual Studio or any debugger.

Thanks and Best Regards Eng. Khaled Salahudeen Mohammad khaled_salah80@hotmail.com Egypt