Professional Documents
Culture Documents
Part 1
8051 C INTRODUCTION
Ps Vs Cs
On the left is a block diagram of a general purpose microprocessor based computer like the PCs and the laptops all of us are familiar with [minus the output device (f.e. CRT display), the input device (f.e. keyboard) and the secondary storage (f.e. hard disk)] To the right is the block diagram of the same computer, implemented on a single chip of silicon. We call this one chip implementation of a computer, or computer-on-a-single-chip, a MICROCONTROLLER
Cs in Embedded Systems
Although the 8051 can have a maximum of 64K bytes of on-chip ROM, many manufacturers have put only 4K bytes or 4096 bytes on the chip. The 8051 became widely popular after Intel allowed other manufacturers to make and market any flavors of the 8051 they please with the condition that they remain code-compatible with the 8051. This has led to many versions of the 8051 with different speeds and amounts of on-chip ROM marketed by more than half a dozen manufacturers.
8052 C
8052 is an advanced 8051. The 8052 an extra 128 bytes of RAM and an extra timer. Therefore, 8052 has 256 bytes of RAM and 3 timers. It has 8K bytes of on-chip program ROM instead of 4K bytes.
8051
8-bit Registers or Byte Registers
16-bit Registers
Simple Instructions
MOV instruction
MOV destination, source copies source to destination MOV A,#55H MOV R0,A MOV R1,A MOV R2,A MOV R3,#95H MOV A,R3 ;load value 55H into reg A ;copy contents of A into R0 ;(A=R0=55H) ;copy contents of A into R1 ;(A=R0=R1=55H) ;copy contents of A into R2 ;(A=R0=R1=R2=55H) ;load value 95H into R3 (R3=95H) ;copy contents of R3 into A (A=R3=95H)
ADD instruction
ADD A, source ADD the source operand to the accumulator register
MOV A,#25H ;load 25H into A MOV R2,#34H ;load 34H into R2 ADD A,R2 ;add R2 to accumulator
ASSEMBLY PROGRAMMING
Structure of Assembly Language Code
ORG 0H MOV R5,#25H MOV R7,#34H MOV A,#0 ADD A,R5 ;start (origin) at 0 ;load 25H into R5 ;load 34H into R7 ;load 0 into A ;add contents of R5 to A ;now A = A + R5 ADD A,R7 ;add contents of R7 to A ;now A = A + R7 ADD A, #12H ;add to A value 12H ;now A = A + 12H HERE: SJMP HERE ;stay in this loop END ;end of asm source file
AND 8051
AND 8051
Par t 2
8051 C DATA PROCESSING INSTRUCTIONS I
ARITHMETIC INSTRUCTIONS
Arithmetic Instructions
Add Subtract Increment Decrement Multiply Divide Decimal adjust
Arithmetic Instructions
Mnemonic ADD A, byte ADDC A, byte SUBB A, byte INC A INC byte INC DPTR DEC A DEC byte MUL AB DIV AB DA A Description add A to byte, put result in A add with carry subtract with borrow increment A increment byte in memory increment data pointer decrement accumulator decrement byte multiply accumulator by b register divide accumulator by b register decimal adjust the accumulator
ADD Instructions
add a, byte ; a a + byte addc a, byte ; a a + byte + C These instructions affect 3 bits in PSW: C = 1 if result of add is greater than FF AC = 1 if there is a carry out of bit 3 OV = 1 if there is a carry out of bit 7, but not from bit 6, or visa versa.
ADD Examples
mov a, #0x3F add a, #0xD3
0011 1111 1101 0011 0001 0010
What is the value of the C, AC, OV flags after the second instruction is executed?
C = 1 AC = 1 OV = 0
7F 127 80 -128 FF -1
0011 1111 (positive) 1101 0011 (negative) 0001 0010 (never overflows)
Subtract
SUBB A, byte subtract with borrow
Example:
SUBB A, #0x4F ; A A 4F C
Notice that there is no subtraction WITHOUT borrow. Therefore, if a subtraction without borrow is desired, it is necessary to clear the C flag.
The increment and decrement instructions do NOT affect the C flag. Notice we can only INCREMENT the data pointer, not decrement.
Multiply
When multiplying two 8-bit numbers, the size of the maximum product is 16-bits FF x FF = FE01 (255 x 255 = 65025)
MUL AB ; BA A * B
Division
Integer Division
DIV AB ; divide A by B A Quotient(A/B), B Remainder(A/B) OV - used to indicate a divide by zero condition. C set to zero
Decimal Adjust
DA a ; decimal adjust a Used to facilitate BCD addition. Adds 6 to either high or low nibble after an addition to create a valid BCD number. Example:
mov a, mov b, add a, DA a #0x23 #0x29 b ; a 23 + 29 = 4C (wanted 52) ; a a + 6 = 52
Par t 3
8051 C DATA PROCESSING INSTRUCTIONS II
LOGIC INSTRUCTIONS
Logic Bitwise logic operations (AND, OR, XOR, NOT) Instr uctions
Clear Rotate Swap
Bitwise Logic
Examples:
00001111 ANL 10101100 00001100 00001111 ORL 10101100 10101111 00001111 XRL 10101100 10100011 CPL 10101100 01010011
a, byte
direct, reg. indirect, reg, immediate
byte, a
direct
byte, #constant
CPL Complement
ex: cpl a
Rotate
Rotate instructions operate only on a rl a mov a, #0xF0 rl a ; a 11110000 ; a 11100001
rrc a
; a A9 ; a BD (10111101), C0 ; a 01011110, C1
Swap
swap a
; a 27h
Shift/Multiply Example
Program segment to multiply by 2 and add 1 clr rl inc c a a
Part 4
JUMP AND LOOP INSTRUCTIONS
In this instruction, the register is decremented; if it is not zero, it jumps to the target address referred to by the label. Prior to the start of the loop the register is loaded with the counter for the number of repetitions. In this instruction both the register decrement and the decision to jump arc combined into a single instruction. The registers can be any of R0 - R7. The counter can also be a RAM location
Part 5
CALL INSTRUCTIONS
CALL INSTRUCTIONS
CALL is used to call a subroutine. Subroutines are often used to perform tasks that need to be performed frequently. This makes a program more structured in addition to saving memory space. There are two instructions : LCALL (long call) and ACALL (absolute call). Deciding which one to use depends on the target address.
CALL INSTRUCTIONS
LCALL (long call)
3-byte instruction, the first byte is the op-code and the second and third bytes are used for the address of the target subroutine. LCALL can be used to call subroutines located anywhere within the 64K-byte address space of the 8051. To make sure that after execution of the called subroutine the 8051 knows where to come back to, the processor automatically saves on the stack the address of the instruction immediately below the LCALL. When a subroutine is called, control is transferred to that subroutine, and the processor saves the PC (program counter) on the stack and begins to fetch instructions from the new location. After finishing execution of the subroutine, the instruction RET (return) transfers control back to the caller. Every subroutine needs RET as the last instruction.
CALL INSTRUCTIONS
LCALL (long call)
CALL INSTRUCTIONS
Part 6
TIMING CALCULATIONS
Table 33
Par t 7
8051 I/O PORT PROGRAMMING
This is a very useful function because the 8051 uC can then be used for monitoring real world processes as we see in digital instruments where different parameters like Pressure, Temperature, Speed of Rotation, etc can be acquired and displayed
This is also very useful because microcontrollers have data processing capability (though limited) which can be used for processing these signals through programs (which we write), and based on the result perform some control function by sending data to the device through the output ports or pins of the microcontroller.
So for example, we can use the input port/pin of the 8051 uC to keep reading the temperature of an oven, and when the temperature exceeds a certain value, we can use the output port/pin of the 8051 uC to either turn-off the switch of the oven automatically or to activate a sound alarm (for manual intervention) and to once again turn it on when the temperature falls below a specified value or to activate a sound alarm.
ANALOGY OF PORTS
We have got sea ports, air ports, bus ports (what are called bus terminals) and rail ports (railway stations) in a city. These ports are used for connecting the city with the rest of the world for bringing people and cargo inside the city (Input Port) They are also used for sending people and cargo outside the city (Output Port) Generally we do not see separate physical ports in cities for Input and Output Functions Now this analogy pretty much applies for the 8051 uC as well
4 I/O Ports in the 8051 are like the 4 railway stations of a big city Individual Pins are like Platforms of the Railway Station You got 8 platforms (pins) per I/O port in 8051!
8051 I/O PORT You have got 4 I/O ports in the 8051 uC. They are PROGRAMMING
named as P0, P1, P2, and P3. You have to always keep in mind that each of these 4 ports have 8 pins each. Individual pins of these ports are named as P0.1, P0.2, P0.3, and so on (Port 0 Platform No.1, Port 0 Platform No.2, and so on) When you RESET the 8051 uC then all the ports of the 8051 are configured as input ports, and are ready to read data, that is, take data inside, that is, all stations and all platforms of the stations are ready for receiving incoming trains!
Port 0
You can use this port as either an input port or an output port. Before using port 0 as an input or an output port, you have to connect each pin of port 0 to 10 Kilo ohm resistors as shown in the diagram on next slide. This is because the port P0 has no internal resistors. The other ports P1, P2, and P3 have internal or builtin resistors on the 8051 chip itself. So external resistors should not be connected to the pins of P1, P2, and P3 ports.
Port 0 as input
After connecting 10Kohm external resistors to all pins of port 0, in the manner shown earlier, in order to make port 0 an input port, the port must be programmed by writing 1 to all its bits.
Port 1 as input
If port 1 has been configured as an output port, to make it an input port again, it must programmed as such by writing 1 to all its bits.
Port 1 as input
In the following code, port 1 is configured first as an input port by writing 1 s to it, then data is received from that port and saved in R7, R6, and R5.
DELAY SUBROUTINE
. . MOV R0, #30H . . ORG 300H DELAY: NOP AGAIN: DJNZ R0, AGAIN RET END
Port 2 Port 2 occupies a total of 8 pins (pins 21 through 28). It can be used as input or output. Port 2 does not need any external resistors since it already has external resistors internally. Upon reset, port 2 is configured as an input port.
Port 2 as input
To make port 2 an input, it must programmed as such by writing 1 to all its bits. In the following code, port 2 is configured first as an input port by writing is to it.
Port 3
Port 3 can be used as input or output. P3 does not need any external resistors. Port 3 is configured as an input port upon reset. Port 3 has the additional function of providing some extremely important signals such as interrupts, serial I/O, timer/counter and read/write control for external memory.
Port 3
Table 44
Single-Bit Instructions
CPU
Data is ANDed with contents of A Result is rewritten to port latch Port Pin Data = Port Latch
Reading from Port: Data is read directly from port pins in case the port is configured as input. Writing to Port: Writes to an internal register (called latch reg) which is reflected to pin if port is configured as output.
Latch
To write data on port Data will be reflected/moved to port pin if configured as o/p, and will have no effect incase of i/p.
Read-modify-write feature
The ports in the 8051 can be accessed by the read-modify-write technique.
(1) reading the port (2) modifying its value (3) writing to the port
Sample Programs
Toggle all bits of P0, P1, and P2 every sec assuming crystal frequency of 11.0592 MHz
Sample Pr og r ams
60% duty cycle sq waveform SETB P1.3 LCALL DELAY LCALL DELAY CLR P1.3 LCALL DELAY SJMP BACK
Oven HERE JNB 2.3 HERE SETB P1.5 CLR P1.5 SJMP HERE
SWITCH STATUS SETB P1.7 AGAIN: JB P1.2 OVER MOV P2, #N SJMP AGAIN OVER: MOV P2, #Y SJMP AGAIN
SWITCH TO P0.1 CHECK SWITCH SWITCH =1 THEN HIGH TO LOW TO ACTIVATE SIREN ON P1.7 SETB P0.1 AGAIN: MOV C, P0.1 JNC AGAIN SETB P1.7 CLR P1.7 SJMP AGAIN
SWITCH TO P1.0 LED TO P2.7 SWITCH STATUS SEND TO LED SETB P1.7 AGAIN: MOV C, P1.0 MOV P2.7, C SJMP AGAIN
Examples:
;cseg stands for code segment cseg at 1000h ;address of next instruction 1000h GREEN_LED equ P1.6 is
Assembler Directives
DATA
SP MY_VAL
Address
EQU Used to create symbols that can be used to represent registers, numbers, and addresses
LIMIT VALUE SERIAL COUNT MY_VAL EQU EQU EQU EQU EQU 2000 LIMIT 200 + 'A' SBUF R5 0x44
dest source
;move byte to accumulator ;move accumulator to byte ;move byte to register of ;current bank ;move byte to internal RAM ;move byte to internal RAM ;with address contained in Rn ;move 16-bit data into data ;pointer
Exchange instructions
XCH a, byte ;exchange accumulator and ;byte XCHD a, byte ;exchange low nibbles of ;accumulator and byte
Addressing Modes
Immediate Mode specify data by its value
mov a, a = mov a, a = mov a, a = mov a, a = #0 ;put 0 in the accumulator 00000000 #0x11 ; put 11hex in the accumulator 00010001 #11 ; put 11 decimal in accumulator 00001011 #77h ; put 77 hex in accumulator 01110111
Addressing Modes
Direct Mode specify data by its 8-bit address
mov a, 0x70 mov 0xD0, a ; copy contents of RAM at 70h to a ; put contents of a into PSW
Addressing Modes
Register Addressing either source or destination is one of R0-R7 mov R0, a mov a, R0
Addressing Modes
Register Indirect the address of the source or destination is specified in registers Uses registers R0 or R1 for 8-bit address:
mov mov mov ; 0xD0, #0 ; use register bank 0 r0, #0x3C @r0, #3 ; memory at 3C gets #3 M[3C] 3
Exer cise: Use Re gister Indir ect to access upper RAM block
Addressing Modes
Register Indexed Mode source or destination address is the sum of the base address and the accumulator. Base address can be DPTR or PC mov dptr, #4000h mov a, #5 movc a, @a + dptr ;a M[4005]
Addressing Modes
Register Indexed Mode Base address can be DPTR or PC
Addr 1000 1002 1003 cseg at 0x1000h mov a, #5 movc a, @a + PC nop ;a M[1008]
PC
A and B Registers
A and B are accumulators for arithmetic instructions They can be accessed by direct mode as special function registers: B address 0F0h A address 0E0h - use ACC for direct mode
Address Modes
mov sp, #0x40 push 0x55 pop b ; ; ; ;
Stack-oriented data transfer another form of register direct addressing, but using SP
Initialize SP SP SP+1, M[SP] M[55] M[41] M[55] b M[55]
Note: can only specify RAM or SFRs (direct mode) to push or pop. Therefore, to push/pop the accumulator, must use acc, not a: push acc push a
Stacks
push pop stack pointer stack
Address Modes
Exchange Instructions two way data transfer XCH a, 0x30 ; a M[30] XCH a, R0 ; a R0 XCH a, @R0 ; a M[R0] XCHD a[3..0] ; exchange a[7..4] a, R0 R0[7..4] R0[3..0] digit Only 4 bits exchanged
Address Modes
Bit-Oriented Data Transfer transfers between individual bits. SFRs with addresses ending in 0 or 8 are bit-addressable. (80, 88, 90, 98, etc) Carry flag (C) (bit 7 in the PSW) is used as a single-bit accumulator RAM bits in addresses 20-2F are bit addressable
Address B
Register SPI0CN ADC0CN ACC PCA0CN PSW T2CON SMB0CN IP P3 IE P2 SCON P1 TCON P0
0xF8 0xF0 0xE8 0xE0 0xD8 0xD0 0xC8 0xC0 0xB8 0xB0 0xA8 0xA0 0x98 0x90 0x88 0x80
Figure 1
Figure 2
Figure 3
Figure 5
Table 2
Figure 6
Figure 7
(a) Inside MAX232 (b) its Connection to the 8051 (Null Modem)
Figure 8
(a) Inside MAX233 (b) Its Connection to the 8051 (Null Modem)
Table 3
PC Baud Rates
Table 4
Example 1 With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud rates. (a) 9600 (b) 2400 (c) 1200 machine cycle frequency = 11.0592 MHz / 12 = 921.6 kHz Timer 1 frequency provided by 8051 UART = 921.6 kHz / 32 = 28,800 Hz
(a) 28,800 / 3 = 9600 (b) 28,800 / 12 = 2400 (c) 28,800 / 24 = 1200 where -3 where -12 where -24 = FD (hex) = F4 (hex) = E8 (hex)
when a byte is written, it is framed with the start and stop bits and transferred serially via the TxD pin when the bits are received serially via RxD, it is deframe by eliminating the stop and start bits, making a byte out of the data received, and then placing it in the SBUF
Figure 9
SM0 and SM1 determine the mode only mode 1 is important when mode 1 is chosen, the data framing is 8 bits, 1 stop bit, and 1 start bit compatible with the COM port of PCs mode 1 allows the baud rate to be variable and is set by Timer 1 of the 8051 for each character a total of 10 bits are transferred, where the first bit is the start bit, followed by 8 bits of data, and finally 1 stop bit.
RI (receive interrupt)
when the 8051 receives data serially via RxD, it places the byte in the SBUF register then raises the RI flag bit to indicate that a byte has been received and should be picked up before it is lost
Example 2 Write a program to transfer letter "A" serially at 4800 baud, continuously.
Example 3 Write a program to transfer the message "YES" serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.
Example 4 Program the 8051 to receive bytes of data serially, and put them in P1. Set the baud rate at 4800, 8-bit data, and 1 stop bit.
Table 5