+ All Categories
Home > Engineering > 8051 MICROCONTROLLER BASIC

8051 MICROCONTROLLER BASIC

Date post: 20-Jan-2017
Category:
Upload: raviprakash-thakur
View: 332 times
Download: 5 times
Share this document with a friend
34
1 Week 10 © Vocational Training Council, Hong Kong. Simple I/O Simple I/O Interfacing Interfacing Microcontroller Applications Department of EC Engineering │ │
Transcript
Page 1: 8051 MICROCONTROLLER BASIC

1Week 10© Vocational Training Council, Hong Kong.

Simple I/O InterfacingSimple I/O Interfacing

Microcontroller ApplicationsDepartment of EC Engineering

│ │

Page 2: 8051 MICROCONTROLLER BASIC

2Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Interface 8051 with the following Input/Output Devices

Switches Solenoid and relays LEDs Seven Segment Display Dot matrix display

In this Lecture ….In this Lecture ….

Page 3: 8051 MICROCONTROLLER BASIC

3Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Mechanical switchesElectromagnetic relaysSolid-state relaysLEDs7-segment DisplayDot Matrix Display

IntroductionIntroductionThe 8051 microcontroller is commonly used for real-world applications, e.g. display control, lighting control, machine control, etc. Various input and output devices are connected to the I/O ports of the microcontroller to deal with different applications. We will introduce some common I/O devices for simple application in this lecture. They are:

Page 4: 8051 MICROCONTROLLER BASIC

4Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Mechanical switches are common input devicesOne or more pairs of contacts that can be open or close.Typical switch designations are:

SPST (single-pole-single-throw) 單刀單擲 SPDT (single-pole-double-throw) 單刀雙擲 DPDT (double-pole-double-throw)

Normally open (N.O.) contacts close when the switch is activated and normally close (N.C.) contacts OPEN when the switch is activated.

Mechanical SwitchesMechanical Switches

N.O. SPST

N.O. DPDT

N.O. SPDT

雙刀雙擲

Page 5: 8051 MICROCONTROLLER BASIC

5Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

http://www.colehersee.com/04resource/spst.htmhttp://www.colehersee.com/04resource/dpdt.htm

Page 6: 8051 MICROCONTROLLER BASIC

6Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Connect mechanical switches to 8051Connect mechanical switches to 8051+5V

8051

EA VCC

Reset

XTAL1

XTAL2

VSS

P3.7

P3.6

P3.5

P3.4

SW1

SW2

SW3

SW4

SW opens, input to 8051 is HIGH (1)SW closes, input to 8051 is LOW (0)

ORG 0000H:JNB P3.7, CASE1

JNB P3.6, CASE2JNB P3.5, CASE3JNB P3.4, CASE4::

CASE1: ::

CASE2: ::

CASE3: ::

CASE4: ::

Check the status of SW1

Figure 9.1

Page 7: 8051 MICROCONTROLLER BASIC

7Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Electromagnetic RelaysElectromagnetic Relays Electromagnetic relay consists of two parts - solenoid

and relay contacts Solenoid is a coil of wire used to produce a magnetic

field to move a steel actuator where points of contacts are located.

The actuator is used to close/open the contact points, such construction is called a relay.

Figure 9.2 construction of a Electromagnetic relay

NC: NORMAL CLOSENO: NORMAL OPEN

Page 8: 8051 MICROCONTROLLER BASIC

8Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Driving an Electromagnetic RelaysDriving an Electromagnetic Relays Figure 9.3 show a typical driving

circuit of an electromagnetic relay. The transistor will act as a switch

to allow current passing through the solenoid.

The diode placed across the coil terminal is to protect the transistor damaged from spike voltage during ON/OFF.

The external circuit connected to relay terminals will be turned ON/OFF by the TTL.

Contact closed if TTL = High Contact opened if TTL = Low

+5V

Vs

Figure 9.3 Driving circuit of an electromagnetic relay

TTL

Page 9: 8051 MICROCONTROLLER BASIC

9Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Solid State Relays (Solid State Relays (SSRSSR)/Switches)/Switches Solid-state relays has no mechanical parts and made of

semi-conductor materials It combines the isolation, driving, and contact closure

functions into a single package It is commonly use to control ac loads Relay closed if input = High Relay opened if input = Low

SSR+

T1

T2

a.c. power supply

Figure 9.4 Driving a solid state relay

Input Control

Page 10: 8051 MICROCONTROLLER BASIC

10Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Light-Emitting Diode (LED)Light-Emitting Diode (LED)

Light-emitting diodes (LEDs) can be turned on when a current passes through it.

Figure 9.5 Shows a typical TTL circuit driving a LEDThe 330 resistor is used to limit the amount of current to pass through the LED to prevent it burning off.The TTL output is LOW, the LED will ONThe TTL output is HIGH, the LED will OFF

