0% found this document useful (0 votes)
28 views

8051 - Basics

A microcontroller is a single-chip computer containing a processor core, memory, and input/output peripherals. It is different from a microprocessor in that all components are integrated on a single chip. Specifically, a microcontroller contains RAM, ROM, I/O ports, and other peripherals like timers on the same chip, while a microprocessor requires external components connected via a motherboard. The 8051 is a common 8-bit microcontroller with features like 4KB program memory, 128B data memory, 32 I/O lines, timers, interrupts, and serial communication port integrated on a single chip.

Uploaded by

Peace
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

8051 - Basics

A microcontroller is a single-chip computer containing a processor core, memory, and input/output peripherals. It is different from a microprocessor in that all components are integrated on a single chip. Specifically, a microcontroller contains RAM, ROM, I/O ports, and other peripherals like timers on the same chip, while a microprocessor requires external components connected via a motherboard. The 8051 is a common 8-bit microcontroller with features like 4KB program memory, 128B data memory, 32 I/O lines, timers, interrupts, and serial communication port integrated on a single chip.

Uploaded by

Peace
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Difference

 between  Microprocessor  
and  Microcontroller  
 
 
CRO CONTROLLER MICRO PROCESSER
s a single chip • It is a cpu
nsists Memory, • Memory, I/O Ports to be
ports connected externally.

CPU
CPU MEMORY
MEMORY

I/O PORTS I/O PORTS

ever
Small size
Low cost
Low power
•  What  is  a  Microcontroller?  
•   A  single  chip  computer  or  A  CPU  with  all  the  
peripherals      like    RAM,  ROM,    I/O  Ports,      
Timers    ,  ADCs  etc...  on  the    same  chip.  For  ex:  
Motorola’s  6811,  Intel’s  8051,  Zilog’s      Z8  and  
PIC  16X  etc.  
4-­‐bit  microcontroller  

!
8-­‐bit  microcontroller  

!
16-­‐bit  microcontroller  

!
The  salient  features  of  8051      
Microcontroller  :-­‐  
i.    4  KB  on  chip  program  memory  (ROM  or  EPROM)).  
ii.  128  bytes  on  chip  data  memory(RAM).  
iii.  8-­‐bit  data  bus  
iv.  16-­‐bit  address  bus  
v.  32  general  purpose  registers  each  of  8  bits  
vi.  Two  -­‐16  bit  Zmers/counters    T0  and  T1  
vii.  Five  Interrupts  (3  internal  and  2  external).  
ix.  Four    Parallel  ports  each  of  8-­‐bits  (PORT0,  PORT1,PORT2,PORT3)  with  
a  total  of  32  I/O    lines.  
x.  One  16-­‐bit  program  counter  and  One  16-­‐bit  DPTR  (  data  pointer)  
xi.  One  8-­‐bit  stack  pointer  
xii.  One    Microsecond  instrucZon  cycle  with  12  MHz  Crystal.  
xiii.  One  full  duplex    serial  communicaZon  port.  
 
A  Single  chip  microcontroller  
Inside  Architecture  of  8051  
External  interrupts  
On-­‐chip   Timer/Counter  

Interrupt   ROM  for  


On-­‐chip   Timer  1   Counter  
Control   program  
RAM   Inputs  
code   Timer  0  

CPU  

Bus   Serial  
4 I/O  Ports  
OSC   Control   Port  

P0  P1  P2  P3   TxD    RxD  


Address/Data  

10  
PIN  Diagram  of  8051  Microcontroller    

!
•  The  8051  microcontroller  is  available  as  a  40  
pin  DIP  chip.    
•   It  works  at  +5  volts  DC.    
•  Among  the  40  pins    ,  a  total  of  32  pins  are  
allo_ed  for  the  four  parallel  ports    P0,P1,P2  
and  P3  i.e  each  port  occupies  8-­‐pins  .  
•  The  remaining  pins  are  VCC,  GND,  XTAL1,  
XTAL2,  RST,  EA  ,PSEN.    
XTAL1,XTAL2:    
 These  two  pins  are  connected  to  Quartz  crystal  
