The working of a microcontroller is sequential. It performs one step after another. Below, I have mentioned the stepwise process and have tried to keep the language lucid. The image below shows the 8051 architecture so that you can pictorially relate the different process.
- When the power supply is turned ON, the oscillator is enabled and within few milliseconds, the crystal circuit causes the voltage across it to oscillate.
- When the frequency of oscillator becomes stable, the process of writing bits on special function registers (SFRs) starts. SFR’s are special registers (small memory locations) which are closely coupled to the CPU. All the vital information needed for the synchronized working and control of the microcontroller is stored here. Everything occurs according to the clock of the oscillator and overall electronics starts working.
- PC or Program counter is reset to zero address of the program memory (ROM/Flash memory). Then the address of the instruction is sent to Instruction Decoder which decodes the instructions and thus executes them. PC is a register in a computer processor that contains the address (location) of the instruction being executed at the current time.
- All the initializations of the different peripherals and microcontroller components take place and the required global variables are loaded in the RAM. This is why certain software and games in your PC require more RAM. The game or software needs to generate more variables while processing the data. When the RAM size is limited, the game or software keeps on hanging, as there is no space for the CPU to pop variables.
- The Instructions are fetched, decoded and executed with the help of Instruction Set Architecture.
- After execution of each instruction, the address in the Program Counter is incremented by 1. So the next instruction is called and the same process continues.
- When the program encounters a subroutine/Interrupt, the execution of the program jumps to a new address which points towards the instructions that the subroutine contains.
- The variables required for the subroutine are popped in the stack.
- After the execution of all the instructions of the subroutine, the variables from the stack memory are flushed. The program counter returns to the earlier address from which it was diverted, and the controller continues to run the main program.
Memory Leakages mainly occur when the variables created are not properly flushed or destroyed from the RAM. Thus, your RAM keeps on generating new variables without destroying the unused ones, till the RAM is completely filled. Ultimately the system crashes or hangs.
You could certainly determine the time, a microcontroller would take to execute an instruction by following certain calculations. Noting the frequency of operation of the CPU and the number of cycles per instruction (can get this info from datasheet), you can estimate the time it takes to execute that instruction and accordingly your whole program. This was accurately possible until controllers started using high-tech computer architecture which does multiple pipelining, branch predictions etc. So you cannot ascertain the time, which was possible in 8051 architecture.
The working explanation is very superficial and the steps may vary a little from controller to controller. This is just to give you an overview of how things happen at the very small. I will be intensifying the content as we move on.