You are on page 1of 12

EEE3410 Microcontroller Applications

LABORATORY Experiment 5

7-segment Display & ADC Controls with 8051


Name Class Date Class No.

Marks

7-segment Display & ADC Controls

Objectives To understand the connection of 7-segment display to 8051 To know how to write 8051 assembly program to control 7-segment display To understand the connection of an ADC to 8051 To know how to write 8051 assembly program to control an ADC to acquire data

Equipment/Components Required PC compatible computer with Windows XP MedWin 8051 simulation software. ADC & 7-segment Display training board with power supply and connection cable HT1000 89C51 Writer floppy disk/USB flash disk

Ver 1.0

Author(s) KK SIN

Date 9/2006

Remark

Laboratory
Vocational Training Council, Hong Kong

Experiment 5

Part A: Background Information


A1. The 7-segment LED numeric display a A 7-segment LED numeric display contains 8 LEDs, of which 7 segments are arranged in the shape of the number 8 to display the digits 0-9, and the eighth one is the decimal point (DP). (Ignore the decimal point in this Laboratory.) Basically, it has one common lead (either anode or cathode) which is connected to the power supply, and the remaining 7 individual leads are for each segment. f e d g b c
Dp

Segment Pattern

Segment displays are driven by connecting each segment to a port bit, or they can be driven by decoder/driver IC designed for the purpose. A decoder/driver chip will accept a parallel input from a 8051 port (binary or ASCII) and drive the display to show the corresponding character. The table shows the corresponding connection of the 8 output port bits to the 8 input terminals of display segments. Output Port bit Display segment 7 Dp 6 g 5 f 4 e 3 d 2 c 1 b 0 a

There are two common types of configurations; namely, the common-anode configuration and the common-cathode configuration.

Common

Dp

Common

Dp

Common-anode configuration

Common-cathode configuration

Common-anode configuration Segment will light up if the output port bit is set LOW. Common-cathode configuration - Segment will light up if the output port bit is set HIGH. Normally a resistor of 330 is connected between each segment terminal and output port bit in order to protect the LED segment. In this laboratory, you will appreciate how to program the 8051 to control 7-segment LED numeric displays.

Experiment 5

Laboratory
Vocational Training Council, Hong Kong

EEE3410 Microcontroller Applications

A2. Analog-to-digital conversion (A/D) Most real time data (e.g. temperature, voltage, velocity) are analog signal, i.e. the signal is continuous varying with time. Microcontroller is a digital device. It can only read and manipulate data in digital form. If this kind of real time analog data is being controlled by microcontroller, some means must be used first to convert the analog data to digital data before manipulating by microcontroller. The common process to get these analog data is to use a device called a transducer (also called sensor) to measure their values. The transducer normally converts measured value into electrical signal (which is either voltage or current) for further conversion to digital form. An example of transducer is a thermistor. Since its resistance varies with the ambient temperature, so the current passing through it will also vary. As a result, current value depends on the temperature being measured. Measure the current will then be the same as to measure the temperature. Yet, the output from a transducer (voltage/current) is still analog; a device is needed to convert them in digital signals (digitization). Such a device is called an analog-to-digital converter (ADC), and this process is called analog-to-digital (A/D) conversion. Once the signal is digitized, it can be sent to the microcontroller for further processing.

A2.1 The ADC0804 IC The ADC0804 IC is one of the most popular analog-to-digital converters. It is in the family of the ADC0800 series. It works with a power supply of +5V, and has a resolution (i.e. output) of 8 bits. Pin description of the ADC0804: Chip select ( CS ) Pin CS is used to activate the ADC0804 IC. The IC is activated and ready to use if a LOW signal is set to this pin. Read ( RD ) (Data output enable) Pin RD is also called output enable. It is an active-low output signal. It is used to get the converted data out of the ADC0804 IC. With CS =0, if a high-to-low pulse is applied to RD , the 8-bit digital is then output to the data pins D0-D7 for pick-up.
CS RD WR CLK INTR

