week10 boolean instructions on the 8051. boolean instructions boolean (or bit) addressable...

32
Week10 Boolean Instructions on the 8051

Upload: jevon-rhoad

Post on 14-Dec-2015

245 views

Category:

Documents


0 download

TRANSCRIPT

Week10

Boolean Instructions on the 8051

Boolean InstructionsBoolean Instructions

Boolean (or Bit) addressable capability is unique to the 8051

Enables efficient handling of bit valuesHaving bit variables makes efficient use of memory

The addresses between 20H and 2FH (i.e. 16 locations) can be accessed in a bitwise manner (as well as the normal byte wide way)

The key idea is that there are special instructions that are specifically for bit operations

Boolean InstructionsBoolean Instructions

0023H

001FH0020H0021H0022H

0028H

0024H0025H0026H0027H

0029H

002EH

002AH002BH002CH002DH

0030H002FH

128bytes

8 bits 32 bytesused

00h

20h

80h

FFh

128 bytes

80h

FFhSFRs

128bytes

DirectAddressing

InDirectAddressing

Direct &Indirect

Addressing

Boolean InstructionsBoolean Instructions

Shown to the right are all the bits in address locations 20H to 2FH and their corresponding bit addresses

For example bit 2 of the byte at address 21H has a bit address of 0AH

There are a total of 128 bit addresses for the 16 bytes between 20H and 2FH

0023H

001FH0020H0021H0022H

0028H

0024H0025H0026H0027H

0029H

002EH

002AH002BH002CH002DH

0030H002FH

0001020304050607

08090A0B0C0D0E0F

1011121314151617

78797A7B7C7D7E7F

7071727374757677

Boolean InstructionsBoolean Instructions

Seen bit addresses, but how are they used?Ans: Special boolean instructions operate on bit addressesAlso referred to as bit variable manipulation instructions

Some examples includeCLR 7CH ; clear bitSETB 01H ; set bitCPL 18H ; complement bit

What does the following instruction doMOV 7CH,A

Excerise: Clear a bit, without using a bit instruction!!

Boolean InstructionsBoolean InstructionsHere are all the

boolean instructions

bit represents a bit address

ANL C,bit

ANL C,/bit

CLR bit

CLR C

CPL bit

CPL C

JB bit,rel8JBC bit,rel8JNB bit,rel8MOV C,bitMOV bit,CORL C,bitORL C,/bitSETB bitSETB C

The benefit of these instructions is that each instruction provides an efficient way to manipulate or test a bit

SFRs (Revisted)SFRs (Revisted)

TCON87H

SPDPL

P080H81H82H83H

TH18DHTL1

TL0TL1

TMOD89H8AH8BH8CH

PCON

8EH8FH90H

DPH

88H

98H

P1

SCON

A0H P2

A8H IE

B0H P3

IP

C8H T2CON

D0H PSW

B8H

E0H .ACC

F0H B

TCON87H

SPDPL

P080H81H82H83H

TH18DHTL1

TL0TL1

TMOD89H8AH8BH8CH

PCON

8EH8FH90H

DPH

88H

98H

P1

SCON

A0H P2

A8H IE

B0H P3

IP

C8H T2CON

D0H PSW

B8H

E0H .ACC

F0H B

SP DPLP080H88H90H98H

DPH PCONTMOD TL0TCON TL1 TH0 TH1

P1SCON

A0HA8H

P2IE

B8HP3IP

C0HC8H T2CON

D8HPSW

E0HE8H

.ACC

B0H

D0H

F0HF8H

B

Certain SFRs are bit addressable

Boolean InstructionsBoolean Instructions

SP DPLP080H88H90H98H

DPH PCONTMOD TL0TCON TL1 TH0 TH1

P1SCON

A0HA8H

P2IE

B8HP3IP

C0HC8H T2CON

D8HPSW

E0HE8H

.ACC

B0H

D0H

F0HF8H

B

98H

80H88H90H

C0H

A0HA8HB0HB8H

C8H

F0H

D0HD8HE0HE8H

F8H

8081828384858687

88898A8B8C8D8E8F

9091929394959697

F0F1F2F3F4F5F6F7

The highlighted SFRs are bit addressible and have the bit addresses in the range 80H to FFH

ExampleExample

