Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How does a HEX file work?

Status
Not open for further replies.

markdem

Member level 3
Member level 3
Joined
May 16, 2006
Messages
58
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,830
Hi all,

I have been asking Google for the last hour, but cant find a answer.

I would like to know what magic make the connection between the HEX file and the micro controller. In other words, how does me writing something like "output_high(PIN_A0);" make PIN_A0 high?
Most websites tell me that the c code is converted to ASM and then a HEX file is made, burned to a PIC and then the PIC executes the code but leave it there. I wan to know HOW the HEX file is executed.

I hope this makes scene.

Thanks.
 

well, for what I know, it all comes down to opcodes, which tells the microcontroller what to do. also, the microcontrollers work with adressing, so when you call output_high, the microcontroller goes to the adress and start following the opcodes from there. if i'm not mistaken, you can find a library where all instructions are defined as hex numbers.

please correct me if i'm wrong
 

Hi all,

I have been asking Google for the last hour, but cant find a answer.

I would like to know what magic make the connection between the HEX file and the micro controller. In other words, how does me writing something like "output_high(PIN_A0);" make PIN_A0 high?
Most websites tell me that the c code is converted to ASM and then a HEX file is made, burned to a PIC and then the PIC executes the code but leave it there. I wan to know HOW the HEX file is executed.

I hope this makes scene.

Thanks.

Hi,

Yes, I do understand your point though cannot give you a totally clear answer or find an exact decription on the web.

Basically the microchip has to have a bootstap or bios type system built in, which when power is applied to the chip, it starts off the most basic functions, this may be hardwired on some earlier chips but I would think modern chips will have a small amount of Rom which is coded at production.

Exactly how the bootstap / bios works with the hardwares logic units, memory and hex program code would probably take volumes to detail.

This link might help
https://en.wikipedia.org/wiki/Booting
 

Hex files are not executed. They are a representation of the numbers to be put into the microcontroller, in a text format with added information to tell a programming unit where in the micro the code is to be placed.

The process is:
1. you write source code
2. you assemble it or compile it. The assembler/compiler produces a hex file as described above.
3. your programming hardware interprets the information in the hex file and burns the data into the chip.

What actually ends up in the chip is the binary values converted from the text, stored at the address near the start of each line in the hex file. If you do a search on "intel hex format" it should explain how the hex file is constructed.

Brian.
 

Thanks for the comments guys, but that is exactly what i can find on the web.

"The process is:
1. you write source code
2. you assemble it or compile it. The assembler/compiler produces a hex file as described above.
3. your programming hardware interprets the information in the hex file and burns the data into the chip."


The part that I would like to know is what happens next? How does the micro controller then use "the numbers" to control hardware.
Basically I would like to know the point when "the numbers" are converted to something happening on the hardware.

I understand that this is not a simple questions, but in amongst all the singing cats and photos of people's dinner there has to be some text about this on the web. I just can't find it.

Thanks
 

The part that I would like to know is what happens next? How does the micro controller then use "the numbers" to control hardware.
Basically I would like to know the point when "the numbers" are converted to something happening on the hardware.

You are essentially asking how a micro controller (or more generally a computer CPU) works. I can't believe that you didn't find reasonable answers on the web.

You should be aware of some serious studies being required.
 

Yes, I am asking how a micro controller works. Problem is that all the text on the web does not answer my question.
All the information I can find goes down to moving bits around memory, but not about how those bit control the hardware.

Good example is in this page

"Output pin. A logic zero (0) is applied to a bit of the P register. The output FE transistor is turned on, thus connecting the appropriate pin to ground."
That's great, but how is a 0 or a 1 in some bit of memory controlling that transistor? That is the magic that i want to know more about.

I understand that this is a complex subject, but I was more looking for something I can read and not a answer here.

Thanks Again
 
Last edited:

Your original question was about HEX files so this is really a different topic altogether.