VI+ VIAGND

VREF/2
DGND

1 2 3 4 5 6 7 8 9 10

20 19 18 17 16 15 14 13 12 11

VCC
CLKR

D0 D1 D2 D3 D4 D5 D6 D7

Pin diagram ADC0804

Laboratory
Vocational Training Council, Hong Kong

Experiment 5

Write ( WR ) (Start conversion)

WR is also called start conversion. It is an active-low input signal. It is used to start the ADC0804 IC for data conversion. With CS =0, if a low-to-high pulse is applied to pin WR , the ADC0804 IC will start converting the analog input value at pin VI+ to an 8-bit digital number. Once data conversion is complete, pin INTR will be set to LOW.
Interrupt ( INTR ) (End of conversion)

INTR is also called end of conversion. It is an active-low output signal. It is usually high but once data conversion is complete, it set to LOW to indicate that the converted data is ready for pick-up.
VCC VCC is the +5V power supply to the ADC0804 IC. It can be also used as a reference voltage (VREF) if the pin VREF/2 is open. VREF/2 VREF/2 is an input used for reference voltage other than 0-5V range. But if VREF/2 is left open, the analog input voltage will be in the range of 0-5V. Data pins D0-D7 DB0-DB7 are the digital data output pins, with D7 as the most significant bit (MSB) and D0 as the least significant bit (LSB). They can only be accessed when both CS =0 and RD is LOW.

A2.2 Digital output of the ADC804 IC In this Laboratory, the VREF/2 pin is left open, i.e. the analog input voltage (VIN) is in the range 0-5V. Because the ADC0804 IC has a resolution of 8 bits, the range is divided into 28=256 steps. Or, we can say that there are 255 quantization levels (0V inclusive).
The step size, or the resolution of the ADC804 IC, is: VREF V step size = 8 2 = REF 2 1 255 2 The digital data value (DOUT) of the ADC804 IC is: VIN D OUT = step size

Experiment 5

Laboratory
Vocational Training Council, Hong Kong

EEE3410 Microcontroller Applications

e.g. : A 5V is set to the VREF pin of a ADC0804, find the digital value output from the ADC if it converts an analog input voltage of 3.5V. 5 Solution: step size = V = 0.0196V ; 255
D OUT = 3 .5 = 178 or 179 (depends on the accuracy of the ADC) 0.0196

A2.3. Control of ADC0804 IC to carry out analog-to-digital conversion Normally ADC0804 IC is interfaced with microcontroller to form a data acquisition system, circuit shown in fig. A2.1. the converted value. Fig. A2.2 shows a flowchart gives the steps for signal time issued by a 8051 to control the ADC0804IC
Start

The microcontroller will send out a fixed sequence

of control signals to direct the ADC to complete the A/D conversion process and output

Set
VCC DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 CS VREF AGND RD WR INTR Output Control bits

CS to LOW

Input analog voltage

VI+ VICLKR CLK

Input bits

WR to LOW Next return WR to HIGH Then return CS to HIGH


Set

8051 Microcontroller

Does INTR change to LOW? Y

ADC0804

CS to LOW Set RD to LOW


Set Read the ready ADC Data by a 8051 input port Repea Next return

Fig. A2.1 Interface of ADC0804 to 8051 microcontroller

RD to HIGH Then return CS to HIGH

Fig. A2.2 Flowchart of timing control of ADC0804


Laboratory
Vocational Training Council, Hong Kong

Experiment 5

CS

WR D0 D7 INTR Start conversion End conversion Data out

Read it
Note: CS is set to low for both RD and WR pulses

(Fig. A2.3

The read and write signal timing diagram for ADC804)

