UNIT-1 Final Microcontroller
UNIT-1 Final Microcontroller
                   Prof. M. N. Kakatkar
Prof. M. N. Kakatkar
     Three criteria in Choosing a Microcontroller
       Prof. M. N. Kakatkar
1. This system architecture was designed and
   recommended by Harvard University
2. According to this architecture the processor is
   having two different memory spaces
3. They are data memory and program
   memory
4. Therefore the distinction between data
   memory and program memory is physical
5. It is connected to the microcontroller or
   microprocessor through separate sets of
   address, data and control buses
                    Prof. M. N. Kakatkar
  Advantages and Disadvantages
1. Accessed by separate sets of buses, both
   program and data can be accessed
   simultaneously
2. Two sets of buses are needed and therefore
   relatively complex bus structure
• Examples: MCS 8051
               PIC Microcontroller
                  Prof. M. N. Kakatkar
Von – Neumann Architecture
          Prof. M. N. Kakatkar
1. This system architecture was designed and
   recommended by Von-Neumann at the
   Princeton University
2. As per this architecture, the Processor is having
   single memory space. The memory may contain
   both data as well as program memory in it
3. The distinction between data memory and
   program memory is logical
4. Therefore same memory space shares data as
   well as program code
5. It is connected to the processor through a single
   set of address, data and control buses
                     Prof. M. N. Kakatkar
  Advantages and Disadvantages
1. Simple construction, less complex bus
   structure
2. Accessed by same set of buses, both program
   and data cannot be simultaneously accessed
• Example: MC68HC11
                   Prof. M. N. Kakatkar
                 Microcontroller Architecture
               Von Neumann Architecture--- CISC
•   Uses one memory spaces for program instructions and data
•   Limits operating bandwidth
•   Execution of data and code occurs in sequential fashion
•   Reduced Speed of Operation
•   CISC- Complex Instruction Set Computer
                                                            Addr
             Addr
Data Data
Addr Data
                                       Memory
                                 (Program and Data)
                                                               Addr
             Addr
Data Data
                                                Data
                      Program Memory
                                               Memory
                                                                      Ref: T1: Page No 57-58
   The 8051
Microcontroller
        8051 Basic Component
•   4K bytes internal ROM
•   128 bytes internal RAM
•   Four 8-bit I/O ports (P0 - P3).
•   Two 16-bit timers/counters
•   One serial interface
        CPU    RAM    ROM
                              A single chip
        I/O          Serial   Microcontroller
               Timer COM
        Port
                     Port
                      Block Diagram
External Interrupts
CPU
      OSC              Bus
                                   4 I/O Ports          Serial
                      Control
                                 P0 P2 P1        P3   TXD   RXD
                                Addr/Data
            Major 8051 featurs
• only 1 On chip oscillator (external crystal)
• 6 interrupt sources (2 external , 3 internal, Reset)
• 64K external code (program) memory(only read)PSEN
• 64K external data memory(can be read and write) by RD,WR
• Code memory is selectable by EA (internal or external)
• We may have External memory as data and code
Features of 8051 Architecture
8952 8k 256 3 8 32 -
891051 1k 64 1 3 16 AC
892051 2k 128 2 6 16 AC
- With an 8-bit data type, any data larger than 8 bits must be
broken into 8-bit chunks before it is processed
        Registers
       A
       B
       R0             DPH         DPL   DPTR
       R1
       R2                    PC         PC
       R3
       R4           16-bit Register
       R5
       R6
       R7
8-bit Registers
PSW(Program Status Word)
CY AC F0 RS1 RS0 OV -- P
The PSW3 and PSW4 are designed as RS0 and RS1, and are
used to change the bank
GND(pin 20):ground
          10 uF                         31
                                             EA/VPP
                  30 pF                      X1
                                        19
                          11.0592 MHz
      8.2 K
                                             X2
                                        18
                  30 pF
                                         9 RST
Machine cycle
Machine cycle
                Machine Cycle Freq.=1/12 XTAL
•    Solution:
    (a) 11.0592 MHz / 12 = 921.6 kHz;
       machine cycle = 1 / 921.6 kHz = 1.085 s
                     FFFFh     External
      8051 Chip
                                DATA
      Internal RAM             Memory
                             (up to 64KB)
         SFRs        0000h       RAM
                     FFFFh     External
     Internal code
       Memory                   CODE
      (EEPROM)                 Memory
                             (up to 64KB)
                     0000h       ROM