Clear bit 3 of I/O port 1Remember that port 1 controls

the digital voltage levels on certain pins

CLR 93HThe assembler can make life

easierCLR P1.3

The instruction encoding is C293H or in bits

11000010 10010011

98H

80H88H90H

C0H

A0HA8HB0HB8H

C8H

F0H

D0HD8HE0HE8H

F8H

8081828384858687

88898A8B8C8D8E8F

9091929394959697

F0F1F2F3F4F5F6F7

Actually, most of the bit addresses in the SFRs have symbolic names

I/O ports on 8051I/O ports on 8051 There are 4 I/O ports on the 8051

Each port is 8 bits wide Individual bits can be configured as either a digital input

or digital output These ports provide a way of communicating with the

real world Available as pins on the 8051 device

Port pins operation is controlled through instructions that access the port registers in the SFR section of memory

Look at the details of Port 0 See how a bit of information is stored See how the same line can act as either a digital input

or a digital output

I/O ports’ SFRsI/O ports’ SFRsNote: On reset or power-on, the Port SFRs contain 0xFF. Why?

SP DPLP080H88H90H98H

DPH PCONTMOD TL0TCON TL1 TH0 TH1

P1SCON

A0HA8H

P2IE

B8HP3IP

C0HC8H T2CON

D8HPSW

E0HE8H

.ACC

B0H

D0H

F0HF8H

B

I/O port 0I/O port 0P0.0

P0.1

P0.2

P0.4

P0.3

P0.5

P0.6

P0.7

Functional diagram of 1 of the 8 I/O ports for Port 0

The execution of an instruction provides data and control information for these internal port signals

Writing bit to a port line

SETB P0.X

1 1

0

NMOS transistor switches off

P0.X pin is floating if there is no pull-up resistor

If port pin is being used as output, an external pull-up resistor is needed

VCC

Writing bit to a port line

CLR P0.X

0 0

1

NMOS transistor switches on

P0.X pin is pulled to GND

Current flows through pull-up resistor to GND

(other loading)

VCC

Reading from a port line. First must write a 1, so as to turn off the NMOS transistor

SETB P0.X

1

0

NMOS transistor is off

P0.X pin now acts as input

Can then read the signal on the pin

JB P0.X,start_pump

NB: Note that signal level on pin is read and NOT the latch value

- the instruction determines this

1

Some instructions (see next slide for list) read and modify the bit stored on the latch CPL P0.X

NB: Note that signal level on latch is read and NOT the pin value in this case

0

01

These read and modify instructions are used when the port is being used as an output

ANL P1,A ORL P2,A

XRL P3,A JBC P1.1,Loop

CPL P3.0 INC P2

DEC P2 DJNZ P3,Loop

MOV P2.1,C CLR P2.6

SET P1.3  

Read/modify instructions

Port 1Key Points

- Does not have dual function

-Has internal pullup resistor on each line

-Port 2 and 3 have internal pullups also

-Still use same approach as Port0 to configure a line as an input

P0.2

P0.3

VCC

VCC

R1

R2

SW1

D18051

Example: Using port 0 to drive a LED and to read a switch (SPST) state

Steps

1. Write a 0 to P0.2 to turn LED on, or a 1 to turn off

CLR P0.2 ; on

SETB P0.2 ; off

2. Configure P0.3 as input by writing a 1 to p0.3 latch

SETB P0.3

3. Read from P0.3 pin to determine whether switch is open or close. One example is the following which continously reads the switch until it is closed

  loop: JB P0.3, loopNote that on reset, all ports have 1

stored on the latch, making each line an input. Why?

; Description : Switches the port pin P3.4 on and off with; 200mSec period @ 50% duty cycle. The pin could be used to ; turn on and off a LED;

PORT_PIN_LED EQU P3.4 ; Connected to red LED on evaluation board;____________________________________________________________________ ; MAIN PROGRAMORG 0000H

LJMP main ORG 0048H main: CPL PORT_PIN_LED ; toggle (complement) the LED CALL delay JMP main  ;--------------------------------------------------------------; Subroutine : delay; Description : Subroutine which introduces a time delay by ; repeatedly executing an instruction ; The repetition is controlled by nested loops.; The total delay is approx 100ms;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo  delay: ; delay 100ms  MOV R7,#200 ; 200 * 500us = 100msdly1: MOV R6,#250 ; 250 * 2.us = 500usdly2: DJNZ R6,dly2 ; sit here for 500us DJNZ R7,dly1 ; repeat 200 times (100ms delay) RET;____________________________________________________________________ END

