September 22, 2017

What happens when power is given to microcontroller or a processor, Part – 2

So after reaching the power reset vector what does it do ? (Click  here to read the first part)

Simple it starts executing the instruction in order, according to PC(Program counter). As we understood from the part-1 The processor does initialization of the registers and then does a reset on the PC. After executing the first instruction, it increments the program counter, which will point to to the next instruction in program memory. This will keep on going until power is given to the chip or it goes to sleep mode.

Now if the PC value keep on incrementing how does we have the full control of the chip. That’s what the instruction written in the program does. When we write if, else,  while condition what is actually happening inside the chip, it changes the program counter so that the next instruction will be the jump to new instruction set we had written for a particular logic.  So it is upto the programmer to write the instruction to meet the task the micro controller/processor has to execute.

 

What happens when power is given to microcontroller or a processor, Part – 1

This is question every embedded system engineer should have a thorough knowledge.  When I ask this question to young/experienced engineers I get answers like, its starts executing the program, yes some persons answers properly also. If  I ask what program, they will say the program you had written like that.

So let me explain in my way what happens when power, (ie when you give the power supply to the microcontroller), is given to the microcontroller. When we talk about a microcontroller, let us little think what is it made of, of course it is a chip(Integrated Circuit, IC ). Now lets think about how is it made of, its made of transistors with which gates are formed and the whole logic inside the microcontroller or the processor(ALU, timer modules etc) is built. And if you think about what is inside the controller, there are registers and modules. The registers represent the status of the mapped device/module (like timers, ports), it can be the configuration of the device for it to work . So think what will happen if the register content has some junk values when we power the device, the system can perform in an unpredictable way and we wont be able to control the device.

Then how to control it ? Simple solution, starting the device itself initialize these registers with known values. DONE, Now you are in control of the microcontroller when power is given to it. This is the first step they do in any device. First task when the power is given to a microcontroller or processor is to initialize all the registers and all the module inside the chip to a know value and state. But still there is a chance that some registers will be in unknown state, All these are mentioned in the datasheet of that particular IC.

Ok now whats next, how the user going to get control of the controller/processor ? that is where are you going to put your program ? Its not your device  right, so who ever makes the device has to tell us from where the processor going to execute the first instruction  and how it will proceed. Yes that’s the answer the device manufacturer fixes a location in the memory from where it first fetches the instruction and start executing it. This location is called reset vector or power on reset location of the processor or microcontroller. Normally it is at the starting of the memory location, that is 0x0000 of the program memory. Some processor start at the last location also. After that for each instruciton cycle it keeps on executing till power is given to the system, of it goes to sleep mode.

This is how a microcontroller or a processor starts its endless journey to serve all of us without complaining anything :)