Microprocessors and
Microcontrollers
       Module V
                Timer programming
● Two 16-bit timers – Timer 0 and Timer 1
● Each timer accessed as two 8-bit registers
● The timers use 1/12 of crystal frequency regardless of MC
  time.
                                                              2
TMOD register
                3
                            Gate
● Gate is used to start and stop the timer – through H/W or
  S/W
● If gate = 0, S/W control and if gate = 1, H/W control
● In S/W control, TR0 and TR1 bits are set/cleared to
  start/stop the respective timer.
                                                              4
                    TCON Register
● TR0, TR1 are used for starting/stopping the timers.
● TF0 , TF1 are timer flags, raised when the count is reached.
                                                                5
           Timer mode 0 (13-bit mode)
● Lower 5 bits of TLx and 8 bits of THx are used for the
  timer.
● When the count rolls over from all 1s to all 0s, TFx flag is
  set.
                                                                6
                        Timer modes
● Mode 1 (16-bit mode)
  ○ Maximum count is 65536
● Mode 2 (8-bit mode)
  ○ THx is loaded with the 8-bit count and the timer is started.
  ○ 8051 copies the count to TLx and this acts as the timer.
  ○ Once the TF flag is set, TLx is automatically reloaded from THx.
  ○ The programmer need only clear the TF and the timer goes on.
                                                                      7
                                  Program
Generate a square wave of 50% duty cycle on
P1.5.
clock period = 1.085 µs
No. of counts = FFFFH –FFF2H
= 0DH(13D) + 1 (for roll over)
                 = 14
Time = 14 X 1.085 µs = 15.19 µs (for half cycle)
For a more accurate calculation, include the clock
cycles for all the instructions in the subroutine.   8
                      Delay calculation
No. of clocks required = 5 ms / 1.085 µs
            = 4608
The counter value = 65536 – 4608
       = EE00H
                                           9
              Counter programming
● When C/T = 1 in TMOD, it is the counter mode.
● The counter counts the pulses supplied at pins 14 or 15.
                                                             10
Program
          11
            Serial data communication
● Synchronous
  ○ block of characters
● Asynchronous
  ○ one byte at a time
  ○ each character is placed between start and stop bits.
                                                            12
SCON register
                13
                         SCON register
● SBUF register
  ○ 8-bit register used to hold the data for transmission / reception.
                                                                         14
Baud rate calculation
                        Timer 1 in
                        Mode 2 is
                        used to set
                        the baud rate
                                  15
Program
          16
Program
          17
Program
          18
Serial port programming in C
                               19
                   Interrupts of 8051
● Six interrupts
  ○ Reset           - 0000H
  ○ INT0            - 0003H
  ○ Timer 0         - 000BH
  ○ INT1            - 0013H
  ○ Timer 1         - 001BH
  ○ Serial Int      - 0023H
                                        20
Interrupt Enable register
                            21
Enabling/disabling interrupts
       or
                                22
        Programming timer interrupts
● Polling the TF wastes CPU time
● TF is raised by means of interrupts
                                        23
Program
          24
25
Program
          Mode 1 of
          Timer 1
                      26
27
                           Program
Generate a square wave of 50 Hz on P1.2. Assume XTAL = 11.0592 MHz.. a
                                                                         28
Programming external hardware interrupts
                                           29
Programming external hardware interrupts
● INT0 and INT1 are low-level triggered interrupts by default.
● Accessed through pins P3.2 and P3.3.
● If interrupts are enabled, 8051 samples the INT pins once
  every machine cycle.
● In order to make these edge-triggered, we must set IT0 and
  IT1 bits of TCON register.
                                                                 30
TCON register
                31
Programming the serial interrupt
                                   32
Programming the serial interrupt
                                   33
Programming the serial interrupt
                                   34
Programming the serial interrupt
                                   35
Interrupt programming in C
                             36
Interrupt programming in C
                             37
Interrupt programming in C
                             38
Interrupt programming in C
                             39
Interrupt programming in C
                             40
Interfacing 8051 with LCD
     Pin details
     of LCD
            Commands
            of LCD
                            41
            Interfacing 8051 with LCD
Send a high-to-low
pulse on E for enabling
the LCD
                                        42
Program
          43
Program
          44
          Interfacing 8051 with ADC 0808
● 8051 sends start of
   conversion (SOC) signal to
   ADC
● ADC returns end of
   conversion signal (EOC)
● Input 1 is used for ADC –
   hence value of A B C is 001
                                           45
Interfacing 8051 with ADC 0808
                                 46
Interfacing 8051 with ADC 0808
                                 47
Interfacing 8051 with ADC 0808
                                 48
Interfacing 8051 with ADC 0808
                                 49
          Interfacing 8051 with DAC 0808
● Digital inputs are converted to
   current, which is then
   converted into voltage by
   connecting a resistor to Iout
   pin.
● The total current is a function
   of the binary numbers at the
   inputs D0 to D7
                                           50
          Interfacing 8051 with DAC 0808
Write a program to send data to DAC to generate a stair step ramp
                                                                    51
Generating a sine wave using DAC 0808
                                        52
Generating a sine wave using DAC 0808
                                        53