21 Interfacing External Peripherals to the 8051 – The x86 Microprocessors: 8086 to Pentium, Multicores, Atom and the 8051 Microcontroller, 2nd Edition

21

Interfacing External Peripherals to the 8051

IN THIS CHAPTER, YOU WILL LEARN

  • How to use external interfaces like keyboards, LCDs, LEDs, ADCs, DACs and motors as interfaces to 8051
  • The necessity of using external chips for motor interfacing
  • The principle used in DC motor control

21.1 | Interfacing ADCs and DACs to 8051

21.1.1 | Interfacing an Analog to Digital Converter to 8051

Our interest is to interface an ADC to an 8051 MCU using Port 2 as the data lines, and some pins of Port 1 for the control signals needed by the ADC. When an analog voltage is given as an input to an ADC, it gets converted to a digital number that is transferred to the 8051. The digital value can be stored in the RAM of the system and may be displayed or used in further computations.

In Section 9.8, the details of the ADC0808/ADC0809 have been covered in detail and that must be referred before any interfacing program is done. The same ADC is utilized here in a similar manner. Figure 21.1 shows the pin connections between the ADC and an 8051.

Figure 21.1 | Connections between the ADC and the 8051 port pins

The salient points regarding the connection are as follows:

  1. Port 2 is used in the input mode to get the converted digital data from the ADC to 8051.
  2. Port pins P1.7, P1.6 and P1.5 are used in the output mode as the address selection pins A, B, C of the ADC.
  3. Port pin P1.0 is used as ALE. Hence, it is considered to be an output pin.
  4. Port pin P1.1 is used to give the start conversion (SC) pulse to the ADC. Hence, it is to be an output pin.
  5. Port pin P1.2 is used in the input mode to receive the End of Conversion (EOC) signal from the ADC.
  6. Port pin P1.3 is used as OE for the ADC. It is defined as an output pin.

Example 21.1

Write a program to interface the ADC 0808/0809 to the 8051.

Solution

Refer to Fig. 21.1. Further, refer to the timing diagram in Fig. 9.21 (Chapter 9) to understand the following steps of the program:

  1. First, the port bits are ‘named’ by using the BIT and EQU directives. This makes the program more readable.
  2. The pins for EOC and ADC_DAT are made to act as input pins, by sending a ‘1’ to them.
  3. Then, the signals ALE, SC and OE are all cleared.
  4. IN0 is selected as the analog input. For that A, B and C are made ‘000’.
  5. To latch this selection of the analog channel address, ALE is made high for a short time (check the amount of delay required from the ADC data sheet).
  6. To start conversion, SC is made high for a short time.
  7. After this delay, both ALE and SC are cleared.
  8. Then, the EOC signal is tested. If it is low, it indicates that ‘conversion’ is over. After that, OE is made high (for enabling the data output lines of the ADC), and the data is inputted to Port 2 of the 8051.
  9. The last thing done is clearing OE.
  10. This sequence is continually repeated for the conversion of the next analog data.

Note While running the program, ‘N’ in the ‘delay’ procedure should be given a value.

21.2 | Interfacing a Digital to Analog Converter (DAC) to 8051

Converting a digital number is an important application in electronics. You must have learned the basic methods used in this conversion. Here, we will use a DAC chip and connect it to the 8051. A digital number sent from the 8051 is converted to an analog current/voltage by this arrangement.

Section 9.9 of this book covers the details of a typical DAC, i.e., DAC 0800. The technical details of the chip and the conversion method and calculations (Example 9.10) are to be read from Section 9.9. Now refer to Fig. 21.2. that shows the connection between the DAC and Port 2 of 8051.

Figure 21.2 | Connecting the DAC 0800 to 8051

Example 21.2

Generate a triangular waveform using the DAC 0800.

Solution

The method is to increment the data outputted to the DAC, from 0 to FFH. On conversion to analog form, it will give a gradually increasing output from 0 to the maximum output voltage. Then, decrement the number from FFH to 0. This will give the high to low triangular transition.

Note As the clock frequency of the 8051 is reasonably high, the time for incrementing the number 0 to FFH and decrementing from FFH to 0 will be quite small. Thus, the waveform generated will have a very short duration. To obtain a slower increase and decrease in analog voltage, a good idea will be to have a small delay after each increment or decrement of the digital number. The delay must be small, such that steps are not seen in the increasing or decreasing portions of the analog voltage. The amount of this small delay can also be used to control the periods of the generated waveforms. This has been done in this program.

Example 21.3

Generate a staircase waveform with five steps.

Solution

A staircase waveform has steps in the increasing portion of the analog voltage. Once the voltage reaches the maximum value, it falls to zero in zero time. Here, we are to generate a staircase waveform with five steps. After each step, a delay is called. The method is as follows.

