Introduction Difference between microcontroller and microprocessor Criteria for choosing a microcontroller Overview of 8051 microcontroller Inside the 8051
Inside the computer
Microprocessor CPU for Computers Commonly no RAM, ROM, I/O on CPU chip itself More versatile More costly General Purpose CPU Microcontroller A single-chip computer On-chip RAM, ROM, I/O ports... Example:Motorola s 6811, Intel s 8051, Zilog s Z8 and PIC 16X Less Versatile Less costly Special purpose CPU
Many chips on motherboard
Microcontrollers and microprocessors are widely used in embedded system products. An embedded product uses microcontroller or microprocessor to do one task and one task only. Example: Printer, camera, mouse etc. For this reason it optimizes the processors used for embedded systems. That s why this processor is known as high end embedded processor.
The need of the microcontroller based, whether 8 bit, 16 bit or 32 bit. Speed Packaging : DIP (Dual in line package), QFP (quad flat package) Power Consumption The amount of ROM and RAM chip The number of I/O pins and timer on chip Cost per Unit
1)8051 have 128 bytes of RAM 2) it consist of 16 bit address bus 3) ROM on 8051 is 4 Kbytes in size 4) it also consist of Two 16 bit Timer/ Counter 5) it consist of 16-bit program counter and data pointer
Register are used to store information temporarily an address pointing to the data to be fetched The most widely used registers A (Accumulator) For all arithmetic and logic instructions B, R0, R1, R2, R3, R4, R5, R6, R7 DPTR (data pointer), and PC (program counter)
In the early days of the computer, programmers coded in machine language, consisting of 0s and 1s Assembly language is referred to as a low level language [label:] Mnemonic [operands] [;comment] Directives (or pseudo-instructions) : Give directions to the assembler
1. First we use an editor to type a program having the extension asm. 2. The asm source file containing the program code created in step 1 is fed to an 8051 assembler. The assembler will produce an object file and a list file The extension for the object file is obj while the extension for the list file is lst. 3. The linker program takes one or more object code files and produce an absolute object file with the extension abs. 4. Next the abs file is fed into a program called OH (object to hex converter) which creates a file with extension hex that is ready to burn into ROM
The program counter is 16 bits wide. This means that it can access program addresses 0000 to FFFFH, a total of 64K bytes of code Program Counter has the value of 0000 when powered up.
ROM Address ORG 0H 0000 7D25 MOV R5,#25H 0000 7F34 MOV R7,#34H ; 0002 7400 MOV A,#0 ; 0004 2D ADD A,R5 ; 0006 2F ADD A,R7 ; 0007 2412 ADD A,#12H ; 0008 80EF HERE: SJMP HERE ; 000A END 000C
Define Byte ORG 500H DATA1: DB 28 ;DECIMAL (1C in Hex) DATA2: DB 00110101B ;BINARY (35 in Hex) DATA3: DB 39H ;HEX ORG 510H DATA4: DB 2591 ;ASCII NUMBERS ORG 518H DATA6: DB My name is Joe
Example: Use assembler directives to place constants 0FCH, 05H, 76H, 28D and character string SAM in consecutive program memory locations beginning from location 0050H ORG 0050H DB 0FCH, 05H, 76H, 28 DB SAM
ORG (origin) END EQU (equate) COUNT EQU 25 MOV R3, #COUNT
There are 128 bytes of RAM in the 8051 The 128 bytes are divided into three different groups
SP=07 PUSH A is invalid must be coded as PUSH 0E0H PUSH 1 ; pushes the register R1 onto the stack Popping :With every POP, the top byte of the stack is copied onto the register specified by the instruction. POP 3 ; POP stack into R3 POP 5
MOV R6,#25H MOV R1, #12H MOV R4, #0F3H PUSH 6 PUSH 1 PUSH 4
Example Write instruction to use the registers of bank 3, and load the same value 05H in the registers R0 to R3
MOV destination,source 8/16 MOVC A,@A+DPTR bit operation MOVX ; it is used for external memory access ADD A, source JNB P1.0, label JB P1.0, label CPL P1.0 Bit Instructions SETB P1.0 CLR P1.0 INC R0 DEC R0 DJNZ resister, label SJMP label Loop Instructions LCALL label
PUSH 3 POP 3
Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips
Example 1 Write the following programs. Create a square wave of 50% duty cycle on bit 0 of port1.
Example 2 Write a program to perform the following: (a) Keep monitoring the P0.1 bit until it becomes high (b) When P0.1 becomes high, read the data from port1 (c) Send a high-to-low (H-to-L) pulse to P0.2 Example 3 Assume that bit P2.3 is an input and represents the condition of an oven. 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.
Example 4 A switch is connected to pin P1.7. Write a program to check the status of SW and perform the following: (a) If SW=0, send letter N to P2 (b) If SW=1, send letter Y to P2 Example 5 A switch is connected to pin P0.1. Write a program to check the status of SW and perform the following: (a) If SW=1, send high to low pulse to activate a siren connected to pin P1.7 Use the carry flag to check the switch status.
Immediate Register Direct Register Indirect Indexed
The entire 128 bytes of RAM can be accessed The register bank locations are accessed by the register names MOV A,4 ; MOV A,R4 Contrast this with immediate addressing mode MOV R0,40H ; MOV 56H,A
The source operand is a constant The immediate data must be preceded by the pound sign, # Can load information into any registers, including 16-bit DPTR register DPTR can also be accessed as two 8-bit registers, the high byte DPH and low byte DPL
MOV A,#25H ; MOV R4,#62 ; MOV B,#40H ; MOV DPTR,#4521H ; MOV DPL,#21H ; MOV DPH,#45H ; ;illegal!! Value > 65535 (FFFFH) MOV DPTR,#68975
MOV A,R0 ; MOV R2,A ; ADD A,R5 ; ADD A,R7 ; MOV R6,A The source and destination registers must match in size MOV DPTR,A will give an error MOV DPTR,#25F5H MOV R7,DPL MOV R6,DPH The movement of data between registers is not allowed MOV R4,R7 is invalid
A register is used as a pointer to the data Only register R0 and R1 are used for this purpose When R0 and R1 hold the addresses of RAM locations, they must be preceded by the @ sign MOV A,@R0 ; MOV @R1,B
Q. Write a program to copy the value 55H into RAM memory locations 40H to 41H using (a) direct addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop
Q. Write a program to clear 16 RAM locations starting at RAM address 60H
Indexed addressing mode is widely used in accessing data elements of look-up table entries located in the program ROM The instruction used for this purpose is MOVC A,@A+DPTR Use instruction MOVC, C means code
Q. The word SAM is to be burned in the flash ROM location starting from 0400H. Write a program to do this and to read this data into internal RAM locations starting from 60H.
Sol. ORG 0000H ; MOV DPTR,#0400H ; CLR A ; MOVC A,@A+DPTR ; MOV 60H,A ; INC DPTR ; CLR A ; MOVC A,@A+DPTR ; MOV 61H,A ; INC DPTR ; CLR A ; MOVC A,@A+DPTR ; MOV 62H,A ; Here: SJMP HERE ;stay here ;Data is burned into code space starting at 400H ORG 400H MYDATA:DB USA END ;
Write a program to get the x value from P1 and send x 2 to P2, continuously
Solution: ORG 0 MOV DPTR,#300H ; MOV A,#0FFH ; MOV P1,A ; BACK:MOV A,P1 ; MOV A,@A+DPTR ; MOV P2,A ; SJMP BACK ; ORG 300H XSQR_TABLE: DB 0,1,4,9,16,25,36,49,64,81 END