Академический Документы
Профессиональный Документы
Культура Документы
What is a microprocessor?
The microprocessor is the integration of a
number of useful functions. These functions are:
The ability to execute a stored set of instructions
to carry out user defined tasks.
General-purpose microprocessor.
Act as CPU for Computers.
No RAM, ROM, I/O on CPU chip itself
Example Intels x86, Motorolas 680x0
CPU
GeneralPurpose
Microprocessor
Data Bus
RAM
ROM
I/O
Port
Timer
Serial
COM
Port
Address Bus
What is Microcontroller?
Microcontroller is a device which integrates a
number of the components of a microprocessor
system onto a single microchip.
So a microcontroller combines onto the same
microchip :
The CPU core
Memory (both ROM and RAM)
Some parallel digital I/O
Microprocessor vs Micro-controllers
Microprocessors
High end of market where performance matters
High power dissipationhigh cost
Need peripheral devices to work
Mostly used in microcomputers.
Microcontollers
Targeted for low end of market where performance
does not matter
Low power dissipation low cost
Memory plus I/O devices, all integrated into one
chip
Mostly used in embedded systems
Embedded Systems
Embedded system means the processor is
embedded into that application.
An embedded product uses a microprocessor or
microcontroller to do one task only.
In an embedded system, there is only one
application software that is typically burned into
ROM.
Example printer, keyboard, video game player
23
Introduction to Microcontroller
The microcontroller is a very common component in
modern electronic systems. Its use is so widespread that it
is almost impossible to work in electronics without coming
across it. Microcontroller are used in a wide number of
electronic systems such as:
Engine management systems in automobiles.
Keyboard of a PC.
Electronic measurement instruments (such as digital
multimeters, frequency synthesisers, and oscilloscopes)
Printers,Mobile phones.
Televisions, radios, CD players, tape recording
equipment.
Features of 8051
ROM
- 4K bytes
RAM
- 128 bytes
Timer
- 2
I/O pins
- 32
Serial port
- 1
Interrupt sources - 6
8051 is based on CISC architecture.
It is based on Harvard architecture. So, it has
separate program and data memory.
8051
8052
4K
8K
128
256
2
3
32
32
1
1
6
8
8051 from Atmel
8031
0K
128
2
32
1
6
Architecture
Based on the instruction set used:
CISC
RISC
CISC
Complex Instruction Set Controller.
It invariably employ a microcode for decoding the
instructions and generate the necessary control
signals for performing the intended operation.
When an instruction is fetched from memory, it is
compared with the existing bit pattern, when match
is found it generates the associated control signals
to ALU and registers.
RISC
Reduced Instruction Set Controller.
These are fast at numerical computations required
in science, graphics and engineering applications.
The total number of transistors used for the design
of RISC is half the number used in CISC.
This reduced number of transistors reduces the
power consumption.
Computer Architecture
Depending on how external memory is connected to
the processors:
Von-Neumann machine.
Harvard machine.
Von-Neumann machine
It has 3 hardware subsystems; a CPU, a main
memory system and an I/O system.
It uses stored program concept i.e., the program
and data are stored in the same memory unit.
It has a single path between the main memory
system and the control unit of the CPU. Because the
common path is used to access both the program as
well as the data, there is possibility of congestion on
the bus. This situation is called Von Neumann
Harvard machine
It uses separate memories for storing the program
and data.
To connect these separate memories, it uses a
separate set of address, data and control lines.
As separate memories and buses are used,
simultaneous access to the memory is possible
without any congestion over the bus, which in turn
increases the performances.
It increases the cost of the system as separate
memories & separate buses are used.
On-chip
ROM for
program
code
Timer/Counter
On-chip
RAM
Timer 1
Timer 0
CPU
OSC
Bus
Control
4 I/O Ports
P0 P1 P2 P3
Address/Data
Serial
Port
TxD RxD
Counter
Inputs
Registers in 8051
In CPU, registers are used to store information
temporarily. That information could be a byte of data
to be processed, or an address pointing to the data
to be fetched. 8051 have 8-bit registers
D7 D6 D5 D4 D3 D2 D1 D0
MSB
LSB
DPTR
PC
Flag Bit
8051 has a flag register to indicate arithmetic
conditions such as carry bit. The flag register of
8051 is called Program Status Word (PSW) register.
PSW.7
PSW.6
PSW.5
PSW.4
PSW.3
PSW.2
PSW.1
PSW.0
RS1
0
0
1
1
Carry Flag
Auxiliary Carry Flag
Available to the user for general purpose
Register Bank selector bit 1
Register bank selector bit 0
Overflow flag
User-definable bit
Parity flag.(odd/even no of 1 bit in the accumulator)
RS0
0
1
0
1
Register bank
0
1
0
1
Address
00H-07H
08H-0FH
10H-17H
18H-1FH
Examples:
1)38h+2Fh=67
0001 1100
+0010 1111
0110 0111
CY=0no carry beyond D7.
AC=1carry from D3 to D4.
P=1 since accumulator has an odd number of 1s.
Bit-Addressable RAM
Register Bank 3
18
17
10
0F
08
07
00
Register Bank 2
Register Bank 1(Stack)
Register Bank 0
and
Register bank
0
1
2
3
Stack in 8051
It is a section of RAM.
Stack is used to store information temporarily.
The information could be data or address.
CPU needs this storage area since there are only a
limited number of registers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
8051
(8031)
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
Vcc
P0.0(AD0
P
) 0.1(AD1)
P0.2(AD2
P
) 0.3(AD3)
P0.4(AD4)
P0.5(AD5)
P0.6(AD6)
P0.7(AD7)
EA/VPP
ALE/PROG
PSEN
P2.7(A15)
P2.6(A14
)P2.5(A13
)P2.4(A12
)P2.3(A11)
P2.2(A10)
P2.1(A9)
P2.0(A8)
Reset Value
0000
0000
0000
0000
0007
0000
Addressing Modes
The CPU can access data in various ways. The
data could be in a register or in memory or be
provided as an immediate value. These various ways
of accessing data are called addressing modes.
Total 5 addressing modes:
1)Immediate
2)Register
3)Direct
4)Register indirect
5)Indexed
Checksum byte
To ensure the data integrity of ROM contents every
system must perform the checksum byte calculation.
The process of checksum will detect any corruption
of the contents of ROM.
One of the causes of ROM corruption is current
surge, when the system is turned on or during
operation.
Unsigned int
16bit datatype.
value in a range of 0-65535.
used to define 16-bit memory address.
8051 is 8-bit microcontroller, so int datatype take
2bytes of RAM.
Signed int
16 bit datatype.
Uses MSB (D15 of D15-D0) to represent sign(-ve or
+ve).
15 bit for magnitude.
Value range from -32768 to 32767.
Time Delay
There are two ways to create a time delay 8051:
1.Using a simple loop
2.Using the 8051 timers
8051 Timers
Both Timer 0 and Timer 1 are 16 bits wide.
Since the 8051 has an 8-bit architecture, each 16-bit
timer is accessed as 2 separate registers of low byte
and high byte.
Timer 0 Register
The 16-bit registers of Timer 0 are accessed as
low byte and high byte. The low byte is called TL0
(Timer 0 low byte) and the high byte register is called
TH0 (Timer 0 high byte). These registers can be
accessed like any other register, such as A,B,R0,R1,
etc.
TH0
TL0
Timer 1 register
Timer 1 is also 16 bit register, and its 16-bit
register is split into two bytes, referred to as
TL1(Timer 1 low byte) and TH1 (Timer 1 high byte).
These registers are accessible in the same way as
Timer 0.
TH1
D15 D14 D13 D12 D11 D10 D9
TH0
D8
D7
D6
D5
D4
D3
D2
D1
D0
C/T
Timer 1
M1
M0
GATE
C/T
M1
Timer 0
LSB
M0
GATE
Every timer has a means of starting
and stopping. Some do this by software, some by
hardware and some by both.
If GATE=0, if we use software for this purpose.
If GATE=1, if we use external hardware for this
purpose.
C/T
Used to select Timer/Counter operation.
Set (1)
Counter.
Cleared(0) Timer.
M1 & M2
These are modes bits used in
order to select the different operating modes of the
timer.
M1
0
0
1
1
TCON register
8-bit addressable register.
TR0 and TR1 flags used to turn on or turn off the
timer are part of TCON (Timer Control) register.
LSB
MSB
TF1
TR1
TF0
TR0
4
IE1
3
IT1
2
IE0
IT0
BIT SYMBOL
FUNCTION
TCON.7 TF1
Timer 1 overflow flag
TCON.6 TR1
Timer 1 run control bit
TCON.5 TF0
Timer 0 overflow flag
TCON.4 TR0
Timer 0 run control bit
Mode 1
It is a 16-bit timer; it allows values of 0000 to
FFFFH to be loaded into timers registers TL and TH.
After TL and TH are loaded with 16-bit initial value,
the timer must be started by setting TR0 for timer 0
and TR1 for timer 1.
After the timer is started, it starts to count up. It
counts up until it reaches its FFFFH. When it rolls
over from FFFFH to 0000, it sets high a flag bit called
TF (timer flag).
Example:
Assume that XTAL=11.0592 MHz.
We want 5ms delay
Solution:
(5ms / 1.085 micro sec) = 4608 clocks
(65536 4608) = 60928 = EE00H
Therefore, we have
TH=EE
TL=00
XTAL
Oscillator
/12
TH
TR
C/T=0
TL
TF goes high
When FFFF=0
TF
Overflow
flag
Mode 0
Its exactly similar to mode1 except that it is a 13 bit
timer instead of 16bit.
Hold values between 0000-1FFFH in TH and TL.
Therefore, when the timer reaches its maximum of
1FFFH it rolls over to 0000 and TF is raised.
Mode 2
8-bit timer allows values from 00-FFH to be loaded
to TH.
After TH is loaded with 8-bit value, the 8051 will
give a copy of it to TL.
Then the timer is started. After timer is started, it
starts count by incrementing the TL register.
It counts up until it reaches FF H. When it rolls from
FFH to 00H, TF is set; TF0 for timer 0 and TF1 for
Timer 1.
TL is reloaded automatically with the original value
kept by TH register.
To repeat the process, simply clear TF and let it go
without reloading the original value by programmer.
Here TH is unchanged. It holds the copy of the
original value.
XTAL
Oscillator
/12
TL
Overflow
flag
Reload
TR
C/T=0
TF
TH
TF goes high
When FF=0
Mode 3
Timer mode 3 is a split-timer mode.
When Timer 0 is placed in mode 3, it essentially
becomes two separate 8-bit timers. That is to say,
Timer 0 is TL0 and Timer 1 is TH0.
Both timers count from 0 to 255 and overflow back
to 0.
Counter
If C/T of TMOD is 1, it is counter, it takes its pulses
from outside the 8051.
The counter counts up as pulses are fed from T0
and T1.
In T0, P3.4 provides clock pulse and counter
counts up for each clock pulse coming from that pin.
For T1, each pulse coming in P3.5 makes the
counter count up.
Serial Transfer
Sender
Receiver
Sender
Receiver
D7
Simplex
Half Duplex
Full Duplex
Transmitter
Receiver
Transmitter
Receiver
Receiver
Transmitter
Transmitter
Receiver
Receiver
Transmitter
stop
bit
0
D7
1
D0
start
bit
RS232 Standards
To allow compatibility among data communication
equipment made by various manufacturers, an
interfacing standard called RS232 was set by the
Electronics Industries Association (EIA) in 1960.
Many types like RS232A,RS232B,RS232C
1=(-3 to -25V)
0=(+3 to +25V)
TH1(Decimal)
TH1(Hex)
9600
-3
FD
4800
-6
FA
2400
-12
F4
1200
-24
E8
SBUF register
SBUF is an 8-bit register used for serial
communication in the 8051.
To transfer a byte of data via the TxD line, it must
be placed in the SBUF register.
Similarly, SBUF hold a byte of data when it is
received by the 8051s RxD line.
SBUF can be assed by any other register in 8051.
The moment a byte is written into SBUF, it is framed
with a start & stop bits & transferred serially via TxD
pin.
Similarly, When the bits are received serially via
RxD, the 8051 deframes it by eliminating the start
and stop bits, making a byte of data received, and
placing it in the SBUF.
SMI
SM0
SM1
SM2
SCON.7
SCON.6
SCON.5
REN
SCON.4
TB8
RB8
TI
RI
SCON.3
SCON.2
SCON.1
SCON.0
SM2
REN
TB8
RB8
TI
RI
0
1
Serial Mode 0
Serial Mode 1,
8-bit data, 1 stop bit, 1 Start bit
Serial Mode 2
Serial Mode 3
D7
SMOD
D0
---
---
---
GF1
GF0
PD
IDL
SMOD = 0
TH1(Decimal)
TH1(Hex)
19,200
9,600
-3
FD
9,600
4,800
-6
FA
4,800
2,400
-12
F4
2,400
1,200
-24
E8
Interrupts Programming
A single microcontroller can serve several
devices. There are 2 ways to do that.i.e Interrupt and
Polling.
Interrupt Method
In this method, whenever any devices needs its
service, the device notifies the MC by sending it an
interrupt signal.
Upon receiving an interrupt signal, the MC
interrupts whatever it is doing and serves the
device. The program associated with the interrupt is
called interrupt service routine (ISR) or interrupt
handler.
Polling Method
In this method MC continuously monitors the
status of a given devices; when the status condition
is met it perform the service.
It is not a efficient use of MC
In interrupt method according to Priority the MC
will service.
In polling method cannot assign priority since it
checks all devices in a round-robin fashion.
D0
--IE.7
IE.6
IE.5
IE.4
IE.3
IE.2
IE.1
IE.0
ET2
ES
ET1
EX1
ET0
EX0
Level-Triggered Interrupt
In this mode, INT0 & INT1 pins are normally high
(just like all I /O port pins) & if a low level signal is
applied to them, it triggers the interrupt. This is
called as level triggered interrupt.
If the low level interrupt signal is not removed
before the ISR is finished it is interpreted as another
interrupt & the 8051 jumps to the vector table to
execute the ISR again.
Therefore to ensure the activation of the hardware
interrupt at the INTn pin, make sure that the duration
of the low level signals is around 4 machine cycles,
but not more.(INT0 have highest priority than INT1)
Edge-Triggered Interrupts
If we want to make INT0 & INT1 as edge triggered
interrupt we must program the bits of TCON register.
LSB
MSB
TF1
TR1
TF1
BIT SYMBOL
TCON.7 TF1
TCON.6 TR1
TCON.5 TF0
TCON.4 TR0
TCON.3
IE1
TCON.2
IT1
TCON.1
IE0
TCON.0
IT0
TR0
4
IE1
3
IT1
2
IE0
IT0
FUNCTION
Timer 1 overflow flag
Timer 1 run control bit
Timer 0 overflow flag
Timer 0 run control bit
External interrupt 1 edge flag
Interrupt 1 type control bit
External interrupt 0 edge flag
Interrupt 0 type control bit
D0
---
PT2
PS
PT1
PX1
PT0
PX0
Priority bit =1 assigns high priority. Priority bit=0 assigns low priority
----PT2
PS
PT1
PX1
PT0
PX0
IP.7
IP.6
IP.5
IP.4
IP.3
IP.2
IP.1
IP.0
Reserved
Reserved
Timer 2 priority bit
Serial port interrupt priority bit
Timer 1 interrupt priority bit
External interrupt 1 priority bit
Timer 0 interrupt priority bit
External interrupt 0 priority bit
Interrupt Programming in C
8051/8052 Interrupt Numbers in C
Interrupt
Name
External Interrupt 0
INT0
Timer Interrupt 0
TF0
External Interrupt 1
INT1
Timer Interrupt 1
TF1
Serial Communication
RI + TI
TF2
LCD Interfacing
In recent years the LCD is finding widespread
use replacing LEDs (seven-segment LEDs or other
multisegment LEDs). This is due to the following
reasons.
The declining prices of LCDs.
The ability to display numbers, characters, and
graphics. This is in contrast to LEDs, Which are
limited to numbers and a few characters.
Incorporation of a refreshing controller into the
LCD, thereby relieving the CPU of the task of
refreshing the LCD.
Ease of programming for characters and graphics.
Keyboard Interfacing
VCC
4.7K
3
4.7K
D0
D1
D2
D3
Port 1
(Out)
Row
D3
D2
D1
D0
Port 2
(In)
Column
Theory of Operation
I2C has a master/slave protocol. The master
initiates the communication. The sequences of
events are:
The Master device issues a start condition. This
condition informs all the slave devices to listen on
the serial data line for instructions.
The Master device sends the address of the target
slave device and a read/write flag.
The Slave device with the matching address
responds with an acknowledgement signal.
Communication proceeds between the Master and
the Slave on the data bus. Both the master and
slave can receive or transmit data depending on
whether the communication is a read or write.
Features
I2C has many features other important features worth mentioning. It
supports multiple data speeds:
standard (100 kbps),
fast (400 kbps) and
high speed (3.4 Mbps) communications.
Built in collision detection,
10-bit Addressing, Mutli-master support, Data broadcast (general call).
(EEPROM) AT24C02A
ADC
ADC are among the most widely used devices for
data acquisition. Digital computers use binary
(discrete) values, but in the physical world
everything is analog (continuous). Temperature,
Pressure (wind or Liquid), humidity, & velocity are
few examples.
A physical quantity is converted into electrical
(voltage, current) signals using a device called a
transducer (Sensors).
Sensor converts the temperature, pressure etc..
Into its corresponding voltage (or current). like this
way ADC works.
Number of Steps
256
5 / 256 = 19.53
10
1024
5 / 1024 = 4.88
12
4096
5 / 4096 = 1.2
16
65563
5 / 65536 = 0.076
ADC0804 Chip
+5V
20
10K
POT
10K
150 pF
6
7
8
VCC
Vin (+)
Vin (-)
A GND
Vref / 2
19
CLK R
CLK in
1
2
10
CS
RD
D GND
D0
D1
D2
D3
D4
D5
D6
D7
18
17
16
15
14
LEDs
13
12
11
WR 3
5
INTR
Normally
Open
START
Vin (V)
0 to 5
5 / 256 = 19.53
2.0
0 to 4
4 / 255 = 15.62
1.5
0 to 3
3 / 256 = 11.71
1.28
0 to 2.56
2.56 / 256 = 10
Not connected
Data out
D0 D7
INTR
RD
Start conversion
End conversion