Divide the total range of 0 to 255 into five parts. Each increment corresponds to 51.
The digital number range for each step is 0, 51, 102, 153, 204 and 255. These numbers are converted to equivalent value analog voltages. The time period for each step is decided by the number loaded into R3 in the delay program. The program is as follows, and the output waveform is in Fig. 21.3

Figure 21.3 | Output waveform

21.3 | Interfacing with LCD Displays

21.3.1 | Liquid Crystal Displays (LCDs)

The technical details of LCD displays have been dealt with in Section 9.10. Please refer to that before reading the following discussion. Now, let us do some display activities using a 16 × 2 LCD. Refer Fig. 21.4 for the connections between the LCD module and 8051. Data and commands are sent from the ports of 8051. Port 1 is used as the data port, and pins P2.1 and P2.2 are used for RS and E.

  1. VSS and R/W are connected to ground.
  2. VCC is connected to 5 V supply.
  3. VEE is connected through a 10 K pot to the supply for contrast adjustment.
  4. RS is connected to P2.0 and E is connected to P2.1.
  5. Pins 7–14 (DB0 to DB7) of the LCD module are connected to Port 1 of 8051.
  6. Pins 15 and 16 of the LCD are used for backlight adjustment (not shown in Fig. 21.4).

Backlight

There is a lamp here instead of reflected light. If backlighting is provided by LEDs as in the case of many 16 × 2 LCDs, connect pin 16 to ground and pin 15 to Vcc through a 100 Ω resistor.

Getting a Character Displayed on the LCD

To display characters on the LCD, the ASCII value of the character should be sent to the data register. Therefore, before sending the data, appropriate control signals should be activated by giving the required logic levels on the port pins. Further, first the LCD is initialized, and cleared; and then, the cursor is positioned. This is done by sending command words to the LCD command register (see Table 9.6).

Algorithm

  1. Send commands words to Port 1. Some important command words are 38 H (initializing LCD), 0EH (making the LCD and cursor ON), 01 (clearing the screen), 06 (shifting the cursor right) and 80 H (moving the cursor to line 1, position 1).
  2. Make RS = 0 (by clearing P2.1) for selecting the command register.
  3. Make R/W = 0 to write to LCD (if this line is grounded as in Fig. 21.4, this step can be skipped).

    Figure 21.4 | Connecting the LCD module to the pins of 8051

  4. Send an H to L pulse at the E pin to complete the writing. For this, make P2.2 high for a short while and then clear it.
  5. With this, the writing of commands is over. Now, the required data must be written.
  6. Make RS = 1 for selecting the data register.
  7. Repeat steps 3 and 4.

In this setup (see Fig. 21.4), one whole 8-bit port was used up for LCD data. To save on pins, it is possible to use LCDs with just 4 data pins of a port. The data and command words are sent as in the previous case, but the method here is to send the 8 bits as two nibbles — thus only four lines of an MCU port need to be used. Figure 21.5 shows that only 7 port pins are needed in total to connect an LCD module to an MCU.

Figure 21.5 | A 4-bit LCD interface

Example 21.4

This program displays ‘YA’ on the LCD. The generation of control signals necessary for sending the commands to the command register are put in a procedure named ‘COMMAND’. Similarly, there is a procedure named ‘DAT’ that takes care of the control signals necessary for sending to the data register, and the characters that are to be displayed.

Figure 21.4 uses all the eight bits of Port 1 as the data bus. Note that first the LCD is initialized, then cleared and then the cursor is positioned. This is done by sending command words to the LCD command register. For latching these words to the command register, RS has to be made low, and E has to be given a high to low pulse. These are done in the COMMAND procedure.

Similarly, for displaying a character, the ASCII value of the data is to be sent to the data register and RS should be made ‘1’. Further, a high to low transition should occur at E. These issues are taken care of in the DAT procedure.

Note The ‘delay procedure’ provides the necessary time for latching data onto the LCD. The data sheet of the LCD should be checked and the exact delay should be given by loading appropriate numbers in the registers used in the delay routine.

21.4 | Interfacing with Light Emitting Diode Displays

21.4.1 | Light Emitting Diodes

In Section 9.13, Light Emitting Diodes (LEDs) have been discussed in great detail. Now, let us use this concept in a system in which an 8051 handles a dynamic display
(see Fig. 21.6). This is an four-digit display of the common cathode type. The ports of 8051 are used in such a way that Port 1 supplies the digit information and Port 2 supplies the segment information. Digit information through Port 1.0 to P1.3 is to select which digit is being activated at a particular time. For segment information, the seven segment code of each digit should be sent as a byte through Port 2.

Figure 21.6 | A dynamic display for an 8051-based system