+5V

TTL

Figure 9.5

330

Page 11: 8051 MICROCONTROLLER BASIC

11Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Control of LEDsControl of LEDs

P1.7

P1.6

P1.5

P1.4

P1.3

P1.2

P1.1

P1.0

+5V

8051

EA VCC

Reset

XTAL1

XTAL2

VSS

• 8 LEDs are connected to Port 1 and linked to 5V supply

• The LEDs can directly be turned ON/OFF by the 8051

• Under this connection, the LEDs will be

OFF when port bit is at logic 1 ON when port bit is at logic 0

Figure 9.6

0 = on1 = off

Page 12: 8051 MICROCONTROLLER BASIC

12Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Example 9.1 :Example 9.1 : Control the 8 LEDs ON/OFF at the same timeControl the 8 LEDs ON/OFF at the same time

All LEDs ON

All LEDs OFF

Page 13: 8051 MICROCONTROLLER BASIC

13Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Program Listing for Example 9.1Program Listing for Example 9.1

ORG 0000HCLR A

LOOP: MOV P1, ACPL AACALL DELAYAJMP LOOP

DELAY: MOV R6, #250DL1: MOV R7, #200DL2: DJNZ R7, DL2

DJNZ R6, DL1RETEND

Start

Set A = 00

Move the content of A to P1

Delay for 0.1s

Invert the content of A

Assume 12MHz clock, determine the delay time.

Page 14: 8051 MICROCONTROLLER BASIC

14Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

ORG 0000HSTART: MOV R1, #07H

MOV A, #11111110BLEFT: MOV P1, A

ACALL DELAYRL ADJNZ R1, LEFT;MOV R1, #07HMOV A, #01111111B

RIGHT: MOV P1, AACALL DELAYRR ADJNZ R1, RIGHTAJMP START;

DELAY: ……….

Example 9.2 : Example 9.2 : Glowing a LED in rotating sequenceGlowing a LED in rotating sequence

Page 15: 8051 MICROCONTROLLER BASIC

15Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Example 9.3: Turning LEDs ON/OFF in a preset sequence by using a Look-up Table

Page 16: 8051 MICROCONTROLLER BASIC

16Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Program Listing of Example 9.3

ORG 0000HSTART: MOV R0, #OKDATA+1

MOV DPTR, #DATAMOV R1, #00H

LOOP: MOV A, R1MOVC A, @A+DPTRMOV P1, AACALL DELAYINC R1DJNZ R0, LOOPAJMP START;

DELAY: MOV R5, #2DL1: MOV R6, #250DL2: MOV R7, #200DL3: DJNZ R7, DL3

DJNZ R6, DL2DJNZ R5, DL1RET;

DATA: DB 01111110BDB 00111100BDB 00011000BDB 00000000BDB 00011000BDB 00111100BDB 01111110BDB 11111111B;DB 01111110BDB 00111100BDB 00011000BDB 00000000BDB 00011000BDB 00111100BDB 01111110BDB 11111111B;DB 00000000BDB 11111111BDB 00000000B

OK: DB 11111111BEND

Page 17: 8051 MICROCONTROLLER BASIC

17Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Exercise: Write a 8051 program, using a look-up table, to light-up the LEDs in the sequence as shown below

Page 18: 8051 MICROCONTROLLER BASIC

18Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Simple I/O applicationsSimple I/O applications

P1.7

P1.6

P1.5

P1.4

P1.3

P1.2

P1.1

P1.0

+5V

8051

EA VCC

Reset

XTAL1

XTAL2

VSS

P3.7

P3.6

P3.5

P3.4

SW1

SW2

SW3

SW4

Figure 9.7

Page 19: 8051 MICROCONTROLLER BASIC

19Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

When SW1 Closed

When SW3 Closed When SW4 Closed

When SW2 Closed

Priority: SW1 SW2 SW3 SW4

Example 9.4: Refer to the 8051 circuit in figure 9.5, write a 8051 program to light-up the LEDs in the pattern as shown below when the respect switch is closed.

YELLOW=ONRED =OFF

Page 20: 8051 MICROCONTROLLER BASIC

20Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Flow Chart of Example 9.4

Start

Initialization

Set P3 as input port

Read SW1SW4 status

SW1 closed?

SW2 closed?

SW3closed?

SW4 closed?

SW1 HandlerY

SW2 HandlerY

SW3 HandlerY

SW4 HandlerY

2

1

21

N

N

N

Page 21: 8051 MICROCONTROLLER BASIC

21Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Program Listing of Example 9.4

ORG 0000HMOV R1, #00000000BMOV R2, #01010101BMOV R3, #00001111BMOV R4, #11110000B;

