# Week10 Boolean Instructions on the 8051. Boolean Instructions  Boolean (or Bit) addressable capability is unique to the 8051  Enables efficient handling.

## Presentation on theme: "Week10 Boolean Instructions on the 8051. Boolean Instructions  Boolean (or Bit) addressable capability is unique to the 8051  Enables efficient handling."— Presentation transcript:

Week10 Boolean Instructions on the 8051

Boolean Instructions  Boolean (or Bit) addressable capability is unique to the 8051  Enables efficient handling of bit values  Having 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 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

Boolean Instructions  Seen bit addresses, but how are they used?  Ans: Special boolean instructions operate on bit addresses  Also referred to as bit variable manipulation instructions  Some examples include CLR 7CH; clear bit SETB 01H; set bit CPL 18H; complement bit  What does the following instruction do MOV 7CH,A Excerise: Clear a bit, without using a bit instruction!!

Boolean Instructions  Here 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,rel8 JBC bit,rel8 JNB bit,rel8 MOV C,bit MOV bit,C ORL C,bit ORL C,/bit SETB bit SETB C The benefit of these instructions is that each instruction provides an efficient way to manipulate or test a bit

SFRs (Revisted) Certain SFRs are bit addressable

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

Example  Clear bit 3 of I/O port 1  Remember that port 1 controls the digital voltage levels on certain pins  CLR 93H  The assembler can make life easier  CLR P1.3  The instruction encoding is C293H or in bits  11000010 10010011 Actually, most of the bit addresses in the SFRs have symbolic names

I/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’ SFRs Note: On reset or power-on, the Port SFRs contain 0xFF. Why?

I/O port 0 P0.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 0 1 These read and modify instructions are used when the port is being used as an output

ANL P1,AORL P2,A XRL P3,AJBC P1.1,Loop CPL P3.0INC P2 DEC P2DJNZ P3,Loop MOV P2.1,CCLR P2.6 SET P1.3 Read/modify instructions

Port 1 Key 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

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, loop Note 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 PROGRAM ORG 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 = 100ms dly1: MOV R6,#250 ; 250 * 2.us = 500us dly2: 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 Example

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

;******************************************************************** PORT_PIN_LED EQU P3.4 ; Connected to red LED on evaluation PORT_PIN_SWITCH EQU P3.2 ; See above schematic ;____________________________________________________________________ ; MAIN PROGRAM CSEG 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 on close: JNB PORT_PIN_SWITCH, close; Stay here if switch is closed JMP loop ;____________________________________________________________________ END Example

Appendix More on I/O ports

Not needed (when pull up present) V1 can be > VCC V1 cannot be > VCC Because 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

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  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) 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 optocoupler

Exercises 1. What is a boolean (bit) instruction? 2. What are the range of addresses for bit addresses 3. How does the 8051 distinguish between bit addresses and byte addresses? 4. Explain the operation of the following instructions 1. CPL 0x43 2. 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

Exercises 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 first 2. Detail the assembly code

Download ppt "Week10 Boolean Instructions on the 8051. Boolean Instructions  Boolean (or Bit) addressable capability is unique to the 8051  Enables efficient handling."

Similar presentations