Delays:Software delay

Software delay
Context

● You are developing an embedded application using one or more members of the 8051 family of microcontrollers.

● You are designing an appropriate software foundation for your application.

Problem

How do you create a simple delay without using any hardware (timer) resources?

Background

See HARDW ARE DELA Y [page 194] for background information.

Solution

Suppose we want to flash the LEDs connected to Port 1 on an 8051 microcontroller with a two second cycle time (so that they are on for 1 second then off for 1 second, ad infinitum). The basic program structure we require could be based on a SUPER LOOP [page 162] as follows:

Delays-0198

We could then measure the pulse frequency we obtained, using an oscilloscope or a software simulator. If we found that these delays were not long enough, we could easily extend them by adding additional layers, as shown in Longer_Loop_Delay()

Delays-0199

Hardware resource implications

Unlike HARDW ARE DELA Y [page 194], SOFTW ARE DELA Y uses no timer resources. Note, however, that the CPU time spent in the delay calculation is wasted: using a sched- uler (see Chapter 13) can, in many circumstances, avoid the waste of CPU time.

Reliability and safety implications

Software delays are not suitable for use in applications where precise timing is required.

Portability

Software delays can be used even on a microcontroller / microprocessor without a built-in timer. However, the precise delay duration obtained varies (enormously) with differences in hardware and software.

Overall strengths and weaknesses

SOFTW ARE DELA Y can be used to produce very short delays.

SOFTW ARE DELA Y requires no hardware timers.

SOFTW ARE DELA Y will work on any microcontroller.

It is very difficult to produce precisely timed delays.

The loops must be returned if you decide to use a different processor, change the clock frequency or even change the compiler optimization settings.

Related patterns and alternative solutions

In most circumstances, it is better to avoid using delays at all: see CO OPERA TIVE SCHED ULER [page 255] for a delay-free alternative that will work in many circumstances.

If you do require delays then HARDW ARE DELA Y [page 194] is often a better alternative.

Example: Creating a 5 µs delay in an I2C library

As we discuss in Chapter 23, software implementation of the I2C serial protocol can require small delays. In Listing 11.5 we illustrate how a delay of around 5 µs can be created in software for use in such a library.

Delays-0200

Listing 11.5 Creating a very short delay

Example: Flashing an LED

We will repeat the ‘flashing LED’ example used in HARDW ARE DELA Y [Page 194] to illustrate the SOFTW ARE DELA Y pattern.

To control the flashing of the LED, we will use an endless loop involving one SOFT W ARE DELA Y and a ‘flash LED’ function.

We assume that we are using a Standard 8051 device with 12 oscillations per instruction cycle and an oscillator frequency of 12 MHz. (see Chapter 4 for further details.)

The key files required in the project follow (Listings 11.6 to 11.11). As usual complete set of files are included on the CD.

Delays-0201Delays-0202Delays-0203Delays-0204Delays-0205Delays-0206Delays-0207

Leave a comment

Your email address will not be published. Required fields are marked *