oscillator    which  runs  the  on-­‐chip  oscillator.  The  
quartz  crystal  oscillator  is  connected  to  the  two  
pins  along  with  a  capacitor  of  30pF  as  shown  in  
the  circuit.  If    we  use  a  source  other  than    the  
crystal  oscillator,  it  will  be  connected  to  XTAL1  
and  XTAL2  is  lef  unconnected.    
 
 
!
•  RST:  The  RESET  pin  is  an  input  pin  and  it  is  an  
acZve    high  pin.  When  a  high  pulse  is  applied  
to  this  pin  the  microcontroller  will  reset  and  
terminate  all  acZviZes.  Upon  reset  all  the  
registers  except  PC  will  reset  to  0000  Value  
and  PC    register  will  reset  to  0007  value.  
 
Register Reset Value

PC 0000

ACC 0000

B 0000

PSW 0000

SP 0007

DPTR 0000

RAM are all zero


•   (External  Access):  This  pin  is    an  acZve  low  
pin.  This  pin  is  connected  to  ground  when  
microcontroller  is  accessing  the  program  code  
stored  in  the    external  memory    and  
connected  to  Vcc  when  it  is  accessing  the  
program  code  in  the    on  chip  memory.  This  pin  
should  not  be  lef  unconnected.  
•  PSEN(Program  Store  Enable)  :  This  is  an  output  
pin  which  is  acZve  low.  When  the  microcontroller  
is  accessing  the  program  code  stored  in  the  
external  ROM  ,this  pin  is  connected  to  the  OE  
(Output  Enable)  pin  of  the  ROM.  
•  ALE  (Address  latch  enable):  This  is  an  output  pin,  
which  is  acZve  high.  When  connected  to  external  
memory  ,  port  0  provides  both  address  and  data  
i.e  address  and  data  are  mulZplexed    through  
port  0  .This  ALE  pin  will  demulZplex  the  address  
and  data  bus  .When  the  pin  is  High  ,  the  AD  bus  
will  act  as  address  bus    otherwise  the  AD  bus  will  
act  as  Data  bus.  
•  P0.0-­‐  P0.7(AD0-­‐AD7)  :  The  port  0  pins  
mulZplexed  with  Address/data  pins  .If  the  
microcontroller  is  accessing  external  memory  
these  pins  will  act  as  address/data  pins  otherwise  
they  are  used  for  Port  0  pins.  
•  P2.0-­‐  P2.7(A8-­‐A15)  :  The  port2  pins  are  
mulZplexed  with  the  higher  order  address  
pins  .When  the  microcontroller  is  accessing  
external  memory  these  pins  provide  the  higher  
order  address  byte  otherwise  they  act  as  Port  2  
pins.  
•  P1.0-­‐  P1.7  :These  8-­‐pins  are  dedicated  for  
Port1  to  perform  input  or  output  port  
operaZons.  
•  P3.0-­‐  P3.7    :These  8-­‐pins  are  meant  for  Port3  
operaZons  and  also  for  some  control  
operaZons    like    
Read,Write,Timer0,Timer1  ,INT0,INT1  ,RxD  
and  TxD  
Address Multiplexing
for External Memory

Accessing
external
code
memory
Accessing External
Data Memory

Figure
2-11
Interface
to 1K
RAM
External code memory
WR  
RD  
PSEN   OE  
ALE   74LS373   CS  
G  
P0.0     A0    
D
P0.7   A7  

D0    
D7  
EA  
P2.0     A8    
 P2.7   A15  

8051   ROM  
External data memory
WR   WR  
RD   RD  
PSEN  
ALE   74LS373   CS  
G  
P0.0     A0    
D
P0.7   A7  