RAM memory space allocation in the 8051
7FH
             30H
             2FH
                                   Bit-Addressable RAM
             20H
             1FH                                         4
                                    Register Bank 3
             18H                                         Register
             17H                                         Banks
                                   Register Bank 2
             10H                                         Each
             0FH                    Register Bank 1      bank has
             08H                                         R0-R7
                                            (Stack)
             07H                                         Selectabl
             00H                   Register Bank 0       e by
                                                         PSW.2,3
-Register bank 0 is the default when 8051 is
powered up
Bit Addressable RAM memory
There are 128 bytes of RAM in the 8051
     -Assigned addresses 00 to 7FH
• Exchange instructions
  XCH a, byte    ;exchange accumulator and byte
  XCHD a, byte   ;exchange low nibbles of
                       ;accumulator and byte
      Exchange Instructions
•    Table Lookup
•    MOVC only can read internal code memory
           SFRs Address
stack pointer
stack
Note: can only specify RAM or SFRs (direct mode) to push or pop. Therefore,
  to push/pop the accumulator, must use acc, not a
                 Stack (push,pop)
• Therefore
      Push    a      ;is   invalid
      Push    r0     ;is   invalid
      Push    r1     ;is   invalid
      push    acc    ;is   correct
      Push    psw    ;is   correct
      Push    b      ;is   correct
      Push    13h
      Push    0
      Push    1
      Pop     7
      Pop     8
      Push    0e0h    ;acc
      Pop     0f0h    ;b
    Bit-Oriented Data Transfer
• transfers between individual bits.
• Carry flag (C) (bit 7 in the PSW) is used as a single-bit
  accumulator
• RAM bits in addresses 20-2F are bit addressable
  mov C, P0.0
  mov C, 67h
  mov C, 2ch.7
         SFRs that are Bit Addressable
     Arithmetic Instructions
        Logic Instructions
          Arithmetic Instructions
•   Add
•   Subtract
•   Increment
•   Decrement
•   Multiply
•   Divide
•   Decimal adjust
       Arithmetic Instructions
Mnemonic       Description
ADD A, byte    add A to byte, put result in A
ADDC A, byte   add with carry
SUBB A, byte   subtract with borrow
INC A          increment A
INC byte       increment byte in memory
INC DPTR       increment data pointer
DEC A          decrement accumulator
DEC byte       decrement byte
MUL AB         multiply accumulator by b register
DIV AB         divide accumulator by b register
DA A           decimal adjust the accumulator
                   ADD Instructions
add a, byte                   ; a  a + byte
addc a, byte                            ; a  a + byte + C
These instructions affect 3 bits in PSW:
C = 1 if result of add is greater than FF
AC = 1 if there is a carry out of bit 3
OV = 1 if there is a carry out of bit 7, but not from bit 6, or visa versa.
Instructions that Affect PSW bits
              ADD Examples
mov a, #3Fh            • What is the value of the C,
add a, #D3h              AC, OV flags after the
                         second instruction is
                         executed?
  0011 1111
  1101 0011
  0001 0010
              C = 1
              AC = 1
              OV = 0
                     Subtract
Example:
SUBB A, #0x4F     ;A  A – 4F – C
Notice that
There is no subtraction WITHOUT borrow.
Therefore, if a subtraction without borrow is desired,
it is necessary to clear the C flag.
Example:
Clr c
SUBB A, #0x4F      ;A  A – 4F
        Increment and Decrement
    INC A               increment A
    INC byte            increment byte in memory
    INC DPTR            increment data pointer
    DEC A               decrement accumulator
    DEC byte            decrement byte
                            FF x FF = FE01
                         (255 x 255 = 65025)
MUL AB ; BA  A * B
• Integer Division
    DIV AB          ; divide A by B
    A  Quotient(A/B)
    B  Remainder(A/B)
DA a ; decimal adjust a
Example:
         mov a, #23h
         mov b, #29h
         add a, b               ; a  23h + 29h = 4Ch (wanted 52)
         DA a                   ; a  a + 6 = 52
              Logic Instructions
                                00001111
                          XRL   10101100
                                10100011
                          CPL   10101100
                                01010011
       Uses of Logic Instructions