TEST: ORL P3, #0FFHJNB P3.7, CASE1

JNB P3.6, CASE2JNB P3.5, CASE3JNB P3.4, CASE4AJMP TEST;

CASE1: MOV A, R1MOV P1, AACALL DELAYXRL A, #11111111BMOV P1, AAJMP TEST;

CASE2 MOV A, R2MOV P1, AACALL DELAYXRL A, #10101010B

MOV P1, AAJMP TEST;

CASE3 MOV A, R3MOV P1, AACALL DELAYXRL A, #11110000BMOV P1, AAJMP TEST;

CASE4 MOV A, R4MOV P1, AACALL DELAYXRL A, #00001111BMOV P1, AAJMP TEST;

DELAY: ………..END

P1.x = 0, ONP1.x = 1. OFF

Page 22: 8051 MICROCONTROLLER BASIC

22Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

A single-character display panelContains 7 LED segments arranged as an “8”

Two configurations: common-anode and common-cathode

7-Segment LED Numeric Display7-Segment LED Numeric Display

a

b

c

d

e

f g

Segment Pattern

Dp

Page 23: 8051 MICROCONTROLLER BASIC

23Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Common-anode configuration

Common-cathode configuration

7-Segment LED7-Segment LED Numeric DisplayNumeric Display

a b c d e f g DpCommon

a b c d e f g DpCommon

Pull-high

Pull-low

Page 24: 8051 MICROCONTROLLER BASIC

24Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Segment displays are driven by connecting each segment to a port bit, or they can be driven by decoder/driver IC

7-Segment LED Numeric Display7-Segment LED Numeric Display

Output Port bit 7 6 5 4 3 2 1 0

Display segment Dp g f e d c b a

Normally the LED should be connected to the power via resistors to protect them from burningWhen using common-cathode configuration, a segment will be lit only if the lead of the segment connected with a High voltage and the common cathode lead with Low voltage

Page 25: 8051 MICROCONTROLLER BASIC

25Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

7-Segment LED Numeric Display7-Segment LED Numeric Display

Use R3 as counter, write a 8051 assembly language program using look-up table method, to display the value in R3 to a 7-segment display

8051

bc

defg

aXTAL1

XTAL2

P2.0

P2.1

P2.2

P2.3

P2.4

P2.5

P2.6

P2.7

Figure 9.8

Vcc

a

b

c

d

e

fg

Dp.

Dp.

Page 26: 8051 MICROCONTROLLER BASIC

26Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Program ListingProgram ListingORG 0000HMOV R3, #00H

LOOP: MOV DPTR, #TABLEMOV A, R3MOVC A, @A+DPTR

;; Display numbers on 7-segment display

MOV P2, AACALL DELAY

;; Increase R3 by 1 and loop back

MOV A, R3ADD A, #1DA AANL A, #0FHMOV R3, AAJMP LOOP;

DELAY: …………..

TABLE: DB 11000000B ; 0DB 11111001B ; 1DB 10100100B ; 2DB 10110000B ; 3DB 10011001B ; 4DB 10010010B ; 5DB 10000010B ; 6DB 11111000B ; 7DB 10000000B ; 8DB 10010000B ; 9; 0=ON, 1=OFFEND

Display 0,1,2,,,,8,9,0,1,2,3….

Output port , P2

7 6 5 4 3 2 1 0

Display segment

Dp g f e d c b a

7-segment ‘0’

1 1 0 0 0 0 0 0BCD addition

Page 27: 8051 MICROCONTROLLER BASIC

27Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Consists of a number of LED arranged in the form of a matrixe.g. 35 LED in a 5 columns x 7 rows matrix, or 64 LED in a 8 x 8 matrixTo display a digit/character, use the method of scanning

cdefg

a

1 2 3 4 5

b

i.e. scan a column at a time. If the scanning is fast enough, it “appears” that the digit/character is displayed (due to illusion of our eyes)

Dot-matrix LED DisplayDot-matrix LED Display

Page 28: 8051 MICROCONTROLLER BASIC

28Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Internal circuitry of a 5 x 7 dot matrix display is shown on the right

Voltage should be supplied to terminals C and 1 to light up the LED indicated in this picture

Dot-matrix LED DisplayDot-matrix LED Display

c

d

e

f

g

a

b

1 2 3 4 5

Page 29: 8051 MICROCONTROLLER BASIC

29Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

P3.0

P3.1

P3.2

P3.3

P3.4

P3.5

P3.6

8051

Reset

XTAL1

XTAL2

VSS

EA Vcc

P1.0

P1.1

P1.2

P1.3

P1.4

cdefg

ab

1 2 3 4 5

Dot-matrix LED DisplayDot-matrix LED Display

