Micro Controller Archtecture Programming
Micro Controller Archtecture Programming
(From, the book The 8051 Microcontroller and Embedded systems- Mazidi )
Overview
Introduction
Block Diagram and Pin
Description of the 8051
Registers
Memory mapping in 8051
Stack in the 8051
I/O Port Programming
Timers
Interrupts &
Applications
Why do we need to learn
Microcontrollers ?
I think, I am not wrong if I say that , in
todays market it is impossible to find any
electronic gadget designed without a
Microcontroller.
Ex: From Consumer Electronics to
Communication Electronics
Not believable ??? See the next slide
Toys , video games, cars, dolls ..
Personal information products: Cell phone,
pager, watch, pocket recorder, calculator
Laptop components: mouse, keyboard, modem,
fax card, sound card, battery charger
Home appliances: door lock, alarm clock,
thermostat, air conditioner, TV remote, VCR,
small refrigerator, exercise equipment,
washer/dryer, microwave oven
Industrial equipment: Temperature/pressure
controllers, Counters, timers, RPM Controllers
So, A good designer should always
know what type of controller he/she
is using ,their architecture,
Programming details ,advantages ,
disadvantages , ways to reduce
production costs and product
reliability etc….
O.K !!!
Then What is a Microcontroller ?
A single chip computer or A CPU with all the
peripherals RAM, ROM, I/O ports... On the
same chip
Example : Motorola’s 6811, Intel’s 8051,
Zilog’s Z8 and PIC 16X
A (8-bit Accumulator)
B (8-bit register for Mul &Div)
PSW (8-bit Program Status Word)
SP (8-bit Stack Pointer)
PC (16-bit Program Counter)
DPTR (16-bit Data Pointer)
Special Function Registers
DATA registers
CONTROL registers
•Timers
•Serial ports
•Interrupt system
0FFFH
DS5000-32
1FFFH
8051
8752 7FFFH
30H
2FH
Bit-Addressable RAM
20H
1FH Register Bank 3
18H
17H
Register Bank 2
10H
0FH Register Bank 1 )Stack(
08H
07H
Register Bank 0
00H
PORTS OF 8051
8051 has 4 Ports. Port 0, Port1, Port2 , Port3
Port 0 is a dual purpose port, it is located from pin
32 to pin 39 (8 pins). To use this port as both
input/output ports each pin must be connected
externally to a 10 k ohm pull-up resistor.This is
because Port 0 is an open drain.
Simple ex: MOV A, #22
BACK MOV P0 ,A
ACALL DELAY
CPL A
SJMP BACK
Port 0 with Pull-Up Resistors
Ports….
Port 1 is a dedicated I/O port from pin 1 to pin
8.Upon reset it is configured as outport. It is
generally used for interfacing to external device
thus if you need to connect to switches or LEDs,
you could make use of these 8 pins,but it
doesn’t need any pull-up resistors as it is having
internally
Like port 0, port 2 is a dual-purpose port.(Pins 21
through 28) It can be used for general I/O or as the high
byte of the address bus for designs with external code
memory.Like P1 ,Port2 also doesn’t require any pull-up
resistors
Ports contd…
P2 P1 to
Set LCD
Timer 0 TH0
TL0
Counter
Count the number of events
Show the number of events on registers
External input from T0 input pin (P3.4) for Counter
0
External input from T1 input pin (P3.5) for Counter
1
8051
External input from Tx input pin.
We use Tx to denote T0 or T1. TH0
P1 to
TL0
LCD
P3.4
a switch T0
Registers Used in Timer/Counter
8051 has two 16-bit Timer registers ,Timer 0 &
Timer 1.
As 8051 has 8-bit architecture , each Timer
register is treated as two 8-bit registers namely
TH0, TL0, TH1, TL1.
One 8-bit mode register -TMOD.
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
SETB TR0
CLR TR0
GATE=1
External control
The hardware way of starting and stopping the timer by
software and an external source.
Timer/counter is enabled only while the INT pin is high
and the TR control pin is set (TR).
TMod contd….
C/T : Timer or counter selected cleared for timer operation
(input from internal system clock). Set for counter
operation (input from Tx input pin).
M1,M0 : Used for mode selection.Because the Timers of 8051
can be set in 4-different modes.
M1 M0 Mode Operation
0 0 0 13-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1)
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
TF Monitor TF until TF=1
Mode 1 contd…
6. When TH0-TL0 rolls over from FFFFH to 0000, the
8051 set TF0=1.
TH0-TL0= FFFE H, FFFF H, 0000 H (Now
TF0=1)
7. Keep monitoring the timer flag (TF) to see if it is
raised.
AGAIN: JNB TF0, AGAIN
8. Clear TR0 to stop the process.
CLR TR0
9. Clear the TF flag for the next round.
CLR TF0
TCON Register
DELAY:
SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
8051- SERIAL COMMUNICATION
Basics of serial communication
Types of Serial communications
RxD and TxD pins in the 8051
The 8051 has two pins for transferring and
receiving data by serial communication. These
two pins are part of the Port3(P3.0 &P3.1)
These pins are TTL compatible and hence they
require a line driver to make them RS232
compatible
Max232 chip is one such line driver in use.
Serial communication is controlled by an 8-bit
register called SCON register,it is a bit
addressable register.
Interfacing to PC
SCON (Serial control) register
SM0 , SM1
These two bits of SCON register determine the framing
of data by specifying the number of bits per character
and start bit and stop bits. There are 4 serial modes.
SM0 SM1
0 0 Serial Mode 0
0 1 Serial Mode 1, 8 bit data,
1 stop bit, 1 start bit
1 0 Serial Mode 2
1 1 Serial Mode 3
REN, TI, RI
IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timer 0 interrupt priority bit
IP.0: External interrupt 0 priority bit
Interrupt Priorities Example
--- --- PT2 PS PT1 PX1 PT0 PX0
1. http://www.eg3.com
2. http://www.ARM.MCU.com
3. http://www.mcjournal.com
4. http://www.iar.com
5. http://http://www.embedded.com
6. http://www.powersoftsystems.com
Epilogue
The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.
---- Robert Frost
GOOD LUCK!