A timer is an n-bit register, where the figure is increased by one every clock pulse.
The timer begins counting from 0 and reaches a maximum 2
n -1, then start from scratch again, and so on up to the maximum number (all bits to 1), all the time.
For example, a timer will count 2bit continuously in sequence, four numbers:
0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ....
ie bit:
00, 01, 10, 11, 00, 01, 10, 11 ...
In this case, we say that the resolution of the timer is 2bit.
The count direction can also be from the 'up "" down ", ie the largest number to smallest number.
A number is stored in the registry for a period of time equal to that of a clock period (Tc).
For example, if Tc = 0.25 s, starting with the first zero and the timer register finquando contain the number 3 will be the delay of 4 sec * Tc or 1s. from zero and the next until the second number three will again be used 1s and so forth, for the third three ... the behavior is so regular.
The timer overflow, it passes the maximum number rappresentable (register with all bits to 1) to zero.
A possible representation of the example is as follows:
In general, the number N which corresponds to a time interval T, as the clock time Tc is given by:
T = (N + 1) * Tc
There's the "+1" because even zero "pledge" a clock cycle.
We can use this period of 1s in our program, for example, to make every second operation: a LED on or off or make a clock. Examples of pseudocode:
Each sec: {
Turn the LED on pin PortB.2 if it is off, turn it off if it is switched back
}
or
Each sec:
{seconds = second + 1;
if (seconds = 60) then:
{minutes = minutes + 1;
seconds = 0;}
if (minutes = 60) then: {hour = hour + 1; minutes = 0; seconds = 0;}
if (hours = 24) then:
{hours = 0; minutes = 0;
seconds = 0;}
}
This mode of operation of the timer, when it counts from 0 up to the maximum value given by the number of bits, is generally referred to as "normal
" and inflexible as a "strictly" related to the number of bits and the clock frequency, you can not change that at will ever.
In a microcontroller, a timer is a set of hardware circuits that operate separately from the execution software, this allows for significant efficiency, accuracy and scalability of the times generated.
A timer can be used not only to schedule events
output, as the examples mentioned above, but also to count the input events : how many times you pressed a button, how much time elapsed between a start and a stop of a clock, the frequency of a signal, the duration of the pulse or the duty cycle of a square wave signal.
In this case, ie when the clock pulse comes from the outside world, the name is more appropriate to counter
. Therefore, when we talk about literature without distinction of timer / counter a secondo del particolare contesto d'utilizzo, se per creare tempi (out) o per contare eventi (in) Un AVR ATmegax8 (88 168, 328) mette a disposizione del programmatore tre timer interni, denominati:
Timer0, da 8 bit
Timer1, da 16 bit Timer2, da 8 bit
I registri che contengono il dato corrente del timer, sono indicati rispettivamente con:
TCNT0
TCNT1
A fianco del registro dato, lavorano altri registri (di comparazione, di controllo), che consentono di gestire il funzionamento del timer.
0 comments:
Post a Comment