D0    
D7  
EA  
P2.0     A8    
 P2.7   A15  

8051   RAM  
8051 Internal Block Diagram
 
•  A  and  B  Registers  :  The  A    and  B  registers  are  special  funcZon  
registers  which  hold  the  results  of  many  arithmeZc  and  logical  
operaZons  of  8051.The  A  register  is  also  called  the  Accumulator  
and        as  it’s  name  suggests,  is  used  as  a  general  register  to  
accumulate  the  results  of  a  large    number  of  instrucZons.  By  
default  it  is  used  for  all  mathemaZcal  operaZons  and  also  data  
transfer  operaZons  between  CPU  and  any  external  memory.    
•     
•  The  B  register  is  mainly  used  for  mulZplicaZon  and  division  
operaZons  along  with  A  register.    
•                                                   MUL  AB        :                                      DIV  AB.  
•  It  has  no  other  funcZon  other  than  as  a  locaZon  where  data  may  be  
stored.  
•  The  R  registers:    The  "R"  registers  are  a  set  of  
eight  registers  that  are  named  R0,  R1,  etc.  up  
to      and  including  R7.  These  registers  are  used  
as  auxillary  registers  in  many  operaZons.  The  
"R"  registers  are  also  used  to  temporarily  
store  values.  
•  Program  Counter(PC)  :  8051  has  a  16-­‐bit  
program  counter  .The  program  counter  always  
points  to  the  address  of  the  next  instrucZon  to  be  
executed.  Afer  execuZon  of  one  instrucZon  the  
program  counter  is  incremented  to  point  to  the  
address  of  the  next  instrucZon  to  be  executed.It  
is  the  contents  of  the  PC  that  are  placed  on  the  
address  bus  to  find  and  fetch  the  desired  
instrucZon.Since  the  PC  is  16-­‐bit  width  ,8051  can  
access  program  addresses  from  0000H  to  
FFFFH  ,a  total  of  6kB  of  code.  
Data  Pointer  Register(DPTR)    
•  It  is  a  16-­‐bit  register    which  is  the    only    user-­‐
accessible.    DPTR,  as  the  name  suggests,  is  
used  to  point  to  data.  It  is  used  by  a  number  
of  commands  which  allow  the  8051  to  access  
external  memory.  When  the  8051  accesses  
external  memory  it  will  access  external  
memory  at  the  address  indicated  by    DPTR.  
This  DPTR  can  also  be  used  as  two  8-­‐registers  
DPH  and  DPL.  
Registers  
A

B
R0
DPTR DPH DPL
R1

R2 PC PC
R3

R4 Some  8051  16-­‐bit  Register


R5

R6

R7

Some  8-­‐bit  Registers  


of  the  8051
•  Stack  Pointer  Register  (SP)  :    It  is  an  8-­‐bit  register  which  
stores  the  address  of  the  stack  top.  i.e  the  Stack  Pointer  is  
used  to  indicate  where  the  next  value    to  be  removed    from  
the  stack  should  be  taken  from.  When  a  value  is  pushed  
onto  the  stack,  the  8051  first  increments  the  value  of  SP  
and  then  stores  the  value  at  the  resulZng  memory  locaZon.  
Similarly  when  a  value  is  popped  off  the  stack,  the  8051  
returns  the  value  from  the  memory  locaZon  indicated  by  
SP,  and  then  decrements  the  value  of  SP.  Since  the  SP  is  
only  8-­‐bit  wide  it  is  incremented  or  decremented  by  two  .  
SP  is  modified  directly  by  the  8051  by  six  instrucZons:  
PUSH,  POP,  ACALL,  LCALL,  RET,  and    RETI.  It  is  also  used  
intrinsically  whenever  an  interrupt  is  triggered.  
•  STACK  in  8051  Microcontroller  :  The  stack  is  a    part  of  RAM  used  by  the  
CPU  to  store  informaZon    temporarily.  This  informaZon  may  be  either  
data  or  an  address    .The  CPU  needs  this  storage  area    as  there  are  only  
limited  number  of  registers.  The  register  used  to  access  the  stack  is  called  
the    Stack    pointer    which  is  an  8-­‐bit  register..So,it  can  take  values  of  00  to  
FF  H.When  the  8051  is  powered  up  ,the  SP  register  contains  the  value  
07.i.e  the  RAM  locaZon  value  08  is  the  first  locaZon  being  used  for  the  
stack  by  the  8051  controller  
           EX  :    MOV  R6  ,#35  H  
                           MOV  R1  ,#21  H  
                         PUSH  R6  
                     PUSH  R1  
