# Timers- Calculating Time period [SOLVED]

#### Pushkar1

Joined Apr 5, 2021
294
I understand a few things about timers in microcontrollers but I still don't fully understand timers. I would really appreciate any help understanding timers. I have spent about four to five hours on the internet to fully understand timers but still i am not getting things clearly.

Till now I understood that any microcontroller has a timer which is used to get the specific time. For example, if 12 ms is required, so timer can be configured for it.Timers can be of different size e.g. 8 bit timer, 16 bit time and 32 bit timer. Timer model has timer control register and timer register that also need to be configured

I am trying to understand how the specific time calculated for a timer. So what are main points we should remember to calculate any time period?

Does it make sense to calculate the time period?

Timer Mode : 8 bit timer
Clock Source : 5 MHz
Pre-scaler : 1/4
Timer Start : 100

How much specific time it will get?

Last edited:

#### ericgibbs

Joined Jan 29, 2010
13,825
How much specific time it will get?
hi P,
What do you make the Timer0 rollover interval, [ FF > 00 ]with those settings.?

E

Update:
Or what number of internal Timer 'counts' will be required to roll over.??

Last edited:

#### Pushkar1

Joined Apr 5, 2021
294
Update:
Or what number of internal Timer 'counts' will be required to roll over.??
A basic 8-bit timer will start from 0 to 255. This will rollover from 0 to 255. I would preset the timer to 100. It will start at 100. It will rollover to 255.

256 - 100 = 156 Timer will repeat every 156 times without Prescale.

This is my understanding, will it happen

#### ericgibbs

Joined Jan 29, 2010
13,825
Hi P,
Sounds close enough. Next point Clock Source : 5 MHz

Which clock does this refer too.?

E
Hint: ref repeat.

C:
[CODE=clike]void interrupt(){      //This function is called on each interrupt

if (TMR0IF_bit) {   //Check if Timer0 has caused the interrupt

LATB = ~LATB;    //Toggle PORT to generate square wave

TMR0IF_bit = 0;         // clear interrupt flag

TMR0L = 98;             // Preload timer0

}

Last edited:

#### Pushkar1

Joined Apr 5, 2021
294
Hi P,
Sounds close enough. Next point Clock Source : 5 MHz

Which clock does this refer too.?
I think so because i choose autoreload option in setting. is something wrong?

So only two options are available in the setting, internal and external clock source. I am assuming that only 5 MHz of internal clocks are being sourced at the input of the timer.

Update :

Timer Mode
: 8 bit timer
Clock Source : 5 MHz
Pre-scaler : 1/4
Timer Start : 100
Clock : Internal clock

Last edited:

#### ericgibbs

Joined Jan 29, 2010
13,825
hi P.
I was aiming to draw your attention to the requirement that the Timer has to be reloaded, without the 100 count reload, the next count would be 256.

So when using say PIC MCU with a internal Timer0 Clock Source : 5 MHz , what would the MCU clock frequency be.??

E

Update:
What Time period would a single cycle of a frequency of 5,000,000 divide by 4 be.= .....
and what is the total time, if that period if multipled by 156 = .....

Last edited:
• Pushkar1

#### Pushkar1

Joined Apr 5, 2021
294
So when using say PIC MCU
Why only PIC?

I thought if we knew the size of the timer, the prescale value, and the clock source frequency for the timer, we could calculate specific time period. There is know need to clock of particular microcontroller. Am i creating misunderstandings in my mind?

#### ericgibbs

Joined Jan 29, 2010
13,825
hi P,
It was for your personal information database, a PIC MCU divides the crystal clock by 4, so a 5MHZ internal clock would mean a crystal of 20MHz.

So what is the answer to this part:
Update:
What Time period would a single cycle of a frequency of 5,000,000 divide by 4 be.= .....
and what is the total time, if that period if multipled by 156 = .

#### Pushkar1

Joined Apr 5, 2021
294
hi P,
It was for your personal information database, a PIC MCU divides the crystal clock by 4, so a 5MHZ internal clock would mean a crystal of 20MHz.
As far as I know, PIC works on CISC architecture whereas ARM works on RISC architecture?

I am trying to understand peripheral of ARM microcontroller. Therefore it would not be beneficial to read the architecture of the pic microcontroller.

May be my question is, what do we need to know to find out the time period? I have already shown some e.g size, prescale, input clock source

Last edited:

#### ericgibbs

Joined Jan 29, 2010
13,825
OK.

Are you able to answer this.

Update:
What Time period would a single cycle of a frequency of 5,000,000 divide by 4 be.= .....
and what is the total time, if that period if multiplied by 156 = .

• Pushkar1

#### Pushkar1

Joined Apr 5, 2021
294
What Time period would a single cycle of a frequency of 5,000,000 divide by 4 be.= .....
and what is the total time, if that period if multiplied by 156 = .
1 clock period = 1/ input frequency 5 MHz = 0.0000002 us

Divide by 4 Prescale
0.0000002 us / 4 = 0.00000005 us

156 cycle * 0.00000005 = 0.0000078 us

Have I got the correct period?

#### ericgibbs

Joined Jan 29, 2010
13,825
hi P,
Try again.
The 4 divider divides the Clock input Freq to the Timer NOT the Period.

E

#### Attachments

• Pushkar1

#### MrChips

Joined Oct 2, 2009
24,397
1 clock period = 1/ input frequency 5 MHz = 0.0000002 us

Divide by 4 Prescale
0.0000002 us / 4 = 0.00000005 us

156 cycle * 0.00000005 = 0.0000078 us

Have I got the correct period?
No. You are wrong by a factor of 1000000.

• Pushkar1

#### Pushkar1

Joined Apr 5, 2021
294
hi P,
Try again.
The 4 divider divides the Clock input Freq to the Timer NOT the Period.
I did it
Frequency into the 8 bit timer = 5000000 Hz /4 (prescaler) = 1,250, 000Hz

1 clock period = 1/ 1,250, 000Hz= 8e-7 us

156 cycle * 8e-7 us = 0.000 1248 us

#### ericgibbs

Joined Jan 29, 2010
13,825
hi P,
That is what I calc it to be.
Do you now understand the method.?

Usually the Count roll over would trigger an ISR.

E

• Pushkar1

#### Pushkar1

Joined Apr 5, 2021
294
Do you now understand the method.?
@ericgibbs Thank you for help. I now fully understand how specific time can be calculated and timer interrupt

#### MrChips

Joined Oct 2, 2009
24,397
I did it
Frequency into the 8 bit timer = 5000000 Hz /4 (prescaler) = 1,250, 000Hz

1 clock period = 1/ 1,250, 000Hz= 8e-7 us

156 cycle * 8e-7 us = 0.000 1248 us
You are still making the same error.

1 / 1MHz = 1μs = 0.000001s

• Pushkar1

#### Pushkar1

Joined Apr 5, 2021
294
You are still making the same error.

1 / 1MHz = 1μs = 0.000001s
1 clock period = 1/ 1,250, 000Hz= 8e-7 s

156 cycle * 8e-7 s = 0.000 1248 s

• ericgibbs

#### trebla

Joined Jun 29, 2019
457
As far as I know, PIC works on CISC architecture whereas ARM works on RISC architecture?
ARM, PIC, AVR and MIPS (PIC32) MCUs are all RISC architecture.
8051 based MCUs are CISC architecture.

• Pushkar1
• 