MICROCONTROLLER AND ITS APPLICATION WORKSHOP January 26, 2007 Usman Institute of Technology, Karachi. Noman Ahmed Lecturer SSUET E-mail:
[email protected]
Introduction
It is hard to imagine the present world of electronic devices without the microprocessor. Cash register, scales, ovens, washing machine, alarm clock, thermostats, and ignition system, etc. 1971 (Microprocessors) Intel 8080, Motorola 6800, RCA 1801, MOS Technology and Zilog Z80. 1976 (Microcontrollers) Intel 8748 (MCS-48 family) 17,000 transistors • CPU, 1KB EPROM, 64B RAM, 27 I/O pins, and an 8-bit timer. • Used in washing machines and traffic light controllers. 1980 (Microcontrollers) Intel 8051 (MCS-51 family) 60,000 transistors • CPU, 4KB ROM, 128B RAM, 32 I/O Pins, a serial port, two 16-bit timers.
A Computer System
Computer is defined by two key traits: The ability to be programmed to operate on data without human intervention. The ability to store and retrieve data. More generally, a computer system includes Peripheral devices for communicating with the human. (hardware) Program that process data. (software) A Computer system contains Central process unit (CPU) connected to random access memory (RAM) and read-only memory (ROM) via the address bus, data bus, and control bus. Interface circuits connect the system buses to peripheral devices. (see next slide)
Block Diagram of Computer System
What’s a Microcontroller?
A Microcontroller is a computer-on-a-chip, or, if you prefer, a single-chip computer. Micro suggests that the device is small, and controller tells you that the device might be used to control objects, processes, or events.
Another term to describe a microcontroller is embedded controller, because the microcontroller and its support circuits are often built into, or embedded in, the devices they control.
Where it Required
Any device that measures, stores, controls, calculates, or displays information is a candidate for putting a microcontroller inside.
Application Automobiles Security System Answering Machine Cellular Phone Microwave Keyless entry Fax machine Toys Camera
Microprocessors vs. Microcontroller Hardware Achitecture Whereas a microprocessor is a single-chip CPU, a microcontroller contains a CPU and much of the remaining circuitry of a complete microcomputer system in a single IC. Microcontrollers (as control oriented devices) are called upon to response to external stimuli in real time. They must perform fast context switching, suspending, one process while executing another in response to an ‘event’ (opening the oven’s door). Applications Microcontrollers are suitable to ‘control’ of I/O devices in designs requiring a minimum component count, whereas microprocessors are suitable to ‘processing’ information in computer systems.
Microprocessors vs. Microcontroller
(cont.) Instruction set features
Microprocessor instruction sets are ‘processing intensive’ implying they have powerful addressing modes with instructions catering to operations on large volumes of data. Microcontrollers, on the other hand, have instruction sets catering to the control of inputs and outputs.
Microcontrollers have built-in circuitry and instructions for input/output operations, event timing, and enabling and setting priority levels for interrupts caused by external stimuli. Microprocessors often require additional circuitry (serial interface Ics, interrupt controllers, timers, etc. ) to perform similar operations.
Hardware Architecture
The MCS-51 is a family of microcontroller ICs developed, manufactured, and marketed by Intel. Other IC manufacturers are Siemens, Advanced Micro Devices (AMD), Fujitsu, Philips are licensed ‘second source’ suppliers of devices in the MCS-51 family. The generic MCS-51 IC is the 8051, the first device in the family offered commercially. Its features are summarized 4KB ROM (64KB external code memory space) 128B RAM (64KB external data memory space) 8-bit I/O ports (4EA) 16-bit timers (2EA) Serial interface Boolean processor 210 bit-addressable locations
Comparison of MCS-51 ICs Part No. 8051
ROM/EPROM 4K ROM
8031
RAM
Timers
128B
2
128B
2
8751
4K EPROM
128B
2
8052
8K ROM
256B
3
256B
3
256B
3
8032 8752
8K EPROM
8051 Block Diagram INT1 INT2
Note: bold-faced pin assignments for P1 and P3.
Timer 1 Timer 0 Serial port Interrupt Control
Other registers
128B RAM
4KB ROM
Timer 1
T1
Timer 0
T0
CPU
Bus Control
I/O ports
Serial port
Oscillator
EA
RST
PSEN
ALE
P0 P1 P2 P3 Address/data
TXD
RXD
8051 Pinouts 30pF XTL1 (19)
VCC (40)
XTL2 (18)
30pF (29) (30) (31) (9)
Port 3
RD(17) WR(16) T1(15) T0(14) INT1(13) INT0(12) TXD(11) RXD(10)
PSEN ALE EA RST
P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0
8051
VSS(20)
P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0 P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
AD7(32) AD6(33) AD5(34) AD4(35) AD3(36) AD2(37) AD1(38) AD0(39) (8) (7) (6) (5) (4) (3) (2) (1)
P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
A15(28) A14(27) A13(26) A12(25) A11(24) A10(23) A9(22) A8(21)
Port 0
Port 1
Port 2
I/O Ports
32 of the 8051’s 40 pins function as I/O port lines. However, 24 of these 32 lines are dual-purpose.
Dual purpose: can operate as I/O, control line, or part of address/data bus. The 8-line in a port can be treated as a unit in interfacing to parallel devices such as printers, A/D converters, and so on. Or, each line can operate independently in interfacing to single-bit devices such as switches, LEDs, transistors, motors, and loudspeakers.
I/O Ports
Port 0 Is a dual-purpose port on pins 32-39 of the 8051 IC. In minimum-component designs, • It is used as a general purpose I/O port. For larger designs with external memory, • It becomes a multiplexed address and data bus. Port 1 Is a dedicated I/O port on pins 1-8. The pins are available for interfacing to external devices as required. Port 2 Is a dual-purpose port on pins 21-28 of the 8051 IC. As a general purpose I/O port Or as the high-byte of the address bus for designs with external ROM or more than 256B of RAM.
I/O Ports (cont.)
Port 3 Is a dual-purpose port on pins 10-17 of the 8051 IC. As a general purpose I/O port These pins are multifunctional, with each having an alternate purpose related to special features of the 8051.
BIT NAME
BIT
functions
P3.0 RXD
B0H
Receive data for serial port
P3.1 TXD
B1H
Transmit data for serial port
P3.2 INT0
B2H
External interrupt 0
P3.3 INT1
B3H
External interrupt 1
P3.4 T0
B4H
Timer/counter 0 external input
P3.5 T1
B5H
Timer/counter 1 external input
P3.6 WR
B6H
External data memory write strobe
P3.7 RD
B7H
External data memory read strobe
Control Signals
PSEN (Program Store Enable)
Is an output signal on pin 29.
It is a control signal that enables external program (code) memory (ROM).
It usually connects to an EPROM’s output enable (OE) pin to permit reading of program bytes. The PSEN signal pulses low (active stage) during the fetch stage of an instruction, which is stored in external program memory. The binary codes of a program (opcode) are read from EPROM, travel across the data bus, and are latched into the 8051’s instruction register (IR) for decoding.
Control Signals
ALE (Address Latch Enable)
Is an output signal on pin 30.
It is used for de-multiplexing the address and data bus.
When port 0 is used in its alternate mode – as the data bus and low-byte of the address bus – ALE is the signal that latches the address into an external register during the first-half of a memory cycle. This done, the port 0 lines are then available for data input or output during the second-half of the memory cycle. The ALE signal pulses at 1/6th the on-chip oscillator frequency
Control Signals
EA (External Access) Is an input signal on pin 31. Is generally tied high (5V) or low (ground). If high • The 8051 executes programs from internal ROM when executing in the lower 4K/8K of memory. If low • Programs execute from external memory only (and PSEN pulses low) RST (Reset) Is an input signal on pin 9. When this signal is brought high for at least 2 machine cycles, the 8051 internal registers are loaded with appropriate values for an orderly system start-up. For normal operation, RST is low.
Memory Organization
Most microprocessors implement a shared memory apace for data and programs. Both the data and programs reside in the system RAM. Microcontroller, on the other hand, the control program must reside in ROM.
The internal memories consist of ROM and RAM. The RAM contains a rich arrangement of general-purpose storage, bit addressable storage, register banks, and special function registers.
RAM 7F
General-purpose RAM (80 bytes)
FF
30 2F
Special Function Registers (SFR)
Bit-addressable locations (16 bytes)
20 1F
Bank registers (32 bytes)
00
80
General-Purpose RAM 7F
General-purpose RAM (80 bytes)
30 2F
Ex: To read the contents of internal RAM address 5FH into the accumulator. Solution1:(direct address mode)
MOV A, 5FH Solution2:(immediate addressing & indirect address mode) 20 1F
Bank registers (32 bytes)
00
MOV R0, #5FH MOV A, @R0
210 (128+82) Bit-Addressable RAM 7F
FF
30 2F
Special Function Registers (SFR)
Bit-addressable locations (16 bytes)
(82 bits)
(128 bits)
20 1F
00
80
The idea of individually accessing bits through software is a powerful feature of most microcontroller.
128 General-Purpose Bit-Addressable Locations 2F
7F
7E
7D
7C
7B
7A
79
78
2E
77
76
75
74
73
72
71
70
2D
6F
6E
6D
6C
6B
6A
69
68
2C
67
66
65
64
63
62
61
60
2B
5F
5E
5D
5C
5B
5A
59
58
2A
57
56
55
54
53
52
51
50
29
4F
4E
4D
4C
4B
4A
49
48
28
47
46
45
44
43
42
41
40
27
3F
3E
3D
3C
3B
3A
39
38
26
37
36
35
34
33
32
31
30
25
2F
2E
2D
2C
2B
2A
29
28
24
27
26
25
24
23
22
21
20
23
1F
1E
1D
1C
1B
1A
19
18
22
17
16
15
14
13
12
11
10
21
0F
0E
0D
0C
0B
0A
09
08
20
07
06
05
04
03
02
01
00
Register Banks 1F 18
R7 ~R0
17 10
R7 ~R0
Bank 2 (8 bytes)
0F 08
R7 ~R0
Bank 1 (8 bytes)
07 00
R7 ~R0
Bank 3 (8 bytes)
Bank 0 (8 bytes)
Ex: Read the contents of address 05H into the accumulator. Solution: (Register address mode) MOV A, R5 (only 1-byte) Solution: (direct address mode) MOV A, 05H (2-byte instruction)
21 Special Function Registers (SFR) F0 E0 D0 B8 B0 A8 A0 99 98 90 8D 8C 8B 8A 89 88 87 83 82 81 80
F7 E7 D7 B7 AF A7
F6 E6 D6 B6 A6
F5 E5 D5 B5 A5
9F 97
9E 96
9D 95
8F
8E
8D
87
86
85
F4 F3 E4 E3 D4 D3 BC BB B4 B3 AC AB A4 A3 Not bit addressable 9C 9B 94 93 Not bit addressable Not bit addressable Not bit addressable Not bit addressable Not bit addressable 8C 8B Not bit addressable Not bit addressable Not bit addressable Not bit addressable 84 83
F2 E2 D2 BA B2 AA A2
F1 E1 B9 B1 A9 A1
F0 E0 D0 B8 B0 A8 A0
9A 92
99 91
98 90
8A
89
88
82
81
80
B ACC PSW IP P3 IE P2 SBUF SCON P1 TH1 TH0 TL1 TL0 TMOD TCON PCON DPH DPL SP P0
Addressing Modes
The 8051 is capable of performing direct and indirect memory accesses on its various memory spaces.
These are the typical methods through which processor systems access memory.
Addressing modes are in integral part of each computer’s instruction set. There are 8 modes available:
Register, Direct, Indirect, Immediate, Relative, Absolute, Long, and Indexed.
Instruction Set
The MCS-51 instruction set is optimized for 8-bit control applications. It provides a variety of fast, compact address modes for accessing the internal RAM to facilitate operations on small data structures. The 8051 instructions have 8-bit opcodes 28 = 256 instructions. 255 are implemented and 1 is undefined. There are 139 one-byte instructions, 92 two-byte instructions, and 24 three-byte instructions. (139+92+24=255)
Instruction Set (cont.)
Types of Instruction
Arithmetic Instructions
Logical Instructions
Data Transfer Instructions
Boolean Instructions
Program Branching Instructions
INTERFACING OF DIFFERENT COMPONENTS WITH MICROCONTROLLER
Input Devices On-Off switch Push button Limit Switch IR sensor Metal Detector DTMP Decoder KEY Pad
Output Devices Leds Relay Solenoid Seven segment LCD display Dot Matrix display Stepper Motor
Interfacing of LCD 8051
LCD P1.0 ~ P1.7
D0 ~ D7 Rs
P2.0 P2.1 P2.2
VCC 10K VEE R/W
E VSS
Temperature Sensor Physical data (temperature, light, flow, speed, and etc.)
Transducers (Sensors)
o
Thermistor Temperature
Output Electrical Signals (current, voltage, resistance, capacitance, and etc.)
Resistance
C
K Ohm
0
29.49
25
10
50
3.893
75
1.7
100
0.817
Interfacing of ADC808/809
ADC808/809 Chip with 8 analog channel. This means this kind of chip allows to monitor 8 different transducers. ADC804 has only ONE analog input: Vin(+). ADC808
8 channels (IN0~IN7)
D0 ~ D7 GND
channel
CBA
CLK
EOC
IN0
000
VCC Vref(+)
OE
IN1
001
IN2
010
IN3
011
IN4
100
IN5
101
IN6
110
IN7
111
Vref(-) SC ALE
C BA
Interfacing of Stepper Motor
Stepper motor translates electrical pulses into mechanical movement (for position control). Applications: Disk Driver, Dot matrix printer, robotics.
A N S
C N
4 Step Sequences S N
S
B
D
Step A s
B
C
D
1 2 3 4
0 1 1 0
0 0 1 1
1 0 0 1
1 1 0 0
Interfacing of Keyboard Keyboard and LCDs are the most widely used input/output devices of the 8051, and a basic understanding of them is essential. See Figure
Project Steps Putting together a microcontroller project involves several steps: 1. Define the task 2. Design and build the circuits 3. Write the control program 4. Test and debug
Sometimes the steps won’t follow exactly in this order.
Projects Digital Clock Keyless entry Mobile Automation Robotic Arm Elevator Control System Industrial Network DC/AC Drive
Projects PABX Autonomous Robot Infant incubator Attendace System Car Tracking Score Board Prepaid Energy meter CNC PCB drill machine