•     In  the  above  instrucZons  the  contents  of  the  Registers  R6  and  R1    are  
moved  to  stack    and  they  occupy  the  08  and  09  locaZons  of  the  stack.Now  
the  contents  of  the  SP  are  incremented  by  two  and  it  is    0A  
•  Similarly  POP  R3  instrucZon  pops  the  contents  of  stack  into  R3  
register.Now  the  contents  of  the  SP  is  decremented  by  1  
•  In  8051  the  RAM  locaZons  08  to  1F  (24  bytes)  can  be  used    for  the  Stack.In  
any  program  if  we  need  more  than  24  bytes    of  stack  ,we  can  change  the  
SP  point  to  RAM  locaZons  30-­‐7F  H.this  can  be  done  with  the    instrucZon    
MOV  SP,#  XX.  
On-Chip Memory
Internal RAM
Registers
1F  
 
 
 
Bank  3  
 
  Four Register Banks
 
18  
Each bank has R0-R7
17   Selectable by psw.2,3
 
 
 
 
Bank  2  
 
 
10  
0F  
 
 
 
 
Bank  1  
 
 
08  
07   R7  
06   R6  
05   R5  
04  
03  
R4  
R3  
Bank  0  
02   R2  
01   R1  
00   R0  
Bit Addressable Memory
2F   7F 78 20h  –  2Fh  (16  locaZons  X  8-­‐bits  =  
2E   128  bits)  
2D  
2C   Bit  addressing:  
2B    mov  C,  1Ah  
2A    or  
29    mov  C,  23h.2  
28  
27  
26  
25  
1A
24  
10
23  
0F 08
22  
07 06 05 04 03 02 01 00
21  
20  
Special Function Registers

q DATA registers

q CONTROL registers
v Timers
v Serial ports
v Interrupt system
v Analog to Digital converter
v Digital to Analog converter Addresses 80h – FFh
v Etc.
Direct Addressing used to
access SPRs
Bit Addressable RAM

Summary
of the 8051
on-chip
data
memory
(RAM)
Example  
•  SETB    25H  ;sets  the  bit  25H(becomes  1)  
       Bit  25H  is  actually  bit  5  of  internal  RAM  
locaZon  24H  
 
•   CLR  25H  ;clears  the  bit  25H(becomes  0)  
•  SETB  P2.3  
       
Bit Addressable RAM

Summary
of the 8051
on-chip
data
memory
(Special
Function
Registers)
Register Banks

q  Active bank selected by PSW [RS1,RS0] bit


Internal  ROM  (On  –chip  ROM):    

!
PARALLEL  I  /O  PORTS    
•  The  8051  microcontroller  has  four    parallel  I/O  
ports  ,  each  of  8-­‐bits  .So,  it      provides  the  user    32  
I/O  lines  for  connecZng  the    microcontroller    to  
the  peripherals.  The  four  ports  are    P0  (Port  0),  
P1(Port1)  ,P2(Port  2)  and  P3  (Port3).  Upon  reset  
all  the  ports  are  output  ports.  In  order  to  make  
them  input,  all  the  ports  must  be  set    i.e  a  high  
bit    must  be  sent  to  all  the  port  pins.  This  is  
normally  done  by  the  instrucZon    “SETB”.  
•  Ex:  MOV  A,#0FFH                  ;    A  =    FF    
•               MOV  P0,A                                ;  make  P0  an  input  port  
Inside  Architecture  of  8051  
External  interrupts  
On-­‐chip   Timer/Counter  