Figure 21.7 shows the complete set up. Four pins of Port 1 are used for ‘digit driving’. These pins are connected to the bases of the four transistors Q1 to Q4. At a time, only one particular transistor is to be ON. These are PNP transistors and are turned ON if a ‘0’ is applied to the bases. This ‘0’ goes to the emitters of the transistor that is connected to the common cathode of the segment LEDs, of a particular digit. At a time, Port 1 gives a ‘0’ only on one of its port lines. To understand this clearly, observe Fig. 21.7. The most significant digit (or the left most digit of the display) is activated time, the segment information for displaying the left-most digit should be placed on Port 1. If Port 2 gives the data 77 H, the first digit displays ‘A’.

Figure 21.7 | A four-digit dynamic display using the 8051

This technique is to be repeated for all digits continuously. The steps are as follows:

  1. Select the first digit to be displayed and send a suitable logic through P1.0 to P1.3 to activate a digit.
  2. Send the segment code through Port 2.
  3. Call a delay of, say, 3 ms.
  4. Repeat this sequence for all four digits.
  5. Then, start again from the first step.

With 4 digits and 3 ms delay, we can get back to the first digit every 12 ms.

This corresponds to a refresh rate of around 83 times per second, which is sufficient to fool the eye into believing that all the digits are ON at the same time [The persistence of human vision is (1/16) of a second (62.5 ms)].

21.5 | Hex Keyboard Interfacing

In Section 9.12, the principle of operation of a hex keyboard has been discussed. With that background, here, we will go on to understanding how such a keyboard is interfaced to 8051.

See the diagram of the hex keyboard. Fig. 21.8 is a 4 × 4 matrix keyboard connected to the 8051. For our purpose, we will use two ports of the 8051: Port 2 as an output port connected to the row lines, and Port 1 as an input port connected to the column lines. Only the lower 4 bits of the two ports are needed. See Fig. 21.8 for the connection, with the ‘key’ positioned at the interconnection of a row and a column. Only when the key at a junction is pressed, a path is established between the corresponding row and column. The program in Example 21.5 finally displays the pressed key at port P0 to which a display (a set of LEDs, for instance) is set up

Figure 21.8 | A hex keyboard connected to the port pins of 8051

Note In the display program, the value of N1 and N2 are to be calculated for the clock frequency of the 8051 being used. A delay of 10 to 20 ms is recommended.

Example 21.5

21.6 | Stepper Motor Interfacing

This topic has been covered in great detail in Section 9.11 and readers are advised to read that to understand the way by which a stepper motor is controlled by a sequence of pulses. Here, we discuss the use of a stepper motor as an interface to the 8051. We can run a motor using a sequence generated by the 8051. However, the motor cannot be driven directly from its port pins, because the motor requires a current much more than the current supplied by the MCU. (The exact current requirement depends on the specifications of the particular motor being used.) As such, current drivers are needed between the 8051 port lines and the leads of the motor. Transistors with high current capability (e.g. Darlington pair or power transistors) can be used. Besides this, there are special motor driving ICs available. One such IC is the ULN 2003 driving IC whose pin diagram is shown in Figure 9.32. Figure 21.9 shows the 8051 MCU generating a sequence for energizing a stepper motor, with the IC ULN 2003 being used to raise the current level. Four pins of Port 1 have been used for sending the driving sequence to the motor. Now, Example 21.6 shows how the stepper motor of Fig. 21.9 is rotated clockwise continuously (refer Example 9.16 as well, for a better understanding)

Figure 21.9 | Connections between 8051 and the stepper motor through a current driver

Example 21.6

21.7 | DC Motor Interfacing

This is a type of motor that operates on direct current and is very commonly used in embedded systems, when continuous movement is needed. The movement may be made ‘controlled’, in the sense that the speed and direction can be changed as per the requirements of the application. Robotics is an area where DC motors are widely used, but this is not the only application. Any type of movement is possible to be achieved with DC motors.

The DC motor has two basic parts: the rotating part that is called the armature and the stationary part called the stator that includes coils of wire called the field coils.

The armature is made of coils of wire wrapped around the core, and the core has an extended shaft that rotates on bearings. The ends of each coil of wire on the armature are terminated at one end of the armature. The termination points are called the commutator and this is where the brushes make electrical contact to bring electrical current from the stationary part to the rotating part of the machine.

Characteristics of DC Motors

DC motors are non-polarized; this means that its power supply voltage can be reversed. The characteristics of a DC motor that we use in applications are as follows.

Speed Varies with Applied Voltage

This feature is important for running a motor at different speeds. This can be done by increasing or decreasing the power supply voltage. However, when we use electronic control, pulse width modulation (PWM) is the method for varying motor speed.