CLR -    clear
RL   –   rotate   left
RLC –    rotate   left through Carry
RR   –   rotate   right
RRC –    rotate   right through
 Carry
SWAP –   swap accumulator nibbles
           CLR ( Set all bits to 0)
CLR A
CLR byte        (direct mode)
CLR Ri          (register mode)
CLR @Ri         (register indirect mode)
                  Rotate
• Rotate instructions operate only on a
RL a
Mov a,#0xF0     ; a 11110000
RR a            ; a 11100001
RR a
Mov a,#0xF0     ; a 11110000
RR a            ; a 01111000
         Rotate through Carry
                               C
RRC a
mov a, #0A9h   ; a  A9
add a, #14h    ; a  BD (10111101), C0
rrc a          ; a  01011110, C1
                               C
RLC a
SWAP a
   ANL C, bit
   ORL C, bit
   CLR C
   CLR bit
   CPL C
   CPL bit
   SETB C
   SETB bit
• Conditional jumps
   loop: mov a, P1
         jz loop           ; if a=0, goto loop,
                           ; else goto next instruction
           mov b, a
             jz led_off
             Setb P1.6
             sjmp skipover
    led_off: clr P1.6
             mov A, P0
   skipover:
       More Conditional Jumps
        Mnemonic                  Description
CJNE A, #data <rel addr>    Compare A and data, jump
                            if not equal
CJNE Rn, #data <rel addr>   Compare Rn and data,
                            jump if not equal
CJNE @Rn, #data <rel addr> Compare Rn and memory,
                           jump if not equal
DJNZ Rn, <rel addr>         Decrement Rn and then
                            jump if not zero
For A = 0 to 4 do        For A = 4 to 0 do
    {…}                      {…}
                                 mov R0, #4
      clr a              loop: ...
loop: ...                        ...
      ...                        djnz R0, loop
      inc a
      cjne a, #4, loop
      Iterative Loops(examples)
      mov a,#50h               mov a,#25h
      mov b,#00h               mov r0,#10h
      cjne a,#50h,next         mov r2,#5
      mov b,#01h         Again: mov @ro,a
next: nop                      inc r0
      end                      djnz r2,again
                               end
      mov a,#0aah
      mov b,#10h               mov a,#0h
Back1:mov r6,#50               mov r4,#12h
Back2:cpl a              Back: add a,#05
      djnz r6,back2            djnz r4,back
      djnz b,back1             mov r5,a
      end                      end
              Call and Return
     ret                ; PC  stack
                 Subroutines
                                   call to the subroutine
 Main:      ...
            acall sublabel
            ...
            ...
sublabel:   ...
            ...
                       the subroutine
            ret
          Why Subroutines?
• When using subroutines, the stack will be used to store the PC, so it is very
  important to initialize the stack pointer. Location 2Fh is often used.
 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 inside an interrupt
  ---     ---    PT2     PS     PT1      PX1   PT0      PX0
-
Counter
• A counter of 8051 is incremented in response to a
  transition from ‘1’ to ‘0’ at its corresponding
  external pin(either T0/T1).
• Thus counter output will show the number of events
  representing of such ‘1’ to ‘0’ transitions .
   – External input from T0 input pin (P3.4) for
     Counter 0
   – External input from T1 input pin (P3.5) for
     Counter 1
                                   TH0 P1          to
                                                   LCD
                                    TL0
                  a switch    T0    P3.4
                                    8051
Registers Used in Timer/Counter
TH0 TL0
Timer 0
TH1 TL1
                          Timer 1
  TMOD Register
  • Timer mode register: TMOD
     MOV TMOD,#21H
     – An 8-bit register
     – Set the usage mode for two timers
        • Set lower 4 bits for Timer 0 (Set to 0000 if not
          used)
        • Set upper 4 bits for Timer 1 (Set to 0000 if not
          used)
     – Not bit-addressable
(MSB)                                                  (LSB)
GATE    C/T    M1       M0     GATE    C/T   M1        M0
          Timer 1                       Timer 0
TMOD Register
GATE Gating control when set. Timer/counter is enabled only
      while the INTx pin is high and the TRx control bit is
      set. When cleared, the timer is enabled whenever the
      TRx control bit is set.
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    Mode bit 1
M0    Mode bit 0
   (MSB)                                            (LSB)
    GATE   C/T     M1    M0    GATE   C/T     M1    M0
             Timer 1                    Timer 0
