5 Bit-addressability (1/2) In most microprocessors, data is accessed in byte-sized chunks.However, in many applications we need to change one bit, for example, to turn on or off a device.The bit-addressability of the 8051The ability to access data in single bit instead of the whole byte.Make 8051 become one of the most powerful 8-bit microprocessors.
6 Bit-addressability (2/2) Which portions of the microprocessor, I/O ports, registers, RAM, or ROM are bit-addressable?ROM holds program code for executions. There is no need for bit-addressability.I/O: all ports are bit-addressableRegister: some of them are bit-addressableRAM: bit-addressable RAM (20H-3FH)
7 I/O Ports and Bit-addressability We can access either the entire 8 bits or any single bit without altering the rest.Example:SETB P2.3 ;set pin 3 of P2 to high (1)CLR P1.0 ;clear pin 0 of P1(0)Remember that D0 is the LSB and D7 is the MSB.
8 Example 8-1 (a-1) Write the following program. Create a square wave of 50% duty cycle on bit 0 of port 1.Solution:The 50% duty cycle means that the period of “on” state is 50% in the period of the whole pulse (“on” state pluses “off” state).That is, the “on” and “off” state have the same length.8051P1.0on state high portionoff state low portion50%50%whole pulse
9 Example 8-1 (a-2)Create a square wave of 50% duty cycle on bit 0 of port 1.HERE:SETB P1.0 ;set to high bit 0 of port 1LCALL DELAY ;call the delay subroutineCLR P1.0 ;P1.0=0LCALL DELAYSJMP HEREAnother way to write the above program is:HERE:CPL P1.0 ;complement bit 0 of port 1
10 Example 8-1 (b-1) Write the following programs. (b) Create a square wave of 66% duty cycle on bit 3 of port 1.Solution:The 66% duty cycle means that the period of “on” state is 66% in the period of the whole pulse.That is the “on” state is twice the “off” state.8051P1.366%34%
11 Example 8-1 (b-2)(b) Create a square wave of 66% duty cycle on bit 3 of port 1.BACK:SETB P1.3 ;set port 1 bit 3 highLCALL DELAYLACLL DELAYCLR P1.3 ;clear bit 2 of port 1SJMP BACK
12 Bit Address of I/O Ports Each port has its byte address.Ex: P0 has address 80H.Each pin of port has its bit address.Ex: pin 7 of port 0 has bit address 87HNote that pin 0 of port 0 has address 80H. Your instruction decides that the operand is bit or byte.The bit address of portsSee Table 8-2 See Figure 8-1 or Figure A-1 for SFR RAM address
13 Table 8-2: Single-Bit Addressability of Ports Port’s BitP0.0P1.0P2.0P3.0D0P0.1P1.1P2.1P3.1D1P0.2P1.2P2.2P3.2D2P0.3P1.3P2.3P3.3D3P0.4P1.4P2.4P3.4D4P0.5P1.5P2.5P3.5D5P0.6P1.6P2.6P3.6D6P0.7P1.7P2.7P3.7D7
14 Figure 8-1. SFR RAM Address (Byte and Bit) (1/2) Special Function RegistersByte addressBit addressF7 F6 F5 F4 F3 F2 F1 F0E7 E6 E5 E4 E3 E2 E1 E0D7 D6 D5 D4 D3 D2 D1 D0A7 A6 A5 A4 A3 A2 A1 A0AF AC AB AA A9 A8B7 B6 B5 B4 B3 B2 B1 B0BC BB BA B9 B8FFF0E0D0B8B0A8A0BACCPSWIPP3IEP2
15 Figure 8-1. SFR RAM Address (Byte and Bit) (2/2) Special Function Registers9F 9E 9D 9C 9B 9AD7 D6 D5 D4 D3 D2 D1 D0A7 A6 A5 A4 A3 A2 A1 A0not bit addressable9998908D8C8B8A89SBUFSCONTH1TL0P1TMODDPH888783828180TH0TL1TCONPCONDPLSPP0
16 Example 8-2For each of the following instructions, state which bit of which SFR will be affected. Use Figure 8-1.(a) SETB 86H (b) CLR 87H (c) SETB 92H(d) SETB 0A7H (e) CLR 0F2H (f) SETB 0E7HSolution:(a) SETB 86H is for SETB P0.6(b) CLR 87H is for CLR P0.7(c) SETB 92H is for SETB P1.2(d) SETB 0A7H is for SETB P2.7(e) CLR 0F2H is for CLR D2 of register B(f) SETB 0E7H is for SETB ACC.7 (D7 of register A)
17 Checking an Input BitTo monitor a bit and make a decision depending on where it is 0 or 1JB bit,target ; jump if bit setHERE: JB P2.1,HERE ;jump to HERE if P2.1=1JNB bit,target ; jump if bit not setHERE: JNB P2.1,HERE ;jump to HERE if P2.1=0See Example 8-3
18 Example 8-3Assume that bit P2.3 is an input and represents the condition of anoven. If it goes high, it means that the oven is hot.Monitor the bit continuously.Whenever it goes high, send a high-to-low pulse to port P1.5 to turn on a buzzer.Solution:HERE:JNB P2.3,HERE ;keep monitoring for highSETB P ;CLR P ;
19 Registers and Bit-Addressability Only registers B, A, PSW, IP, IE, SCON, TCON are bit-addressable.See Figure 8-1 Examples 8-4 and 8-5 use JNB, JB to monitor one bit.Examples 8-6 and 8-7 control bits of PSW.
20 Example 8-4Write a program to see if the accumulator contains an even number.If so, divide it by 2.If not, make it even and then divide it by 2.Solution:MOV B,# ;B=2JNB ACC.0,YES ;If A=0, jump to YESINC A ;it is odd, make it evenYES:DIV AB ;A/B
21 Example 8-5Write a program to see if bits 0 and 5 of register B are 1. If they arenot, make them so and save it in R0.Solution:Use bit address of B.0 and B.5:JB 0F0H,NEXT_1 ;jump if B.0=1SETB 0F0H ;NEXT_1: JB 0F5H,NEXT_2 ;jump if B.5=1SETB 0F5H ;NEXT_2: MOV R0,B ;save to R0
22 Example 8-6 Write a program to save the accumulator in R7 of bank 2. Solution:CLR PSW.3 ; Register bank 2SETB PSW.4MOV R7,ACYAC--RS1RS0OVPPSWRS1RS0Register BankAddress00H – 07H108H – 0FH210H – 17H318H – 1FH
23 Example 8-7While there are instructions such as JNC and JC to check the carry flag bit (CY), there are no such instructions for the overflow flag bit (OV).How would you write code to check OV?Solution:The OV flag is PSW.2 of the PSW register.We can use the following instruction to check the OV flag.JB PSW.2,TARGET ;jump if OV=1
24 Bit-addressable RAM The bit-addressable RAM locations are 20H to 2FH. Only 16 bytes of RAM are bit-addressable.16 * 8 bits = 128 bits (in decimal) = 80H bits (in hex)They are addressed as 00 to 7FHNote that the bit addresses 80H to F7H belong to SFR.See Figure 8-3
25 Figure 8-3. 128 Bytes of Internal RAM Byte address7FGeneral purpose RAM30Bit-addressable locations2F7F7E7D7C7B7A797877767574737271706F6E6D6C6B6A696867666564636261605F5E5D5C5B5A595857565554535251504F4E4D4C4B4A494847464544434241403F3E3D3C3B3A393837363534333231302F2E2D2C2B2A292827262524232221201F1E1D1C1B1A191817161514131211100F0E0D0C0B0A090807060504030201002E2D2C2B2A292827262524232221201FBank 31817Bank 2100FBank 10807Default register bank for R0 - R700
26 Example 8-8 Find out to which byte each of the following bits belongs. Give the address of the RAM byte in hex.(a)SETB 42H;set bit 42H to (d)SETB 28H;set bit 28H to 1(b)CLR 67H;clear bit (e)CLR 12 ;clear bit 12 (decimal)(c)CLR 0FH;clear bit 0FH (f)SETB 05Solution:(a) RAM bit address of 42H belongs to D2 of RAM location 28H(b) RAM bit address of 67H belongs to D7 of RAM location 2CH(c) RAM bit address of 0FH belongs to D7 of RAM location 21H(d) RAM bit address of 28H belongs to D0 of RAM location 25H(e) RAM bit address of belongs to D4 of RAM location 21H(f) RAM bit address of belongs to D5 of RAM location 20H
27 Example 8-9The states of bits P1.2 and P1.3 of I/O port P1 must be saved beforethey are changed. Write a program to save the status of P1.2 in bitlocation 06 and the status of P1.3 in bit location 07.Solution:CLR ;clear bit address 06CLR ;clear bit address 07JNB P1.2,OVER ;If P1.2=0,jumpSETB ;OVER:JNB P1.3,NEXT ;If P1.3=0,jumpSETB ;NEXT:...
29 Single-bit Operations Instructions that are used for single-bit operations are given in Table 8-1.These instructions can be used for any bit. Some instructions that allow single-bit operations, but only along with the carry flag (CY).In 8051, there are several instructions by which the CY flag can be manipulated directly.See Table 8-3.
30 Table 8-1: Single-Bit Instructions FunctionSETBbitSet the bit (bit = 1)CLRClear the bit (bit = 0)CPLComplement the bit (bit = NOT bit)JBbit,targetJump to target if bit = 1 (jump if bit)JNBJump to target if bit = 0 (jump if no bit)JBCJump to target if bit = 1, clear bit (jump if bit, then clear)
31 Table 8-3: Carry Bit-Related Instructions FunctionSETB Cmake CY = 1CLR Cclear carry bit (CY = 0)CPL Ccomplement carry bitMOV b,Ccopy carry status to bit location (CY = b)MOV C,bcopy bit location status to carry (b = CY)JNC targetjump to target if CY = 0JC targetjump to target if CY = 1ANL C,bitAND CY with bit and save it on CYANL C,/bitAND CY with inverted bit and save it on CYORL C,bitOR CY with bit and save it on CYORL C,/bitOR CY with inverted bit and save it on CY
32 Example 8-10Write a program to save the status of bits P1.2 and P1.3 on RAMbit locations 6 and 7, respectively.Solution:CY is used as a bit buffer.MOV C,P1.2 ;save status of P1.2 on CYMOV 06,C ;save in RAM bit location 06MOV C,P1.3 ;save status of P1.3 on CYMOV 07,C ;save in RAM bit location 07
33 Example 8-11 (1/2)Assume that RAM bit location 12H holds the status of whether there has been a phone call or not.If it is high, it means there has been a new call since it was checked the last time. Write a program to display “New Message” on an LCD if bit RAM 12H is high.If it is low, the LCD should say “No New Messages”.Solution:Use CY to hold the status.Use JNC to check CY flag.We use “LCALL DISPLAY” to display message (see Chap. 12)
34 Example 8-11 (2/2) MOV C,12H ;copy bit location 12H to JNC NO ;check to see if is highMOV DPTR,#400H ;address of YES_MGLCALL DISPLAY ;displaySJMP EXIT ;get outNO: MOV DPTR,#420HLCALL DISPLAYEXIT:;data to be displayed on LCDORG 400HYES_MG: DB “New Messages”ORG 420HNO_MG: DB “No New Messages”MOV C, 12HJump if CY＝0TestNot Jump if CY ≠0Display NO_MGDisplay YES_MGSJMP EXITEXIT
35 Example 8-12Assume that the bit P2.2 is used to control the outdoor light and bitP2.5 to control the light inside a building. Show how to turn on theoutside light and turn off the inside one.Solution:STEB C ;CY=1ORL C,P2.2 ;CY = P2.2 ORed with CYMOV P2.2,C ;turn it “on”CLR C ;CY=0ANL C,P2.5 ;CY=P2.5 ANDed with CYMOV P2.5,C ;turn it off
37 Reading Ports When reading ports, there are two possibilities: Read the status of the input pinRead the internal latch of the output pinReaders must study and understand the material on the internal working of ports that is given in Appendix C2.See Chapter 4.Table 8-4 shows instructions to read external pinsTable 8-5 shows instructions to read latches
38 Table 8-4: Instructions For Reading an Input Port MnemonicsExamplesDescriptionMOV A,PXMOV A,P2Bring into A the data at P2 pinsJNB PX.Y,..JNB P2.1,TARGETJump if pin P2.1 is lowJB PX.Y,..JB P1.3,TARGETJump if pin P1.3 is highMOV C,PX.YMOV C,P2.4Copy status of pin P2.4 to CY
40 Read-modify-write Feature The sequence of actions taken：1. CPU reads the latch of the port2. CPU perform the operation3. Modifying the latch4. Writing to the pinNote that 8 pins of P1 work independently.All read-modify-write instructions use the ports as the destination operand.That is, these ports are configured as output ports.
41 You are able toList the 8051 Assembly language instructions for bit manipulationCode 8051 instructions for bit manipulation of portsExplain which 8051 registers are bit-addressableDescribe which portions of the 8051 RAM are bit-addressableDiscuss bit manipulation of the carry flagDescribe the carry flag bit-related instructions of the 8051
42 Homework Chapter 8 Problems：7,16,24,25,40,41,44 Note: Please write and compile the program of Problems 7,40,41,44.