MCU internal workings

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Hello

I don't know but it seems to me that every common microcontroller has CPU, Memory and peripherals devices such as ADC, timers, UART, SPI, I2C. When the program is executed on the microcontroller, the CPU sends instructions to the peripherals as written in the program.

I don't understand what is CPU processing time. Does it mean how long does it take for the CPU to execute an instruction?

What is the processing time if the PIC microcontroller is running with 20Mhz?
 

nsaspook

Joined Aug 27, 2009
13,086
https://docs.scinet.utoronto.ca/index.php/CPU_time
CPU time (or processing time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system, as opposed to elapsed time, which includes for example, waiting for input/output (I/O) operations or entering low-power (idle) mode. The CPU time is measured in clock ticks or seconds. Often, it is useful to measure CPU time as a percentage of the CPU's capacity, which is called the CPU usage.

Programs and applications normally do not use the processor 100% of the time that they are running; some of that time is spent on I/O operations and fetching and storing data on the RAM or storage device. The CPU time is only when the program actually uses the CPU to perform tasks such as doing arithmetic and logic operations.
Look at the Linux time command for an example of how a OS typically tracks 'time'.

https://www.cloudsavvyit.com/11954/how-to-time-a-linux-program/
The Linux time Tool
Given that user and sys times report CPU time only, whereas real time reports actual wall clock time, it is (thus) very common to see the time tool return output where a combination of user + sys does not equal real time. An example can be seen when timing sleep:


1644683512348.png


1644683577726.png
 

MrChips

Joined Oct 2, 2009
30,720
Hello

I don't know but it seems to me that every common microcontroller has CPU, Memory and peripherals devices such as ADC, timers, UART, SPI, I2C. When the program is executed on the microcontroller, the CPU sends instructions to the peripherals as written in the program.

I don't understand what is CPU processing time. Does it mean how long does it take for the CPU to execute an instruction?

What is the processing time if the PIC microcontroller is running with 20Mhz?
That is correct.

Every CPU has a basic clock cycle. Some MCUs will execute an instruction on every clock cycle. Others may require a number of clock cycles in order to execute an instruction. Some instructions take more than one cycle to execute.

A 20MHz has a clock period of 50ns. Hence one cycle takes 50ns.

Most instructions on a Microchip PIC16F877 MCU will take one cycle to execute. Some instructions require two cycles.

You can find this information on every MCU by reading the manufacturer's datasheet.
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Can you tell what is describe below
Programs and applications normally do not use the processor 100% of the time that they are running;
I think when the program is executed on the microcontroller, the CPU provides service to the peripherals. It means 100% of CPU time will be used by the program.
 

AlbertHall

Joined Jun 4, 2014
12,345
Most instructions on a Microchip PIC16F877 MCU will take one cycle to execute. Some instructions require two cycles.
From the data sheet for this (and many other PICs):

All instructions are executed within one single instruc-
tion cycle
, unless a conditional test is true or the pro-
gram counter is changed as a result of an instruction.
In this case, the execution takes two instruction cycles
with the second cycle executed as a NOP. One instruc-
tion cycle consists of four oscillator periods. Thus, for
an oscillator frequency of 4 MHz, the normal instruction
execution time is 1 µs.
 

MrChips

Joined Oct 2, 2009
30,720
Can you tell what is describe below


I think when the program is executed on the microcontroller, the CPU provides service to the peripherals. It means 100% of CPU time will be used by the program.
Programs and applications normally do not use the processor 100% of the time that they are running;
This is misleading.

What the writer probably meant is that most of the time the CPU is not doing anything useful, such as while waiting for input from the keyboard.

As long as the processor is running it is executing an instruction, even if it is in a "do nothing" loop.
The only time the processor is not running is when it is in a low power mode such as IDLE, SLEEP, POWER DOWN, HALT or STOP mode.
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
This is misleading.

What the writer probably meant is that most of the time the CPU is not doing anything useful, such as while waiting for input from the keyboard.
I do not understand how the CPU does not do anything useful. Please describe example.

If it is written in the code that the program uses the CPU to read the keyboard input then the CPU will be used to read the keyboard input.
 

drjohsmith

Joined Dec 13, 2021
852
Hello

I don't know but it seems to me that every common microcontroller has CPU, Memory and peripherals devices such as ADC, timers, UART, SPI, I2C. When the program is executed on the microcontroller, the CPU sends instructions to the peripherals as written in the program.

I don't understand what is CPU processing time. Does it mean how long does it take for the CPU to execute an instruction?

What is the processing time if the PIC microcontroller is running with 20Mhz?
where are you learning from ?
 

nsaspook

Joined Aug 27, 2009
13,086
I do not understand how the CPU does not do anything useful. Please describe example.

If it is written in the code that the program uses the CPU to read the keyboard input then the CPU will be used to read the keyboard input.
Think about baking bread as an analogy to your example. You could watch every second until the bread is done or only check the bread once at the correct done time. Usefully spent processing time is relative on what else needs to be done between the start and end of the baking process.
 

MrChips

Joined Oct 2, 2009
30,720
Here are two examples of blocking wait loops:

Example #1

delay = 1000;
while (delay--);

Example #2

while (no_input_character_received);

Both loops prevent the rest of the program from doing useful processing.
 

nsaspook

Joined Aug 27, 2009
13,086
I would hope the TV remote is in a power save mode with the CPU instruction clock stopped (doing NO work) and most functionality halted until a button pressed interrupt brings it back to CPU life.
 

BobTPH

Joined Jun 5, 2013
8,813
I would hope the TV remote is in a power save mode with the CPU instruction clock stopped (doing NO work) and most functionality halted until a button pressed interrupt brings it back to CPU life.
Of course it is, which is doing no useful work.

Bob
 

nsaspook

Joined Aug 27, 2009
13,086
No useful work and no work are obviously different in the energy requirements. No work is an energy saver functionality commonly used to replace no useful work idle routines in low-power devices.
 
Last edited:

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Here are two examples of blocking wait loops:

Example #1

delay = 1000;
while (delay--);

Example #2

while (no_input_character_received);

Both loops prevent the rest of the program from doing useful processing.
From this it is understood that we should write a program in such a way that one process cannot block another process.
Example # LED has to be turn on-off every 500 ms.

As soon as the system is started, the CPU sends instructions to turn ON the LED. After that the CPU waits for 500 ms and then sends the instruction to turn OFF the LED.

I think the main point to understand is that the CPU only needs to turn the LED ON and OFF, in the delay time, the CPU can execute other process.
 

ericgibbs

Joined Jan 29, 2010
18,766
Hi Dadu.
In the Arduino Series of MCU's they have a 'millis' function, which enables the program to calculate the required time delay period for an operation, without halting the main program sequence.

E
 
Top