Drive Column P1 MethodOn column 1 00001Off 00000On column 2 00010Off 00000On column 3 00100Off 00000On column 4 01000Off 00000On column 5 10000Off 00000

Repeat 100 times

Page 30: 8051 MICROCONTROLLER BASIC

30Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Example: Displaying the character “E” on the dot-matrix.

Dot-matrix LED DisplayDot-matrix LED Display

bcdefg

a

1 2 3 4 5

Displaying the character “E” on the 5x7dot-matrix.

Step 1: signals on pinspins 12345 – “10000”pins gfedcba – “1111111”

Step 2: signals on pinspins 12345 – “01000”pins gfedcba – “1001001”

Step 3: signals on pinspins 12345 – “00100”pins gfedcba – “1001001”

Step 4: signals on pinspins 12345 – “00010”pins gfedcba – “1001001”

bcdefg

a

1 2 3 4 5 1 2 3 4 5

bcdefg

a

1 2 3 4 5

bcdefg

a

1 2 3 4 5

bcdefg

a

1 2 3 4 5

bcdefg

a

Page 31: 8051 MICROCONTROLLER BASIC

31Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Code Words and Displays of ‘0’ to ‘4’Code Words and Displays of ‘0’ to ‘4’00

1111

10B

0101

0001

B01

0010

01B

0100

0101

B00

1111

10B

0000

0000

B01

0000

10B

0111

1111

B01

0000

00B

0000

0000

B

0100

0110

B01

1000

01B

0101

0001

B01

0010

01B

0100

0110

B

0010

0001

B01

0000

01B

0100

1001

B01

0011

01B

0011

0011

B

0001

1000

B00

0101

00B

0001

0010

B01

1111

11B

0001

0000

B

Page 32: 8051 MICROCONTROLLER BASIC

32Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Code Words and Displays of ‘5’ to ‘9’Code Words and Displays of ‘5’ to ‘9’00

1001

11B

0100

0101

B01

0001

01B

0100

0101

B00

1110

01B

0011

1100

B01

0010

10B

0100

1001

B01

0010

01B

0011

0000

B

0000

0001

B01

1111

10B

0111

1001

B00

0001

01B

0000

0011

B

0011

0110

B01

0010

01B

0100

1001

B01

0010

01B

0011

0110

B

0000

0110

B

0100

1001

B

0100

1001

B00

1010

01B

0001

1110

B

Page 33: 8051 MICROCONTROLLER BASIC

33Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

Program Listing – Program Listing – character display on 5x7 Dot-matrix display character display on 5x7 Dot-matrix display (1/2)(1/2)ORG 0000H

START: MOV DPTR, #TABLE ; point to the starting add of 1st charMOV R3, #10 ; display 10 characters: 0,1,2,3,,,,,9,0,1,….

LOOP: MOV R2, #100 ; scan 100 times for each characterSCAN: ACALL SCAN1 ; 10ms x 100 = 1000ms

DJNZ R2, SCANINC DPTR ; increase DPTR by 5 toINC DPTR ; point to the starting address ofINC DPTR ; the next characterINC DPTRINC DPTRDJNZ R3, LOOP ; loop back to display 10 charAJMP START; ======================; == Scan Subroutine ==; ======================

SCAN1: MOV R1, #00H ; R1 points to the starting add of a charMOV R5, #00000001B ; start from the leftmost columnMOV R4, #05 ; there are 5 columns, R4 as a counter !

LOOP1: MOV A, R1 ; get the code at add R1+DPTRMOVC A, @A+DPTRMOV P3, A ; send the code to the dot-matrix display (ROW)MOV P1, R5 ; turn-on a particular transistor (COLUMN)

Page 34: 8051 MICROCONTROLLER BASIC

34Week 9© Vocational Training Council, Hong Kong.

EEE3410 Microcontroller Applications

MOV R6, #5 ; delay for 2ms delay routineDL1: MOV R7, #200 ; delay routineDL2: DJNZ R7, DL2 ; delay routine

DJNZ R6, DL1 ; delay routineORL P1, #11111111B ; turn-off display

ANL P1,#00000000B ; turn-off displayMOV A, R5 ; RL A ; move to next columnMOV R5, A ; totally there are 5 columnsINC R1 ; NEXT COLUMN DATADJNZ R4, LOOP1 ;RET ; return the main program; ============================; == Character Table ==; ============================

TABLE: DB 00111110B ; codes for 0DB 01010001BDB 01001001BDB 01000101BDB 00111110BDB 00000000B ; code for 1: ; codes for 2 to 9: ;END

Program Listing – Program Listing – character display on 5x7 Dot-matrix display character display on 5x7 Dot-matrix display (2/2)(2/2)


Recommended