Interrupt   ROM  for  


On-­‐chip   Timer  1   Counter  
Control   program  
RAM   Inputs  
code   Timer  0  

CPU  

Bus   Serial  
4 I/O  Ports  
OSC   Control   Port  

P0  P1  P2  P3   TxD    RxD  


Address/Data  
Figure 1-2. Inside the 8051 Microcontroller Block Diagram
47  
PORT  0  
•  Port  0    is  an  8-­‐bit  I/O  port  with  dual  purpose.  
If  external  memory  is  used,  these  port  pins  are  
used  for  the  lower  address  byte  address/data  
(AD0-­‐AD7),  otherwise  all  bits  of  the  port  are  
either  input  or  output..  Unlike  other  ports,  
Port  0    is  not  provided  with  pull-­‐up  resistors  
internally  ,so  for  PORT0  pull-­‐up  resistors  of  
nearly  10k  are  to  be  connected  externally  as  
shown  in  the  fig.  
!
•  Dual  role  of  port  0:  Port  0    can  also  be  used  as  
address/data  bus(AD0-­‐AD7),  allowing  it  to  be  
used  for  both  address  and  data.  When  
connecZng  the  8051  to  an  external  memory,  port  
0  provides  both  address  and  data.  The  8051  
mulZplexes  address  and  data  through  port  0  to  
save  the  pins.  ALE  indicates  whether    P0  has  
address  or  data.  When  ALE  =  0,  it  provides  data  
D0-­‐D7,  and    when  ALE  =1  it  provides    address  and  
data  with  the  help  of  a  74LS373  latch.  
External code memory
WR  
RD  
PSEN   OE  
ALE   74LS373   CS  
G  
P0.0     A0    
D
P0.7   A7  

D0    
D7  
EA  
P2.0     A8    
 P2.7   A15  

8051  
ROM  
External data memory
WR   WR  
RD   RD  
PSEN  
ALE   74LS373   CS  
G  
P0.0     A0    
D
P0.7   A7  

D0    
D7  
EA  
P2.0     A8    
 P2.7   A15  

8051   RAM  
Port  1    
•  Port  1  occupies  a  total  of  8  pins  (pins  1  through  8).  It  
has  no  dual  applicaZon  and  acts  only  as    input  or  
output  port.  In  contrast  to  port  0,  this  port  does  not  
need  any  pull-­‐up  resistors  since    pull-­‐up  resistors  
connected    internally.  Upon  reset,  Port  1  is  configured  
as  an  output  port.  To  configure  it  as  an  input  port  ,  
port  bits    must  be  set    i.e  a  high  bit    must  be  sent  to  all  
the  port  pins.  This  is  normally  done  by  the  instrucZon    
“SETB”.  For  Ex  :  
•   MOV  A,  #0FFH      ;  A=FF  HEX  
•   MOV  P1,A        ;              make  P1  an  input  port  by  wriZng    1’s  
to  all  of  its  pins  
Hardware Structure of I/O Pin  

Read  latch   Vcc  


TB2  
 Load(L1)  

Internal  CPU   D              Q   P1.X  


bus   pin  
  P1.X    

Write  to  latch   Clk          Q   M1  

TB1  
Read  pin  
Writing “1” to Output Pin P1.X

Read  latch   Vcc  


TB2  
 Load(L1)   2.  output  pin  is  
1.  write  a  1  to  the  pin   Vcc  
D              Q   1   P1.X  
Internal  CPU  
bus   pin  
  P1.X    
0   output  1  
Write  to  latch   Clk          Q   M1  

TB1  
Read  pin  
Writing “0” to Output Pin P1.X

