MA1
MA1
MA1
Microprocessors
CPU for Computers No RAM, ROM, I/O on CPU chip itself Example: Intel's x86, Motorolas 680x0
Microcontroller
A smaller computer On-chip RAM, ROM, I/O ports... Example: Motorolas 6811, Intels 8051, Zilogs Z8 and PIC
Microcontroller
CPU, RAM, ROM, I/O and timer are all on a single chip
Fix amount of on-chip ROM, RAM, I/O ports For applications in which cost, power and space are critical
Versatility
General-purpose
Not Expansive
Single-purpose
6
Office
Telephones, computers, security systems, fax machines, microwave, copier, laser printer, color printer, paging etc.
Auto
Trip computer, engine control, air bag, ABS, instrumentation, security system, transmission control, entertainment, climate control, cellular phone, keyless entry
7
Choosing a Microcontroller
8-bit microcontrollers
Motorolas 6811 Intels 8051 Zilogs Z8 Microchips PIC
There are also 16-bit and 32-bit microcontrollers made by various chip makers
The 8051 family has the largest number of diversified (multiple source) suppliers
Intel (original) Atmel Philips/Signetics AMD Infineon (formerly Siemens) Matra Dallas Semiconductor/Maxim
10
8051 Microcontroller
Intel introduced 8051, referred as MCS- 51, in 1981. The 8051 is an 8-bit processor
The CPU can work on only 8 bits of data at a time
The 8051 became widely popular after allowing other manufactures to make and market any flavor of the 8051.
12
8051 Family
The 8051 is a subset of the 8052 The 8031 is a ROM-less 8051
Add external ROM to it You lose two ports, and leave only 2 ports for I/O operations
13
8051 Features
64KB Program Memory address space 64KB Data Memory address space 4K bytes of on-chip Program Memory 128 bytes of on-chip Data RAM 32 bidirectional and individually addressable 1/0 lines Two 16-bit timer/counters Full duplex UART 6-source/5-vector interrupt structure with two priority levels On-chip clock oscillator
14
Some companies provide a 20-pin version of the 8051 with a reduced number of I/O ports for less demanding applications
15
16
17
18
19
RST
RESET pin is an input and is active high (normally low) Upon applying a high pulse to this pin, the microcontroller will reset and terminate all activities This is often referred to as a power-on reset Activating a power-on reset will cause all values in the registers to be lost
20
RST
In order for the RESET input to be effective, it must have a minimum duration of 2 machine cycles. In other words, the high pulse must be high for a minimum of 2 machine cycles before it is allowed to go low.
21
EA
EA, external access, is an input pin and must be connected to Vcc or GND The 8051 family members all come with onchip ROM to store programs and also have an external code and data memory. Normally EA pin is connected to Vcc EA pin must be connected to GND to indicate that the code or data is stored externally.
22
24
Port 0
Port 0 is also designated as AD0-AD7. When connecting an 8051 to an external memory, port 0 provides both address and data. The 8051 multiplexes address and data through port 0 to save pins. ALE indicates if P0 has address or data.
When ALE=0, it provides data D0-D7 When ALE=1, it has address A0-A7
25
Port 3
Port 3 can be used as input or output. Port 3 has the additional function of providing some extremely important signals
27
Vss Vcc
P0.0 - P0.7
I I
I/O
Ground: 0 V reference. Power Supply: This is the power supply voltage for normal, idle, and power-down operation.
Port 0: Port 0 is an open-drain, bi-directional I/O port. Port 0 is also the multiplexed low-order address and data bus during accesses to external program and data memory. Port 1: Port I is an 8-bit bi-directional I/O port. Port 2: Port 2 is an 8-bit bidirectional I/O. Port 2 emits the high order address byte during fetches from external program memory and during accesses to external data memory that use 16 bit addresses. Port 3: Port 3 is an 8 bit bidirectional I/O port. Port 3 also serves special features as explained.
28
I/O I/O
P3.0 - P3.7
I/O
EA*/VPP
4K ROM
128 B RAM
Timer 0 Timer 1
CPU
Bus Control
OSC
4 I/O Ports
Serial Port
TXD RXD
P0
P1
P2
P3
8051 Microcontroller
31
60K
64K
External
SFR
64K
EXT
EA = 0
INT
EA = 1
4K
Program Memory
34
8051 Microcontroller
35
C AC F0 RS1 RS0 OV F1 P
Carry Auxiliary Carry User Flag 0 Register Bank Select Parity User Flag 1 Overflow
8051 Microcontroller
36
8051 Microcontroller
37
BIT Addressable 1. A total of 32 bytes from locations 00 to 1F Area 128 BYTE hex are set aside for register banks and the INTERNAL RAM stack. Reg Bank 3
2. A total of 16 bytes from locations 20H to 2FH Reg Bank 2 are set aside for bit-addressable read/write Register Banks memory. Reg Bank 1 3. A total of 80 bytes from locations 30H to 7FH Reg Bank 0 are used for read and write storage, called scratch pad.
8051 Microcontroller 38
8051 Microcontroller
39
R0 R0 R0 R0
R1 R1 R1 R1
R2 R2 R2 R2
R3 R3 R3 R3
R4 R4 R4 R4
R5 R5 R5 R5
R6 R6 R6 R6
R7 R7 R7 R7
Bank 1
Bank 0
8051 Microcontroller
40
8051 Microcontroller
41
8051 Microcontroller
42
8051 Stack
The stack is a section of RAM used by the CPU to store information temporarily.
This information could be data or an address
The register used to access the stack is called the SP (stack pointer) register
The stack pointer in the 8051 is only 8 bit wide, which means that it can take value of 00 to FFH When the 8051 is powered up, the SP register contains value 07 RAM location 08 is the first location begin used for the stack by the 8051
8051 Microcontroller 43
8051 Stack
The storing of a CPU register in the stack is called a PUSH
SP is pointing to the last used location of the stack As we push data onto the stack, the SP is incremented by one This is different from many microprocessors
Loading the contents of the stack back into a CPU register is called a POP
With every pop, the top byte of the stack is copied to the register specified by the instruction and the stack pointer is decremented once
8051 Microcontroller
44
8051 Microcontroller
45
8051 Microcontroller
46
8051 Microcontroller
47
8051 TIMERS
8051 Microcontroller 49
8051 Timer/Counter
OSC 12
C /T 0
TLx
THx
TFx
(1 Bit)
C /T 1
(8 Bit) (8 Bit)
T PIN
TR
Gate
INT PIN
8051 Microcontroller
INTERRUPT
50
TMOD Register
GATE: When set, timer/counter x is enabled, if INTx pin is high and TRx is set. When cleared, timer/counter x is enabled, if TRx bit set. C/T*: When set, counter operation (input from Tx input pin). When cleared, timer operation (input from internal clock).
8051 Microcontroller 51
TMOD Register
8051 Microcontroller
52
TCON Register
8051 Microcontroller
53
Timer 1
Mode 0
Mode 1 Mode 2
TIMER 0
OSC 12
C /T 0
C /T 1
TL0 TH0
TF0
T 0PIN
TR0
Gate
INT 0 PIN
INTERRUPT
TIMER 0 Mode 0
13 Bit Timer / Counter
OSC 12
C /T 0
C /T 1
T 0PIN
TL0 (5 Bit)
TH0 (8 Bit)
TF0
INTERRUPT
TR0
Gate
INT 0 PIN
TIMER 0 Mode 1
16 Bit Timer / Counter
OSC 12
C /T 0
C /T 1
T 0PIN
TL0 (8 Bit)
TH0 (8 Bit)
TF0
INTERRUPT
TR0
Gate
INT 0 PIN
TIMER 0 Mode 2
8 Bit Timer / Counter with AUTORELOAD
OSC 12
C /T 0
C /T 1
T 0PIN
TL0 (8 Bit)
TH0 (8 Bit)
Reload
TF0
INTERRUPT
TR0
Gate
INT 0 PIN
TH0 (8 Bit)
TIMER 0 Mode 3
Two - 8 Bit Timer / Counter
OSC 12
C /T 0
C /T 1
T 0PIN
TL0 (8 Bit)
TF0
INTERRUPT
TR0
Gate
INT 0 PIN
OSC
12
TH0 (8 Bit)
TF1
INTERRUPT
TR1
TIMER 1
OSC 12
C /T 0
C /T 1
TL1 TH1
TF1
T1PIN
TR1
Gate
INT 1 PIN
INTERRUPT
TIMER 1 Mode 0
13 Bit Timer / Counter
OSC 12
C /T 0
C /T 1
T1PIN
TL1 (5 Bit)
TH1 (8 Bit)
TF1
INTERRUPT
TR1
Gate
INT 1 PIN
TIMER 1 Mode 1
16 Bit Timer / Counter
OSC 12
C /T 0
C /T 1
T1PIN
TL1 (8 Bit)
TH1 (8 Bit)
TF1
INTERRUPT
TR1
Gate
INT 1 PIN
TIMER 1 Mode 2
8 Bit Timer / Counter with AUTORELOAD
OSC 12
C /T 0
C /T 1
T1PIN
TL1 (8 Bit)
TH1 (8 Bit)
Reload
TF1
INTERRUPT
TR1
Gate
INT 1 PIN
TH1 (8 Bit)
Programming Timers
Example: Indicate which mode and which timer are selected for each of the following. (a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H Solution: We convert the value from hex to binary.
(a) TMOD = 00000001, mode 1 of timer 0 is selected. (b) TMOD = 00100000, mode 2 of timer 1 is selected. (c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
8051 Microcontroller
64
Programming Timers
Find the timers clock frequency and its period for various 8051-based system, with the crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
Solution:
Serial: To transfer to a device located many meters away, the serial method is used. The data is sent one bit at a time.
8051 Microcontroller
67
There are special ICs made by many manufacturers for serial communications.
UART (universal asynchronous Receiver transmitter) USART (universal synchronous-asynchronous Receivertransmitter)
8051 Microcontroller 68
The start bit is always one bit, but the stop bit can be one or two bits The start bit is always a 0 (low) and the stop bit(s) is 1 (high)
8051 Microcontroller 69
8051 Microcontroller
70
As far as the conductor wire is concerned, the baud rate and bps are the same.
8051 Microcontroller 71
:Synchronous Serial Communication :8-Bit UART with Timer Data Rate :9-Bit UART with Set Data Rate :9-Bit UART with Timer Data Rate
72
8051 Microcontroller
73
SBUF Register
SBUF is an 8-bit register used solely for serial communication. For a byte data to be transferred via the TxD line, it must be placed in the SBUF register.
The moment a byte is written into SBUF, it is framed with the start and stop bits and transferred serially via the TxD line.
SBUF holds the byte of data when it is received by 8051 RxD line. When the bits are received serially via RxD, the 8051 deframes it by eliminating the stop and start bits, making a byte out of the data received, and then placing it in SBUF.
8051 Microcontroller 74
SBUF Register
Sample Program:
8051 Microcontroller
75
SCON Register
SM0 SM1 SM2 REN TB8 RB8 TI RI
Set to Enable Serial Data reception Enable Multiprocessor Communication Mode 9th Data Bit Sent in Mode 2,3
8051 Microcontroller
76
8051 Microcontroller
77
When 8051 is powered up, SMOD is zero We can set it to high by software and thereby double the baud rate.
8051 Microcontroller 83
8051 Microcontroller
84
8051 Interrupts
8051 Microcontroller 85
INTERRUPTS
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service
1. Interrupt 2. Polling
8051 Microcontroller
86
Interrupt Vs Polling
1. Interrupts
Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal. Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
2. Polling
The microcontroller continuously monitors the status of a given device. When the conditions met, it performs the service. After that, it moves on to monitor the next device until every one is serviced.
8051 Microcontroller 87
Interrupt Vs Polling
The polling method is not efficient, since it wastes much of the microcontrollers time by polling devices that do not need service. The advantage of interrupts is that the microcontroller can serve many devices (not all at the same time).
Each devices can get the attention of the microcontroller based on the assigned priority. For the polling method, it is not possible to assign priority since it checks all devices in a round-robin fashion.
The microcontroller can also ignore (mask) a device request for service in Interrupt.
8051 Microcontroller 88
5. It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine which is RETI (return from interrupt).
6. Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted.
8051 Microcontroller Suresh.P.Nair , HOD - ECE , RCET 89
4. Serial communication has a single interrupt that belongs to both receive and transfer.
8051 Microcontroller 90
We can configure the 8051 so that when Timer 0 Overflows or when a character is sent/received, the appropriate interrupt handler routines are called.
8051 Microcontroller 91
8051 Microcontroller
92
The interrupts must be enabled by software in order for the microcontroller to respond to them. There is a register called IE (interrupt enable) that is responsible for enabling (unmasking) and disabling (masking) the interrupts.
8051 Microcontroller 94
(a) MOV IE,#10010110B ;enable serial, timer 0, EX1 Another way to perform the same manipulation is:
SETB IE.7 ;EA=1, global enable SETB IE.4 ;enable serial interrupt SETB IE.1 ;enable Timer 0 interrupt SETB IE.2 ;enable EX1
Interrupt Priority
When the 8051 is powered up, the priorities are assigned according to the following. In reality, the priority scheme is nothing but an internal polling sequence in which the 8051 polls the interrupts in the sequence listed and responds accordingly.
8051 Microcontroller
97
Interrupt Priority
We can alter the sequence of interrupt priority by assigning a higher priority to any one of the interrupts by programming a register called IP (interrupt priority). To give a higher priority to any of the interrupts, we make the corresponding bit in the IP register high.
8051 Microcontroller
98
Reserved
PS
PT1 PX1
PT0
PX0
Serial Port Timer 1 Pin INT 1 Pin Priority bit=1 assigns high priority Priority bit=0 assigns low priority
8051 Microcontroller 99
The B Register
Commonly used as a temporary register, much like a 9th R register. Used by two op-codes MUL AB, div AB B register holds the second operand and will hold part of the result Upper 8 bits of the multiplication result Remainder in case of division. Can also be accessed through its SFR address of 0F0H. Bit addressable.
Can be accessed as 2 separate 8-bit registers if needed. DPTR is useful for string operations and Look-Up-Table (LUT) operations.
The SP Register
SP is the stack pointer. SP points to the last used location of the stack. Push operation will first increment SP and then copy data. Pop operation will first copy data and then decrement SP. In 8051, stack grows upwards (from low memory to high memory) and can be in the internal RAM only. On power-up, SP points to 07H. Register banks 2,3,4 (08H to 1FH) form the default stack area.
Stack can be relocated by setting SP to the upper memory area in 30H to 7FH. mov SP, #32H
Position PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0 Carry Flag
Function Auxiliary Carry Flag. For BCD Operations Flag 0. Available to the user for general purposes. Register bank select bits. Set by software to determine which register bank is being used. Overflow Flag Not used Parity Flag. Even Parity.
(RCAP2H) and (RCAP2L) exist only in the 8052 and they are copies of the TH2 and TL2 registers.
Control Registers
IP Interrupt Priority. IE Interrupt Enable. TMOD Timer Mode. TCON Timer Control. T2CON Timer 2 Control (8052) SCON Serial Port Control. PCON Power Control (80C51).
1. 2. 3. 4. 5.
8051 Microcontroller
112
8051 Microcontroller
113
The source and destination registers must match in size. MOV DPTR,A will give an error
The movement of data between Rn registers is not allowed MOV R4,R7 is invalid
8051 Microcontroller 114
8051 Microcontroller
115
SFR Addresses ( 1 of 2 )
117
SFR Addresses ( 2 of 2 )
118
Example
119
8051 Microcontroller
120
8051 Microcontroller
121
8051 Microcontroller
122
External Direct
External Memory is accessed. There are only two commands that use External Direct addressing mode: MOVX A, @DPTR MOVX @DPTR, A DPTR must first be loaded with the address of external memory.
8051 Microcontroller
123
8051 Microcontroller
124
MOV Instruction
MOV destination, source ; copy source to destination.
MOV A,#55H ;load value 55H into reg. A MOV R0,A ;copy contents of A into R0 ;(now A=R0=55H) MOV R1,A ;copy contents of A into R1 ;(now A=R0=R1=55H) MOV R2,A ;copy contents of A into R2 ;(now A=R0=R1=R2=55H) MOV R3,#95H ;load value 95H into R3 ;(now R3=95H) MOV A,R3 ;copy contents of R3 into A ;now A=R3=95H
Intel 8051 Programming 125
ADD Instruction
ADD A, source ;ADD the source operand to the accumulator
MOV A, #25H
MOV R2,#34H
ADD A,R2
;add R2 to accumulator
;(A = A + R2)
126
;HEX
; ASCII NUMBERS ;ASCII CHARACTERS
128
129
130
Operand 1
A
Operand 2
B
Result
A=low byte, B=high byte
131
Table 6-2:Unsigned Division Summary (DIV AB) Division byte / byte Numerator A Denominator B Quotient A Remainder B
8051 Microcontroller
132
8051 Microcontroller
133
134
135
136
Looping
137
138
139
140
141
142
Call Instructions
LCALL (long call): 3-byte instruction
2-byte address Target address within 64K-byte range
143