You are asking a big question and a small answer can never be comprehensive but I'll give it a try. Please bear in mind that microcontrollers are basically dumb circuits, it's just that there are a lot of them packed into a small space.

The value in the HEX file produced by your compiler/assembler is a binary number, in the case of PICs each will be 12, 14 or 16 bits wide. Within those bits there are fields, some of the bit fields contain instructions, some contain constants. If you look at the data sheet at the start of the instruction set you will see a table which clearly shows how the program words break down into the fields. All these words are stored in the program memory of the PIC so it contains a list of instructions, each at a different memory address.

Also inside the PIC is a program counter which is simply a reloadable sequential counter that increases it's value by 1 every time an instruction is executed. When powered on or the PIC is reset, the program counter is set to a particular value called the reset vector. Most PICs use 0000 as the reset value but some use the last address in memory instead, read up on the 10F series to see why.

The program counter is connected to the program memory so the counter value selects the address where one of the instructions is stored. The bits at that address are then broken down into the fields mentioned earlier. This allows the instruction to be 'decoded' into what it has to do. It then starts a sequence of actions to perform the instruction using nothing more than normal logic gates. The program counter then advances to the next instruction. If the instruction was one that could divert the program flow, such as a call or jump, the destination is loaded into the program counter so it continues picking up instructions from there instead.

Some instructions have the destination field bits set to an internal register, some to an internal RAM or EEPROM address. When an internal register is selected, the result of the instruction is routed by the decoding logic so it gets stored in that register. If the register is a port, the results bits are stored in that port and an electrical connection to the pins of the port register makes the logic state of the bit appear on the pin of the PIC. Each bit of the port register is connected to a corresponding pin on the IC body.

The same happens when reading a pin in to the program. The instruction field contains bits that decode into a read operation, this sets in motion a sequence of operations that transfer the contents of the port register to the W regiater where it can be used in other instructions.

To see the actual way a number is converted to a voltage and vice versa, look at the data sheet for the 74LS245 which works in an almost identical way. The only difference in the case of a PIC is a further register called TRIS (TRI State) in which the bits operate some routing gates to change the direction of the pin from being an electrical output or an electrical input. The TRIS bit as analogous to the direction pin on the 74LS245.

Does that help?


Brian.
 

Thanks Brian.

Thanks for the great info. I have read the a heap of material and sort of understand how the instructions get executed, and that the port is "linked" to a part of memory.
However, you have stated the question in a way I should of in the first place (sorry, English is not my first language)

The questions should of been " How a number (bit in memory) is converted to a voltage and vice versa"
I had a look at the datasheet of the 74LS245, but it looks like a buffer. Is that the part you meant and I am not understanding something?

Thinking to myself, is the solution as simple as the charge on the cell of memory is connected to the base of the output transistor? I could not imagine how this would be done, but am I on the right track?

I wish when I type "how does a micro controller work" into Google it would give me a technical paper about the internal workings of the core.

Thanks again.
 

The 74LS245 is a bi-directional buffer which is exactly what's behind the pins inside the PIC. In a way, you are correct about it being a charge conducted to the base but all the PIC registers, including the one which are ports are actually made of flip-flops so each bit can assume one of two states, high or low. When writing a value to the flip-flop, it sets each bit to the 0 or 1 at that position in the data. So behind each port pin you have a flip-flop and a buffer and for a whole port you usually have a bank of 8 flip-flops sharing a common clock (the write signal). Unless they are clocked again (written to again) they retain the same state so the voltage on the pin stays constant until the program changes it.

When reading a pin, the buffer will have been set to input mode so the pin connects to the PIC internal data bus and the W register (again 8 flip-flops) is clocked so it latches the data into it.

As each instruction is executed, several internal operations take place to route the data and store it in it's destination. This is why the PIC needs at least four clock cycles to execute an instruction. Each of the clock cycles is operatig a flip-flop somewhere inside the silicon to move the data around.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top