ES Lec7
ES Lec7
االنظمة المدمجة
Embedded Systems
Course Code: ELE 413
Theory of operation
The main component of such a timing system is a free
running binary counter.
The counter increments for each incoming timing pulse.
The counter counts continuously from 0 to 2n-1, where n
is the number of bits in the counter.
Since it runs independently, it can count inputs or clock
pulses concurrently while the microcontroller is executing
the main program.
TIMERS AND COUNTERS
Theory of operation
If the input clock to the binary counter has a fixed known
frequency, we can make an accurate measurement of
time interval by counting the pulses from the clock.
For example, if a particular clock’s frequency is 1 MHz
(period 1 µs), and we have counted 3000 pulses on the
clock signal, then the elapsed time is 3000
microseconds.
TIMERS AND COUNTERS
Theory of operation
In microcontrollers, the input clock to the internal timing
system is software selectable.
The microcontroller’s own clock source (which is fixed)
can be selected as the clock input to the free running
counter module of the timing system. In such a case, the
timing system is known to be working as a timer because
it is driven by a fixed known frequency clock.
TIMERS AND COUNTERS
Theory of operation
However, the input clock to the timing system can also be
provided through an external I/O pin of the
microcontroller in which case it is known to be working as
a counter, and will count the external pulses which may
appear at random intervals.
These pulses could be manual inputs from a push button,
or more likely, would be produced by some other signal
source, like a road sensor that generates a pulse every
time a car is passed over it.
TIMERS AND COUNTERS
Theory of operation
A functional structure of a simple timer module is shown
This has a free running 16-bit up counter that increments its
value on each clock pulse. Thus, the 16-bit output count
represents the number of pulses arrived since the counter
was last reset to zero.
We can convert this value into time interval by knowing the
frequency (or period) of the input clock signal.
TIMERS AND COUNTERS
Theory of operation
Suppose, we want to measure the time elapsed between
any two successive events.
Lets assume that when the first event occurs, the timer is
reset to zero, and when the second event occurs, the timer
output is 25000.
If we know that the input clock has period of 1 µs, then the
time elapsed between the two events is 25000×1 µs = 25
milliseconds.
TIMERS AND COUNTERS
Theory of operation
A more advanced timer has a prescaler.
A prescaler is essentially a configurable clock-divider circuit.
Depending on the selected configuration bits, the prescaler
output could be the same as the input signal, or it may have
half the frequency, one-fourth the frequency, one-eighth
the frequency, etc.
TIMERS AND COUNTERS
Fosc is the frequency of the instruction cycle clock, which is the CPU clock divided by 4
(each instruction takes four clock cycles). It is used in many places in the chip such as the
input to a timer.
Timer0 Operation Modes
• Timer Mode:
o In timer mode, Timer0 increments on every instruction cycle
(Fosc/4).
o A prescaler can be applied to divide the clock frequency for
longer delays.
• Counter Mode:
o In counter mode, Timer0 increments based on an external
signal applied to the T0CKI pin.
o The edge of the signal (rising or falling) is selectable via the
T0SE bit.
Interrupts in Timer0
T0SE=0
T0SE=1
T0CS (Timer0 Clock Source Select bit):
1 = Signal present on T0CKI (Timer0 Clock Input) pin (RA4 pin) used as
Timer0 clock source.
These timers run at a speed of 1/4 of the clock speed. So, if we use 4
MHz crystal, the internal timer will run at 1 MHz.
TMR0
0 0 0 0 0 0 0 1 0 1 0 0 0
1/0
according to
prescaler rate
(1) TMR0 As A Timer
TMR0 Over Flow Time Out
When over flow occurs (TMR0 value change from 255→0), the
timer0 interrupt flag bit T0IF (INTCON.T0IF) is set to 1.
Example1:
1
X = = 15.2587 = 16
-6
65536 * 10
The required 1s over flows to get time equal to 1 sec = 16 over flows.
Case2: The initial value of TMR0 =39 (T0 =39)
1 over flow TMR0 Time Out = (256-T0) x prescale x 4 x clock_period =
-6
(256-39) x 256 x 4 x 0.25*10 sec = 55552 μSec
X = 1
= 18
-6
55552* 10
The required 1s over flows to get time equal to 1 sec = 18 over flows.
Example 2:
1.5
X = = 22.89 = 23
-6
65536 * 10
The required 1s over flows to get time equal to 1.5 sec = 23 over flows.
Case2: The initial value of TMR0 =39 (T0 =39)
1 over flow TMR0 Time Out = (256-T0) x prescale x 4 x clock_period =
-6
(256-39) x 256 x 4 x 0.25*10 sec = 55552 μSec
X = 1.5
= 27
-6
55552* 10
The required 1s over flows to get time equal to 1.5 sec = 27 over flows.
Example 3:
f = 4MHz.
OPTION_REG Configuration:
0 0 0 0 0 1 1 1
OPTION_REG = 0x07;
The required 1s over flows to get time equal to 1 sec
• Assume The initial value of TMR0 =0 (T0 =0)
1
X = = 15.2587 = 16
-6
65536 * 10
The required 1s over flows to get time equal to 1 sec = 16 over flows.
void main() {
int Num=0;
TRISB = 0x00; // PORTB is configured to be output
PORTB = 0X55; // Initialize PORTB
OPTION_REG = 0x07; // Prescaler (1:256) is assigned to the timer TMR0
TMR0 = 0; // Timer0 counts from 0 to 255
while(1)
{
if (T0IF == 1) // check if 1 over flow occurs // (INTCON.T0IF == 1)
{
Num ++; // 1 over flow occurs causes Num to be incremented by 1
T0IF =0;
TMR0 = 0;
}
if (Num == 16)
{
PORTB = ~PORTB; // Toggle PORTB every 1 Sec
Num = 0;
}
}
}
void main() {
int Num=0;
TRISB = 0x00; // PORTB is configured to be output
PORTB = 0X55; // Initialize PORTB
OPTION_REG = 0x07; // Prescaler (1:256) is assigned to the timer TMR0
TMR0 = 0; // Timer0 counts from 0 to 255
while(1)
{
while(Num < 16)
{
if (T0IF == 1) // check if 1 over flow occurs // (INTCON.T0IF == 1)
{
Num ++; // 1 over flow occurs causes Num to be incremented by 1
T0IF =0;
TMR0 = 0;
}
}
PORTB = ~PORTB; // Toggle PORTB every 1 Sec
Num = 0;
}
}
EX2: TMR0 As A Counter
OPTION_REG.F5 = 1; // Counter TMR0 receives pulses through the RA4 pin (T0CKI)
OPTION_REG.F3 = 1; // Prescaler rate is 1:1
TMR0 = 0; // Reset timer/counter TMR0
do {
if (TMR0 == TEST) // Does the number in timer match the value stored in TEST?
PORTD.B3 = 1; // (Set RD3 to 1 if the number in timer match the value stored in TEST
}
while (1); // Remain in endless loop
}