Sequence of signal to control the ADC0804 1. Set CS to LOW. (Select the ADC IC) 2. Send a low-to-high pulse to WR , so as to start data conversion. Return CS to High again to de-select the ADC 3. Keep monitoring INTR . If INTR is LOW, that means the conversion is finished. If INTR is still HIGH, keep polling until it becomes LOW. 4. Set CS to LOW to select the ADC 5. Send a high-to-low pulse to RD so that data read by 8051 input port can be done 6. Return RD to High again to indicate port reading completed
7. Return CS to High again to de-select the ADC 8. Repeat steps for another conversion

Experiment 5

Laboratory
Vocational Training Council, Hong Kong

EEE3410 Microcontroller Applications

A3. ADC & 7-segment Display training board Figure A3.1 shows the circuit diagram of the ADC & 7-segment Display training board. Port 2, Port bit 3.6 and Port bit 3.2 are connected to the 8-bit output, pin WR and pin INTR of ADC0804 respectively. Port 0 is connected to a common-anode 7-segment display. 2-digit 7-segment numeric display is connected to the upper & lower nibbles of Port 1 via a two BCD to 7-segment decoders. Port bits 3.0, 3.1, 3.3 & 3.4 0 are connected to 4 push buttons (S1 to S4) respectively.

Laboratory
Vocational Training Council, Hong Kong

Experiment 5

Part B: Procedures
B1. Controlling a 7-segment numeric display A source program shown in table B1.1 controls the 7-segment display at Port 0 to show the digits 0-F in counting up manner. B1.1 B2.2 B2.3 B2.4 Complete the lines 29 31 in table B1.1. Use the 8051 simulation software to check the correct execution of the program. Use the HT1000 89C51 writer to write the HEX file of the program into a blank 89C51 chip. Insert the 89C51 chip to the ADC & 7-segment Display training board and observe the result of execution. Table B1.1
Line 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Label Mnemonics ORG MOV MOV MOV Operands 00h R3, #0 DPTR, #TABLE P0, #01000000b R3 R3, #16, CONT R3, #0 A, R3 A, @A+DPTR P0, A DELAY COUNT_UP R5, #5 R6, #250 R7, #200 R7, LOOP3 R6, LOOP2 R5, LOOP1 Remarks
;reset the counter ;display 0 ;subroutine to count from 0-9 ;increment the counter ;if R310, continue counting ;otherwise count again from 0 ;display the value of R3 in ;7-segment format

COUNT_UP: INC CJNE MOV CONT: MOV MOVC MOV ACALL AJMP DELAY: LOOP1: LOOP2: LOOP3: MOV MOV MOV DJNZ DJNZ DJNZ RET

;sets the delay time

;inner loop ;middle loop ;outer loop ;return to main program

;lookup table to convert from hex to a corresponding digit TABLE: DB 01000000b, 01111001b DB 00100100b, 00110000b DB 00011001b, 00010010b DB 00000010b, 01111000b DB 00000000b, 00010000b DB _________b,_________b DB _________b,_________b DB _________b,_________b END

;0, 1 ;2 ,3 ;4, 5 ;6, 7 ;8, 9 ;A, b ;C, d ;E, F

Experiment 5

Laboratory
Vocational Training Council, Hong Kong

EEE3410 Microcontroller Applications

B2. Controlling 7-segment numeric displays through BCD decoder/driver A source program shown in table B2.1 is expected to control the 2-digit 7-segment display at Port 1 to show the values from 00 to 99 in counting up manner. But it cannot correctly display the values ONE after ONE in sequence. You are required to correct the program in table B2.1 to give the right result. B2.1 By using the 8051 simulation software, correct the program as required. B2.2 Write the HEX file of the program into a blank 89C51 chip. B2.3 Check the program by the ADC & 7-segment Display training board. Table B2.1
Line 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Label Mnemonics ORG 00h MOV A, #0 Operands Remarks
;reset the counter ;display the value of A ;A=A+ 1 ;if A100, continue counting ;otherwise count again from 0

