21.08.2013 Views

Embedded Systems Design with the Atmel AVR Microcontroller Part II

Embedded Systems Design with the Atmel AVR Microcontroller Part II

Embedded Systems Design with the Atmel AVR Microcontroller Part II

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6.4. APPLICATIONS 143<br />

be computed by computing <strong>the</strong> time difference between <strong>the</strong> two captured event times, and, based on<br />

<strong>the</strong> clock speed of <strong>the</strong> microcontroller, <strong>the</strong> programmer can compute <strong>the</strong> actual time changes and<br />

consequently <strong>the</strong> frequency of <strong>the</strong> signal.<br />

In many cases, a microcontroller can’t afford <strong>the</strong> time to poll for one event. Such situation introduces<br />

<strong>the</strong> second method: interrupt systems. Most microcontroller manufacturers have developed<br />

built-in interrupt systems <strong>with</strong> <strong>the</strong>ir timer input modules. Instead of continuously polling for a flag,<br />

a microcontroller performs o<strong>the</strong>r tasks and relies on its interrupt system to detect <strong>the</strong> programmed<br />

event. The task of computing <strong>the</strong> period and <strong>the</strong> frequency is <strong>the</strong> same as <strong>the</strong> first method, except<br />

that <strong>the</strong> microcontroller will not be tied down to constantly checking <strong>the</strong> flag, increasing <strong>the</strong> efficient<br />

use of <strong>the</strong> microcontroller resources. To use interrupt systems, of course, we must pay <strong>the</strong> price by<br />

appropriately configuring <strong>the</strong> interrupt systems to be triggered when a desired event is detected.<br />

Typically, additional registers must be configured, and a special program called an interrupt service<br />

routine must be written.<br />

Suppose that for an input capture scenario <strong>the</strong> two captured times for <strong>the</strong> two rising edges are<br />

$1000 and $5000, respectively. Note that <strong>the</strong>se values are not absolute times but <strong>the</strong> representations<br />

of times reflected as <strong>the</strong> values of <strong>the</strong> free running counter.The period of <strong>the</strong> signal is $4000 or 16384<br />

in a decimal form. If we assume that <strong>the</strong> timer clock runs at 10 MHz, <strong>the</strong> period of <strong>the</strong> signal is<br />

1.6384 msec, and <strong>the</strong> corresponding frequency of <strong>the</strong> signal is approximately 610.35 Hz.<br />

6.4.2 COUNTING EVENTS<br />

The same capability of measuring <strong>the</strong> period of a signal can also be used to simply count external<br />

events. Suppose we want to count <strong>the</strong> number of logic state changes of an incoming signal for a given<br />

period of time. Again, we can use <strong>the</strong> polling technique or <strong>the</strong> interrupt technique to accomplish<br />

<strong>the</strong> task. For both techniques, <strong>the</strong> initial steps of turning on a timer and configuring a physical input<br />

port pin are <strong>the</strong> same. In this application, however, <strong>the</strong> programmed event should be any logic state<br />

changes instead of looking for a rising or a falling edge as we have done in <strong>the</strong> previous section. If<br />

<strong>the</strong> polling technique is used, at each event detection, <strong>the</strong> corresponding flag must be cleared and<br />

a counter must be updated. If <strong>the</strong> interrupt technique is used, one must write an interrupt service<br />

routine <strong>with</strong>in which <strong>the</strong> flag is cleared and a counter is updated.<br />

6.4.3 OUTPUT COMPARE—GENERATING TIMING SIGNALS TO INTER-<br />

FACE EXTERNAL DEVICES<br />

In <strong>the</strong> previous two sections, we considered two applications of capturing external incoming signals.<br />

In this subsection and <strong>the</strong> next one, we consider how a microcontroller can generate time critical<br />

signals for external devices. Suppose in this application, we want to send a signal shown in Figure 6.3<br />

to turn on an external device.The timing signal is arbitrary but <strong>the</strong> application will show that a timer<br />

output system can generate any desired time related signals permitted under <strong>the</strong> timer clock speed<br />

limit of <strong>the</strong> microcontroller.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!