Timer modes
TCON Register
 • Timer control register: TCON
    – Upper nibble for timer/counter, lower nibble for
      interrupts
 • TR (run control bit)
    – TR0 for Timer/counter 0; TR1 for Timer/counter 1.
    – TR is set by programmer to turn timer/counter
      on/off.
        • TR=0: off (stop)
        • TR=1: on (start)
(MSB)                                                  (LSB)
 TF1      TR1    TF0   TR0      IE1    IT1     IE0    IT0
       Timer 1   Timer0               for Interrupt
 TCON Register
 (MSB)                                                     (LSB)
  TF1      TR1    TF0   TR0        IE1     IT1     IE0     IT0
        Timer 1   Timer0                  for Interrupt
Steps of Mode 1
5. When TH0-TL0 rolls over from FFFFH to 0000, the 8051 set
   TF0=1.
   TH0-TL0= FFFEH, FFFFH, 0000H (Now TF0=1)
6. Keep monitoring the timer flag (TF) to see if it is raised.
   AGAIN: JNB TF0, AGAIN
7. Clear TR0 to stop the process.
   CLR TR0
8. Clear the TF flag for the next round.
   CLR TF0
Mode 1 Programming
  XTAL
oscillator    ÷ 12
              C/T = 0
                                                  Timer
                                                 overflow
                                                   flag
                              TH      TL            TF
             TR
                        TF goes high when FFFF      0
    Timer Delay Calculation for XTAL = 11.0592 MHz
(a) in hex
• (FFFF – YYXX + 1) × 1.085 s
• where YYXX are TH, TL initial values respectively.
• Notice that values YYXX are in hex.
(b) in decimal
• Convert YYXX values of the TH, TL register to decimal
    to get a NNNNN decimal number
•    then (65536 – NNNNN) × 1.085 s
Counter
   1: -3V to -25V;
   0: +3V to +25V
  Reason: for long distance wired line
Input-output voltage are not TTL
 compatible
So, we need MAX232/233 for voltage
 converter. Commonly known as line drivers
           RS232 Pins
Connectors:
Minimally, 3 wires: RxD, TxD, GND
Could have 9-pin or 25-pin
         DB-25                           DB-9
    25-Pin Connector                9-Pin Connector
DTE and DCE
        Null Modem Connection
Null modem is a communication method to
 connect two DTEs (computer, terminal, printer
 etc.) directly using a RS-232 serial cable.
With a null modem connection the transmit and
 receive lines are crosslinked.
Depending on the purpose, sometimes also one
 or more handshake lines are crosslinked.
                                         10-143
  PIC18 Connection to RS232
Line driver
SBUF register
                                  B1
    Read pin
   Hardware Structure of I/O Pin
             MOV A,#55H
   BACK:     MOV P1,A
             ACALL DELAY
             CPL A
             SJMP BACK
   – Let P1 toggle.
   – You can write to P1 directly.
  Writing “1” to Output Pin P1.X
                                     B1
       Read pin
     Writing “0” to Output Pin P1.X
                                     B1
       Read pin
Reading “High” at Input Pin
                                     B1
      Read pin
3. Read pin=1 Read
      latch=0
 Reading “Low” at Input Pin
                                     B1
      Read pin
3. Read pin=1 Read
      latch=0
Other Pins
• P0 is an open drain.
   – Open drain is a term used for MOS chips in the same
     way that open collector is used for TTL chips.
• When P0 is used for simple data I/O we must connect it to
  external pull-up resistors.
   – Each pin of P0 must be connected externally to a 10K
     ohm pull-up resistor.
   – With external pull-up resistors connected upon reset,
     port 0 is configured as an output port.
Port 0 with Pull-Up Resistors
               Vcc
                                10 K
               P0.0
      8051     P0.1
                                Port
               P0.2
      8951     P0.3
               P0.4              0
               P0.5
               P0.6
               P0.7
Port 3 Alternate Functions
            P3.0         RxD        10
            P3.1         TxD        11
            P3.2         INT0       12
            P3.3         INT1       13
            P3.4         T0         14
            P3.5         T1         15
            P3.6         WR         16
            P3.7         RD         17