COUNT_UP: MOV ACALL INC CJNE MOV CONT: DELAY: LOOP1: LOOP2: LOOP3: AJMP ; MOV MOV MOV DJNZ DJNZ DJNZ RET END

P1, A DELAY A A, #100, CONT A, #0 COUNT_UP R5, #5 R6, #250 R7, #200 R7, LOOP3 R6, LOOP2 R5, LOOP1

;sets the delay time

;inner loop ;middle loop ;outer loop ;return to main program

B3. Controlling ADC A source program is given in table B3.1 which controls an 8-bit ADC0804 IC repetitively to acquire analog voltage signals from a potentiometer, and convert the signals to 8-bit digital values and finally to output the values to the Port 2 of 8051. After the 8-bit digital value is inputted, the 8051 has to convert the 8-bit value to an equivalent decimal value and display it by the 2-digital 7-segment display. When you turn the potentiometer, the analog voltage input to the ADC will change and similarly, the digital values displayed will also change. Problem: it is found that the analog voltage input, measure by a DMM, to the ADC cannot be correctly displayed by the 2-digital 7-segment display.

Laboratory
Vocational Training Council, Hong Kong

Experiment 5

B2.1 B2.2 B2.3

By using the 8051 simulation software, correct the program as required. Write the HEX file of the program into a blank 89C51 chip. Check the program by the ADC & 7-segment Display training board.

Table B3.1
Line 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Label Mnemonics ORG MOV MOV MOV STARTCON CLR : NOP SETB NOP CHK_END: JB NOP READ: MOV MOV ACALL AJMP Operands 00h P1, #0 P2, #0FFh P3, #0FFh P3.6 P3.6 P3.2, CHK_END A, P2 P1, A DELAY STARTCON Remarks ;Display 00 ;set port 2 as input port ;set port 3 output to High ; ;Set port bit 3.6 to Low, ;start ADC conversion ;reset port bit 3.6 to High ;check for end of conversion ;input from ADC ; ;output to port 1

DELAY: LOOP1: LOOP2: LOOP3:

MOV MOV MOV DJNZ DJNZ DJNZ RET END

R5, #5 R6, #250 R7, #200 R7, LOOP3 R6, LOOP2 R5, LOOP1

; ;sets the delay time

;inner loop ;middle loop ;outer loop ;return to main program

Note: As the pins of CS and RD are directly connected to the ground in the training board, they are always at LOW input. That means the ADC is always selected and ready for output. So the source program in the above table has not necessary to include any instructions for chip selection and read action of the ADC.

10

Experiment 5

Laboratory
Vocational Training Council, Hong Kong

EEE3410 Microcontroller Applications

B2.4

After the program is corrected and executed by a 89C51 chip, complete the table below by first turning the potentiometer knob at the end of counter clockwise direction, record the voltage input to the ADC by a DMM and the value displayed by the 2-digit 7-segment display. Then slowly turn the knob in clockwise direction until at the end position. Record down the values displayed at any four intermediate positions. Compare with the voltage values measured by DMM. Value indicated by the 2-digital 7-segment display Analog voltage inputted to the ADC (Volts) 0V

Position of knob At Full counter clockwise At Intermediate position 1 At Intermediate position 2 At Intermediate position 3 At Intermediate position 4 At Full clockwise

0 0

Laboratory
Vocational Training Council, Hong Kong

Experiment 5

11

Part C: Exercise
C1. Assume that pin Vref/2 is connected to a reference voltage of 2V. Determine the following. (a) Step Size:

(b) Maximum range for Vin

(c) D7 D0 value if Vin = 1.2 V

(d) Vin if D7 D0 = 1111 1111

(e) Vin if D7 D0 = 1101 1011

C2.

What is the value for Vref/2 to set the ADC0804 has a step size of 8 mV.

- End -

12

Experiment 5

Laboratory
Vocational Training Council, Hong Kong