;********************************************************************;; Author : R. Conway; Date : Feb 2000; File : lab4a51; Hardware : Any 8051 based Microcontroller; Description : Reads in from an input port pin P3.2 and uses this ; input to either switch the port pin P3.4 on and off.; Note the switch is debounced through hardware;; CIRCUIT Vcc; | ; 8051 |-| ; ---- | | 270R; | | | Vcc; | LED |_| |; | |\ / | |-|; P3.4 |-|->0---(/)--- | | 100K ; | |/ \// | |; | Schmitt |_|; | /| /| |; P3.2 |--o<-|----o< |-------------|------|; | \| \| | |; | | |; | 0.01uF --- /; | --- / Switch; | | o/; | | |; | ---|----; | Gnd

ExampleExample

; FLOWCHART;; ------------------; | Configure Port(s)|; ------------------; |---------<-----------; ------------------ |; | Turn off LED | |; ------------------ |; |---------<------- |; / \ | |; / Is \ Yes | |; / switch \--->------- |; \ opened / |; \ / |; \ / |; | No |; ------------------ |; | Turn on LED | |; ------------------ |; |---------<------| |; / \ | |; / Is \ Yes | |; / switch \--->------- | ; \ closed / |; \ / |; \ / |; | No |; ---------->-----------;

ExampleExample

;********************************************************************

PORT_PIN_LED EQU P3.4 ; Connected to red LED on evaluation

PORT_PIN_SWITCH EQU P3.2 ; See above schematic

;____________________________________________________________________ ; MAIN PROGRAMCSEG

ORG 0000H

SETB PORT_PIN_SWITCH ; Make this PORT_PIN an input

loop:CLR PORT_PIN_LED ; Switch this PORT_PIN off (output)

open: JB PORT_PIN_SWITCH, open ; Stay here if switch is opened

SETB PORT_PIN_LED ; Turn LED onclose: JNB PORT_PIN_SWITCH, close ; Stay here if switch is closed JMP loop ;____________________________________________________________________

END

ExampleExample

Appendix

More on I/O ports

P3.2

P3.2

V1

R1

R2

TR1

D1

8051

V1

R4

R3

TR2

D2

8051

Not needed (when pull up present)

V1 can be > VCC V1 cannot be > VCCBecause internal pullup limits base current, it may not be possible to fully switch on TR1

This means using a high gain transistor

Be carefull when using Port 0 as there are no internal pullup resistors

Using external transistor switches

P3.2

P3.2

V1

R1

R2

TR1

D1

8051

V1

R4

R3

TR2

D2

8051

Not needed (when pull present)

V1 can be > VCC V1 cannot be > VCC

No problem here with turning on TR2 as the port output can sink up to 10 mA typically

Using external transistor switches

Example of using relay to control a motor

Example of using relay to control a motor

Electromechanical device No electrical connection

between drive circuitry and switching side

Need external diode to prevent back EMF from damaging transistor when switched off

Cannot switch at high speed Closed switch has very low

resistance (milli ohms)

R1

D1

TR1

Relay1

DigitalGND

VCC

12V

Motor

Motor GND

From uC

Solid State Relays (SSR) are also available, with isolation and higher swicthing speeds

Depending on type of motor to be used, normally can used special ICs such as H-bridges or motor control ICs

Using an optocouplerUsing an optocoupler

TTLo/p

VCC1 VCC2

R1 R2

ExercisesExercises

1. What is a boolean (bit) instruction?2. What are the range of addresses for bit addresses3. How does the 8051 distinguish between bit addresses

and byte addresses?4. Explain the operation of the following instructions

1. CPL 0x432. JB 0x44, action

5. When dealing with I/O ports, some instructions access port pins, while some access the SFR. Give one example of each

ExercisesExercises

6. A program reads from Port 1.1 and port 1.2 and turns on an LED on P1.2 if both inputs are high.

1. Draw a flow chart first2. Detail the assembly code