The method is to apply a pulse train to the power terminals of the motor. The average voltage obtained at the terminals is then proportional to the duty cycle of the pulse train, which is proportional to the speed of rotation (rpm) of the motor. Thus, as the duty cycle is increased, the motor rpm increases and vice versa. When the power supply is constant, it runs at 100% of its power rating (at no load). As the duty cycle reduces, the speed and the power reduce. Figure 2.10 shows pulse trains of various duty cycles.

Figure 21.10 | PWM waveforms at various duty cycle

When it is necessary to do speed control of DC motors for embedded applications, an MCU can be made to generate the PWM waveform based on some criterion or depending on sensor output values. Many MCUs have a PWM unit as an integrated peripheral –
the user just needs to use a few registers to specify the pulse repetition time (T) and the duty cycle. The 8051 does not have a PWM unit – but such a waveform can be generated easily by a simple program.

Torque Varies with Current

The torque of a motor is the rotary force produced on its output shaft. Torque increases with increased current, which means that it increases with increase in power supply voltage.

Reversal of Polarity of the Supply Voltage Causes Reversal of Direction of Rotation

This aspect is very important in many applications, especially in robotics, when the motor needs to reverse its direction of rotation. For example, a robotic vehicle will have to change from forward motion to reverse motion when an obstacle comes in its path. To do this dynamically, some sort of controlling switch is necessary, and this is available in the form of the H bridge.

21.7.1 | H-Bridge

The H-bridge is so named because it has four switching elements at the limbs of an H and the motor forms the cross bar. Figure 21.11 shows the ‘idea’ of the H bridge. There are two switches at the top (left and right) and two more switches at the bottom. They are named S1, S2, S3 and S4.

Figure 21.11 | The principle of operation of an H-bridge

When the motor is not expected to rotate, all the switches are to be kept open. When switches S1 and S4 alone are closed, the motor rotates in the clockwise direction, with switches S2 and S3 closed, the rotation is anti-clockwise. In the positions when the top two switches and/or the bottom two switches are closed, the motor gets short circuited and such a situation should not be allowed.

The valid states of the switch are shown in Table 21.1, assuming that activation by a ‘1’ corresponds to a switch closure.

Table 21.1 | Switch Status for Direction of Motor Rotation

What is the mechanism to realize an H bridge?

It can be done using any device that has switching properties such as relays, transistors and MOSFET. However, if you are trying to run a DC motor from an MCU output, the best bet would be a motor driving IC with H bridge. The IC L293D is a dual H-bridge C that also provides sufficient current to drive a small motor.

The L293D IC whose pin configuration (shown in Fig. 21.12) is a dual H-Bridge motor driver. With one such IC, two DC motors can be driven that can be controlled in both clockwise and counter clockwise directions.

Figure 21.12 | Pins of the L293D motor driver

For applications that do not need reversal of direction, the four output pins can be used for driving four separate motors. This IC is rated for an output current of 600 mA and peak output current of 1.2 A per channel. Moreover, for protection of the circuit against back EMF, snubber/flywheel diodes are included within the IC. A simple schematic for interfacing a DC motor using L293D is shown in Fig. 21.13. Refer Table 21.2 for the status of A and B.

Figure 21.13 | Connections between an 8051, an H-bridge and a DC motor

Table 21.2 | Action Performed for the Four Combinations of A and B

Three pins of the chip are needed as inputs from the MCU. The enable pin has to be set, and the pins A and B are to be controlled by the port lines P1.0 and P1.1, which generate the necessary logic to get the motor to rotate as required.

Embedded application may use either stepper or DC motors. However, when it comes to speed, weight, size and cost, DC motors are always preferred over stepper motors.

KEY POINTS OF THIS CHAPTER

  • ADCs and DACs can be interfaced to the 8051 very easily .
  • Waveforms such as ramp, triangular and staircase waveforms can be generated by the interfacing a DAC to the 8051.
  • LCDs are displays devices that can be connected easily to the 8051 and programmed using the command words of the display module.
  • Dynamic LED displays are better than static LED displays because of power savings.
  • Hex keyboard interfacing needs an input port as well as an output port.
  • DC and stepper motors need extra chips that raise the current level of the circuit.

QUESTIONS

  1. Explain the use of the ALE and SC signals of a typical ADC.
  2. What is indicated by the EOC signal ?
  3. How does a dynamic LED display work?
  4. In an LCD module, what is the use of the RS signal?
  5. How can an LCD display work using just one port of the 8051?
  6. What is meant by ‘key debounce time’?
  7. Why is there an IC between an 8051 and stepper motor?
  8. What is the role of an H bridge in the use of a DC motor?
  9. How can the rate of rotation of a stepper motor be controlled?
  10. Why is PWM used in DC motor control?.