Read  latch   Vcc  


TB2  
 Load(L1)   2.  output  pin  is  
1.  write  a  0  to  the  pin   ground  
D              Q   0   P1.X  
Internal  CPU  
bus   pin  
  P1.X    
1   output  0  
Write  to  latch   Clk          Q   M1  

TB1  
Read  pin  
Reading “High” at Input Pin

Read  latch   Vcc   2.  MOV  A,P1    


TB2   external  pin=High  
1.  write a 1 to the pin MOV  Load(L1)  
P1,#0FFH

1   1   P1.X  pin  
Internal  CPU  bus   D              Q  
  P1.X    
0   M1  
Write  to  latch   Clk          Q  

TB1  
Read  pin  
3.  Read  pin=1  Read  latch=0  
Write  to  latch=1  
Reading “Low” at Input Pin

Read  latch   Vcc   2.  MOV  A,P1  


TB2  
1.  write a 1 to the pin  Load(L1)   external  pin=Low  
MOV P1,#0FFH
1   0   P1.X  pin  
Internal  CPU  bus   D              Q  
  P1.X    
0   M1  
Write  to  latch   Clk          Q  

TB1  
Read  pin  
3.  Read  pin=1  Read  latch=0  
Write  to  latch=1  
8051  IC  
Port  2    
•  Port  2  is  also  an  eight  bit  parallel  port.  (pins  21-­‐  
28).  It  can  be  used  as  input  or  output  port.  As  this  
port  is  provided  with  internal  pull-­‐up  resistors  it    
does  not  need  any  external  pull-­‐up  resistors.  
Upon  reset,  Port  2  is  configured  as  an  output  
port.  If  the  port  is  to  be  used  as  input  port,  all  the  
port  bits  must  be  made  high  by  sending  FF  to  the  
port.  For  ex,  
•  MOV  A,  #0FFH                  ;  A=FF  hex  
•   MOV  P2,  A                                ;  make  P2  an  input  port  by  
wriZng  all  1’s  to  it  
Dual  role  of  port  2    
•  Port2    lines  are  also  associated  with  the  higher  order  
address  lines  A8-­‐A15.  In  systems  based  on  the  8751,  
8951,  and  DS5000,  Port2  is  used  as  simple  I/O  port..  
But,  in  8031-­‐based  systems,  port  2  is  used  along  with  
P0  to  provide  the  16-­‐bit  address  for  the  external  
memory.  Since  an  8031  is  capable  of  accessing  64K  
bytes  of  external  memory,  it  needs  a  path  for  the  16  
bits  of  the  address.  While  P0  provides  the  lower  8  bits  
via  A0-­‐A7,  it  is  the  job  of  P2  to  provide  bits  A8-­‐A15  of  
the  address.  In  other  words,  when  8031  is  connected  
to  external  memory,  Port  2  is  used  for  the  upper  8  bits  
of  the  16  bit  address,  and  it  cannot  be  used  for  I/O  
operaZons.  
PORT  3    
•  Port3  is  also  an  8-­‐bit  parallel  port    with  dual  funcZon.(  pins  
10  to  17).  The  port  pins  can    be  used    for  I/O  operaZons        
as  well  as  for    control    operaZons.  The  details  of  these  
addiZonal    operaZons  are  given  below  in  the  table.  Port  3  
also  do  not  need  any      external  pull-­‐up  resistors  as  they  are  
provided  internally      similar  to  the  case  of  Port2  &  Port  1.  
Upon  reset    port  3  is  configured  as  an  output  port  .  If  the  
port  is  to  be  used  as  input  port,  all  the  port  bits  must  be  
made  high  by  sending  FF  to  the  port.  For  ex,  
•  MOV  A,  #0FFH                  ;  A=  FF  hex  
•   MOV  P3,  A                                ;  make  P3  an  input  port  by  wriZng  all  1’s  
to  it  

You might also like