MPMC - Module - 3 - Part-2
MPMC - Module - 3 - Part-2
Timers/Counters
• A timer is a counter that is increased with every time an instruction is executed ( 11.0592MHz)
• Uses of Timers & Counters
• Interval Timing
• Periodic event timing
• Time base for measurements
• Event Counting
• Baud Rate Generation
• 8051 Timers
• 2 16-bit timers (Timer 0 and Timer 1) , (max 65,535)
• Flag is set when the timer overflows
• Timers can be based on internal clock (OSC/6) or from external source (counter mode).
• TMOD - Timer/Counter mode register
• TCON - Timer/Counter control register
Timers/Counters
The 8051 has 2 timers/counters:
• Timer/Counter 0
• Timer/Counter 1
• They can be used as:
• The Timer :Used as a time delay generator.
• The clock source is the internal crystal frequency of the 8051.
• An event counter.
• External input from input pin to count the number of events on registers.
• These clock pulses could represent the number of people passing through
an entrance, or the number of wheel rotations, or any other event that can
be converted to pulses
Timers
• Set the initial value of registers
• Start the timer and then the 8051
counts up.
• Input from internal system clock
(machine cycle)
• When the registers equal to 0 and
the 8051 sets a bit to denote time
out
Counters
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
• External input from Tx input pin.
• Tx is used to denote T0 or T1.
Registers Used in Timer/Counter
• The C/Tx bit is used selects the source of pulses for the Timer to count.
• C/T1 = 1 ==> Timer1 counts pulses from Pin T1 (P3.5) (Counter Mode)
• C/T1 = 0 ==> Timer1 counts pulses from internal oscillator (Timer Mode)
• C/T0 = 1 ==> Timer0 counts pulses from Pin T0 (P3.4) (Counter Mode)
• C/T0 = 0 ==> Timer0 counts pulses from internal oscillator (Timer Mode)
TMOD (Timer Mode)
TxM0 TxM1 Mode Description
CLR TR0
CLR TF0
Peripheral Control Registers: TCON (Timer Control)
• Timer control register TMOD is a 8-bit register which is bit addressable and in which Upper nibble is for
timer/counter, lower nibble is for interrupts
• Timer Control or TCON Register is used to start or stop the Timers of 8051 Microcontroller.
• TF: Overflow flag
• Set by hardware on Timer/Counter overflow
• Cleared by hardware when processor vectors to interrupt routine
• TR: Run control bit
• Set/Cleared by software to turn Timer/Counter on/off
• IE: Interrupt Edge flag
• Set by hardware when external interrupt edge detected
• Cleared when interrupt processed
• IT: Interrupt Type control bit
• Set/Cleared by software to specify falling edge/low level triggered external interrupts
TCON (Timer Control)
• TF1: Timer 1 overflow flag TF0: Timer 0 overflag
• TR1: Timer 1 run control bit TR0: Timer 0 run control bit.
• IE1: External interrupt 1 edge flag IE0: External interrupt 0 edge flag
• IT1: External interrupt 1 type flag IT0: External interrupt 0 type flag.
TCON (Timer Control)
• TR (Timer 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)
the steps are as follows. TH0= 0xFC; //initial value for 1ms
0x66
Machine Cycle
LED Blinking
Interrupts
8051 Interrupt
• An interrupt is an external or internal event that disturbs the
microcontroller to inform it that a device needs its service.
• Starts to execute the interrupt service routine until RETI (return from
interrupt)
• Upon executing the RETI the microcontroller returns to the place where it
was interrupted.
• Serial : 0023h
• These pins are TTL compatible and hence they require a line driver to make
them RS232 compatible
• When it is high, it allows the 8051 to receive data on the RxD pin.
• REN must be set to 1 to receive and transfer data. When REN=0, the
receiver is disabled.
• When 8051 finishes the transfer of 8-bit character, it raises the TI flag to indicate
that it is ready to transfer another byte. The TI bit is raised at the beginning of the
stop bit.
• When the 8051 receives data serially, via RxD, it gets rid of the start and stop bits
and places the byte in the SBUF register.
• Then it raises the RI flag bit to indicate that a byte has been received and should be
picked up before it is lost. RI is raised halfway through the stop bit.
Peripheral Data Registers: SBUF (Serial Data Buffer)
• The Serial Buffer or SBUF register is used to hold the serial data while
transmission or reception.
Peripheral Control Registers: PCON (Power Control)
• The PCON or Power Control register is used to control the 8051 Microcontroller’s Power Modes
and is located at 87H of the SFR Memory Space.
• Using two bits in the PCON Register, the microcontroller can be set to Idle Mode and Power
Down Mode.
• During Idle Mode, the Microcontroller will stop the Clock Signal to the ALU (CPU) but it is given
to other peripherals like Timer, Serial, Interrupts, etc. In order to terminate the Idle Mode, you
have to use an Interrupt or Hardware Reset.
• In the Power Down Mode, the oscillator will be stopped and the power will be reduced to 2V. To
terminate the Power Down Mode, you have to use the Hardware Reset.
• The PCON Register can also be used control the Baud Rate of the Serial Port using SMOD Bit.
• There are two general purpose Flag Bits in the PCON Register, which can be used by the
programmer during execution.
Peripheral Control Registers: PCON (Power Control)
Thank You