1 / 28

Week 6 and 7

Week 6 and 7. Logical Operations. Logical Instructions. Logical instructions perform Boolean operations such as AND and OR operations on byte values In order to adhere to convention, a byte with its bit locations is below.

abby
Download Presentation

Week 6 and 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 6 and 7 Logical Operations

  2. Logical Instructions • Logical instructions perform Boolean operations such as AND and OR operations on byte values • In order to adhere to convention, a byte with its bit locations is below. • Note that bit 0 is the least significant bit (LSB) and bit 7 is the most significant bit (MSB)

  3. Logical Instructions: ANL • Logical AND can be used to set selective bits to a zero and leave other bits unchanged • All 8 bits of each operand involved • Consider the instruction ANL A,#data • Which ANDs a constant value with the contents of the accumulator and stores the result in the accumulator • For example, if A contains 5AH, then the instruction ANL A,#06H will produce the result 02H in A • ANDing is a good way to mask off certain bits 01011010 00000110 00000010

  4. Logical Instructions: ORL • The logical OR instruction operates in a similar manner • Instead of masking off bits, it can be used to set selective bits in a byte that are clear beforehand • Consider the instruction ORL A,#data • Which ORs a constant value with the contents of the accumulator and stores the result in the accumulator • For example, if A contains 5AH, then the instruction ORL A,#06H will produce the result 5EH in A • Note that this example will set bits 1 and 2 to a one irregardless of their previous value. All other bits are unaltered. 01011010 00000110 01011110

  5. Logical Instructions: XRL • The logical XOR instruction operates by performing exclusive operations on a bit by bit basis • it can be used to toggle (invert) selective bits in a byte • Consider the instruction XRL A,#data • Which XORs a constant value with the contents of the accumulator and stores the result in the accumulator • For example, if A contains 5AH, then the instruction XRL A,#06H will produce the result 5CH in A • Note that this example inverts bits 1 and 2. All other bits are unaltered. 01011010 00000110 01011100

  6. Logical Instructions: Full details for ANL, ORL and XRL • Just like the arithmetic instructions, there are a number of variations, They are ANL A,source ORL A,source XRL A,source ANL A,#data ORL A,#data XRL A,#data ANL direct,A ORL direct,A XRL direct,A ANL direct,#data ORL direct,#data XRL direct,#data • Where source can be any of Rn, direct or @Ri

  7. Logical Instructions: Rotate instructions • As well as the boolean type instructions, it is common for processors to support shift and/or rotate instructions • Perform power of 2 mul/div quickly • Shift instructions most useful • Extract bits from value • Can be used for some serial I/O applications • The 8051 supports rotate instructions • These rotate instructions shift the contents of the accumulator to the left or right, one bit a time • All rotate instructions operate on A and store the rotate values in A

  8. Logical Instructions: RL A • The instruction is called the Rotate Left (RL) RL A • And has the effect of rotating the bits to the left, with the MSB fedback into the LSB. • Example • Before: 11101100 • After: 11011001

  9. Logical Instructions: RLC A • Rotate Left through Carry flag (RLC) RLC A • And has the effect of rotating the bits to the left through the carry bit, with the carry bit fedback into the LSB. • Example • Before: 1 01101100 • After: 0 11011001

  10. Logical Instructions: Other Rotate instructions • Can also rotate right RR A RRC A • Same ideas as before, except rotation is right

  11. Example • Given A, extract bits 3 and 4 as a 2 bit value in A ANL A,#18H RR A RR A RR A

  12. Example • Given a value in memory location 22H, determine the number of ones in the stored value. MOV R1,#0 MOV R0,#8 CLR C Loop: MOV A,22H RRC A MOV 22H,A MOV A,#0 ADDC A,R1 MOV R1,A DJNZ R0,loop

  13. SFRs • Special Function Registers (SFRs) are 8 bit registers that have a special purpose • One such purpose is that certain SFRs are used to control/interact with hardware components • An example is the use of an SFR to control an I/O pin that turns on a DC motor • Another example is the use of an SFR to read a switch input • There are many different SFRs. • The objective initially is to understand how to access an SFR and then to see some simple examples

  14. Data Memory (Revisted) • Data memory is shown here • Remember that the upper 128 bytes are accessible only through indirect addressing (@Ri) • The lower 128 bytes can use both direct and indirect addressing • In effect, so far we have only seen 128 bytes (7-bit address) that are accessible using direct addressing??? • Well, there are more!!!

  15. SFRs • From the programmer’s model view, SFRs appear as memery locations with addresses in the range 80H to FFH • Even though there are already 128 locations with same addresses, this is still possible • Reason: SFRs are accessed using only direct addressing, while the upper 128 bytes of data memory is accessible using only indirect addressing.

  16. This appears confusing. Why is it like this?? SFRs • Given R0 with the value 82H, consider the following two instructions MOV A,82H MOV A,@R0 • The first instruction accesses address 82H in the SFR section • The second instruction accesses address 82H in the upper 128 bytes of data memory

  17. SFRs • It is not possible to use these locations for general purpose storage • Each of the possible 128 SFRs has a particular function • One particular SFR at address 90H is used to control I/O port P1 • I/O port P1 refers to 8 I/O pins and their associated output section • Each one of the eight I/O pins can be individually controlled through the SFR at address 90H • Microcontrollers have I/O ports on-chip whereas microprocessors require additional external H/W for I/O ports

  18. Example ;******************************************************************** ; Author : R. Conway ; Date : Feb 2000; ; File : led.a51; ; Hardware : Any 8051 based Microcontroller; ; Description : Switches the port pin P1.4 on and off with ; 200mSec period @ 50% duty cycle. The pin could be ; used to turn on and off a LED ; ; Vcc ; | ; 8051 |-| ; ---- | | 270R ; | | | ; | |_| ; | |\ / | ; P1.4 |-|->o---(/)--- ; | |/ \// LED ; ;********************************************************************

  19. ;____________________________________________________________________;____________________________________________________________________ ; MAIN PROGRAM MOV 90H,#10H MOV A,#10H main: XRL 90H,A ; toggle (complement) bit 4 of port 1 JMP main ;____________________________________________________________________ Example EXCITING: THIS IS OUR FIRST REAL OUTPUT FROM THE 8051!!! • This program will cause the external pin called P1.4 to toggle high and low at a very high speed (for the 8051!). • With this pin controlling an LED, it is not possible to see it go on and off at such a high speed • Connect a scope probe to the pin on the 8051 and see the waveform

  20. Example: Output

  21. Can we make the LED go on and off at a slower speed? Yes: By introducing a delay between toggling pin 4 of port 1. Use a software delay MOV 90H,#10H MOV A,#10H main: XRL 90H,A ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1: MOV R6,#250 ; 250*2us=500us dy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay) JMP main Example

  22. Example: Output

  23. Example: More Readable code MOV P1,#10H MOV A,#10H main: XRL P1,A ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1: MOV R6,#250 ; 250*2us=500us dy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay) JMP main P1 equ 90H

  24. Example: Using Simulator

  25. SFRs • SFRs are located at addresses in the range 80H to FFH. However, not all addresses are used. • Two views

  26. Exercises • Detail using instructions how to • Zero the 4 least significant bits in R1, leaving the 4 most significant bits unchanged • Set bits 0,1 and 3 of A to a one, leaving remaining bits unchanged • Set bits 0 and 3 of A to a one, bits 2 and 3 to a zero and remaining bits unchanged • Invert all the bits of A • Jump to the location labelled go, if bits 7 or 3 of A are 1. • Jump to the location labelled go, if bits 7 and 3 of A are 1.

  27. Exercises • How can the SFR section use the same addresses as the upper 128 bytes of RAM? • Can the SFR section be used for general purpose storage? • Give an example of 4 special purpose registers • List their addresses • List their symbolic names • Detail a complete program that toggles port 2.1 and 2.4 (at same time) as fast as possible

  28. Exercises • Detail a complete program that turns port 2.0 on for 100ms, then turns port 2.0 off and port 2.1 on for 100ms, then turns port 2.1 off and port 2.2 on for 100ms and so on up to port 2.7 and then restarts back at port 2.0. Develop a flowchart solution first!

More Related