[go: up one dir, main page]

0% found this document useful (0 votes)
29 views55 pages

MPMC - Module - 3 - Part-2

The 8051 microcontroller has two 16-bit timers/counters called Timer 0 and Timer 1. They can be used as timers to generate time delays using the internal clock, or as event counters by counting external input pulses. The timers use two 8-bit registers each (THx and TLx) and can operate in different modes like 13-bit, 16-bit, auto-reload, and split timer modes using the TMOD register. The TCON register controls starting and stopping the timers using the TRx bit and checks for overflow using the TFx bit. Precise time delays can be generated by calculating the reload value based on the required delay and timer clock period.

Uploaded by

atharv atre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views55 pages

MPMC - Module - 3 - Part-2

The 8051 microcontroller has two 16-bit timers/counters called Timer 0 and Timer 1. They can be used as timers to generate time delays using the internal clock, or as event counters by counting external input pulses. The timers use two 8-bit registers each (THx and TLx) and can operate in different modes like 13-bit, 16-bit, auto-reload, and split timer modes using the TMOD register. The TCON register controls starting and stopping the timers using the TRx bit and checks for overflow using the TFx bit. Precise time delays can be generated by calculating the reload value based on the required delay and timer clock period.

Uploaded by

atharv atre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Timers & Counters

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

• 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 (Timer mode)

• One 8-bit control register-TCON (Timer Control)


TL0/TH0 (Timer 0 Low/High)
• The Timer 0 consists of two SFRs: TL0 and TH0.
• The TL0 is the lower byte and the TH0 is the higher byte and together
they form a 16-bit Timer0 Register.
TL1/TH1 (Timer 1 Low/High)
• The TL1 and TH1 are the lower and higher bytes of the Timer 0.
Peripheral Control Registers: TMOD (Timer Mode)
• The TMOD or Timer Mode register or SFR is used to set the Operating
Modes of the Timers T0 and T1.
• The lower four bits are used to configure Timer0 and the higher four bits are
used to configure Timer1.
• Both Timer 0 &Timer 1 use the same Mode register TMOD.
• It is not bit addressable.
• It is used similar to any other register of 8051 . For ex: MOV TMOD, #21H
TMOD (Timer Mode)
Every timer has a mean of starting and stopping.
• GATE=0
• Internal control
• The start and stop of the timer are controlled by way of software.
• Set/clear the TR for start/stop timer.
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 (Timer Mode)
• The Gate(x) bit is used to operate the Timer(x) with respect to the INT(x) pin
or regardless of the INT(x) pin.
• GATE1 = 1 ==> Timer1 is operated only if INT1 is SET.
• GATE1 = 0 ==> Timer1 is operates irrespective of INT1 pin.
• GATE0 = 1 ==> Timer0 is operated only if INT0 is SET.
• GATE0 = 0 ==> Timer0 is operates irrespective of INT0 pin.

• 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

0 0 0 13-bit Timer Mode, 8-bit THx + 5-bit TLx ((x= 0 or 1))

0 1 1 16-bit Timer Mode, 8-bit THx + 8-bit TLx

1 0 2 8-bit Auto Reload Timer Mode, 8-bit auto reload timer/counter;


THx holds a value which is to be reloaded into TLx each time it overflows.

1 1 3 Two 8-bit Timer Mode or Split Timer Mode

x = 0 for Timer 0 and x = 1 for Timer 1.


TMOD (Timer Mode)
Timers
Timer Modes
- 0: 13 bit timer
- 1: 16-bit timer
- 2: 8-Bit auto reload
- 3: Split timer mode

Mode 0: 13-Bit Timer


- 5 Lower bits of lower byte(TL0/TL1) + all 8 bits of upper byte (TH0/TH1).
- Backward compatible to the 8048
- Not generally used
Timers
Mode 1: 16-bit
- All 16 bits of the timer (TH0/TL0, TH1,TL1) are used.
- Maximum count is 65,536
- At 12Mhz, maximum interval is 65536 microseconds or 65.536 milliseconds
- TF0 must be reset after each overflow
- THx / TLx must be manually reloaded after each overflow.

Mode 2: 8-bit Auto Reload


- Only the lower byte (TLx) is used for counting.
- Upper byte (THx) holds the value to reload into TLx after an overflow.
- TFx must be manually cleared.
- Maximum count is 256
- Maximum interval is 256 Microseconds or .256 milliseconds
Timers
Mode 3 - Split Timer
- Splits Timer 0 into two 8-bit timers
- TL0 sets TF0
- TH0 sets TF1
- Timer 1 is available for other 3 modes, but the TF1 is not available.

 11.0592MHz is the operating frequency of 8051 microcontroller


 1 machine cycle has 12 clock cycle
 Duration of 1 MC = (1/11.059)*12 = 1.085 micro Second
Timer Mode-1
The working of mode-1 taking timer 0 into consideration:
• 16-bit timer (TH0 and TL0)
• TH0-TL0 is incremented continuously when TR0 is set to 1. And the 8051 stops to
increment TH0-TL0 when TR0 is cleared.
• The timer works with the internal system clock. In other words, the timer counts up
each machine cycle.
• When the timer (TH0-TL0) reaches its maximum of FFFFH, it rolls over to 0000, and TF0
is raised.
• Programmer should check TF0 and stop the timer 0.
Timer Mode-1
Steps of Mode 1
1. Choose mode 1 timer 0
MOV TMOD,#01H
2. Set the original value to TH0 and TL0.
MOV TH0,#FFH
MOV TL0,#FCH
3. Its better to clear the TF: TF0=0.
CLR TF0
4. Start the timer.
SETB TR0
Timer Mode-1

• The 8051 starts to count up by incrementing the TH0-TL0.


• TH0-TL0= FFFCH, FFFDH, FFFEH, FFFFH, 0000H
Timer Mode-1
• 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)

• Keep monitoring the timer flag (TF) to see if it is raised.

AGAIN: JNB TF0, AGAIN

• Clear TR0 to stop the process.

CLR TR0

• Clear the TF flag for the next round.

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)

• TF (timer flag, control flag)


• TF0 for timer/counter 0; TF1 for timer/counter 1.
• TF is like a carry. Originally, TF=0. When TH-TL roll over to 0000 from FFFFH, the TF is set to 1.
• TF=0 : not reach
• TF=1: reach
• If we enable interrupt, TF=1 will trigger ISR.
TCON (Timer Control) Instructions
• For timer 0 • For timer 1
• SETB TF1 = SETB TCON.7 • CLR TF0 = CLR TCON.5

• CLR TR1 = CLR TCON.6 • SETB TF0 = SETB TCON.5

• SETB TR1 = SETB TCON.6 • CLR TR0 = CLR TCON.4

• CLR TF0 = CLR TCON.5 • SETB TR0 = SETB TCON.4


Timers
Timer Delay and Timer Reload Value

• Timer Delay = Delay Value × Timer Clock Cycle Duration

• Delay Value = how many counts before register(s) roll over

• Timer Clock Cycle Duration = 6/oscillator frequency

• Delay Value = Maximum Register Count – Timer Reload Value

• Maximum Register Count = 65535


Steps for generating precise Delay using 8051 Timers
• Divide the time delay with timer clock period.
• N = time delay/1.085 μs

• Subtract the resultant value from 65536 (= 2^16, 16 bit).


• MMMM = 65536 - N

• Convert the difference value to the hexa-decimal form.


• MMMM d = XXYY h

• Load this value to the timer register.


• TH=XXh
• TL=YYh
Steps for generating precise Delay using 8051 Timers
In order to generate a delay of 1ms, TMOD = 0x01; // Timer 0 Mode 1

the steps are as follows. TH0= 0xFC; //initial value for 1ms

• NNNN = 1ms/1.085μs ≈ 922. TL0 = 0x66;

TR0 = 1; // timer start


• MMMM = 65536-922 = 64614
while (TF0 == 0); // check overflow condition
• 64614 in Hexadecimal = FC66h
TR0 = 0; // Stop Timer

• Load TH with 0xFC and TL with TF0 = 0; // Clear flag

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.

• A Microcontroller can serve various devices.

There are two ways to do that:


1. Interrupts
2. Polling.

• The program which is associated with the interrupt is called the


interrupt service routine (ISR) or interrupt handler.
Steps in executing an interrupt
• Upon receiving the interrupt signal the Microcontroller , finish current
instruction and saves the PC on stack.

• Jumps to a fixed location in memory depending on type of interrupt

• 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.

• Get pop PC from stack


Interrupt Sources
 Original 8051 has 6 sources of interrupts:
• Reset
• Timer 0 overflow
• Timer 1 overflow
• External Interrupt 0
• External Interrupt 1
• Serial Port events (buffer full, buffer empty, etc)

Enhanced version has 22 sources


• More timers, programmable counter array, ADC, more external interrupts, another serial port
(UART)
Interrupt Vectors
 Each interrupt has a specific place in code memory where program
execution (interrupt service routine) begins.
• External Interrupt 0: 0003h

• Timer 0 overflow: 000Bh

• External Interrupt 1: 0013h

• Timer 1 overflow: 001Bh

• Serial : 0023h

• Timer 2 overflow(8052+) 002bh


IE (Interrupt Enable)
• The IE or Interrupt Enable Register is used to enable or disable
individual interrupts.
• If a bit is SET, the corresponding interrupt is enabled and if the bit is
cleared, the interrupt is disabled.
• The Bit7 of the IE register i.e. EA bit is used to enable or disable all the
interrupts.
IE (Interrupt Enable)
• EA : Global enable/disable.
• --- : Undefined.
• ET2 : Enable Timer 2 interrupt.
• ES : Enable Serial port interrupt.
• ET1 : Enable Timer 1 interrupt.
• EX1 :Enable External 1 interrupt.
• ET0 : Enable Timer 0 interrupt.
• EX0 : Enable External 0 interrupt.
Enabling and disabling an interrupt
• By bit operation
• Recommended in the middle of program
• SETB EA ; Enable All SETB IE.7
• SETB ET0 ; Enable Timer0 ovrf SETB IE.1
• SETB ET1 ; Enable Timer1 ovrf SETB IE.3
• SETB EX0 ; Enable INT0 SETB IE.0
• SETB EX1 ; Enable INT1 SETB IE.2
• SETB ES ; Enable Serial port SETB IE.4
• By Mov instruction
• Recommended in the first of program
MOV IE, #10010110B
IP (Interrupt Priority)
• The IP or Interrupt Priority Register is used to set the priority of the
interrupt as High or Low.
• If a bit is CLEARED, the corresponding interrupt is assigned low
priority and if the bit is SET, the interrupt is assigned high priority.
Interrupt Priority Register
• 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 Priority
• The interrupt with the highest PRIORITY gets serviced first.

• All interrupts have a power on default priority order.


1. External interrupt 0 (INT0)
2. Timer interrupt0 (TF0)
3. External interrupt 1 (INT1)
4. Timer interrupt1 (TF1)
5. Serial communication (RI+TI)

• Priority can also be set to “high” or “low” by IP reg.


Interrupt Priority
• MOV IP , #00000100B or SETB IP.2 gives priority order
1. Int1
2. Int0
3. Timer0
4. Timer1
5. Serial
• MOV IP , #00001100B gives priority order
1. Int1
2. Timer1
3. Int0
4. Timer0
5. Serial
Serial Communications
Serial Communications
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
Peripheral Control Registers: SCON (Serial Control)
• The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s Serial
Port.
• It is located as an address of 98H.
• Using SCON, we can control the Operation Modes of the Serial Port, Baud Rate of the
Serial Port and Send or Receive Data using Serial Port.
• SCON Register also consists of bits that are automatically SET when a byte of data is
transmitted or received.
Peripheral Control Registers: SCON (Serial Control)
Serial Port Mode Control Bits
SM0 SM1 Mode Description Baud Rate

8-Bit Synchronous Shift Fixed Baud Rate


0 0 0
Register Mode (Frequency of oscillator / 12)

8-bit Standard UART Variable Baud Rate (Can be set


0 1 1
mode by Timer 1)
Fixed Baud Rate
9-bit Multiprocessor
1 0 2 (Frequency of oscillator / 32 or
Comm. mode
Frequency of oscillator / 64

9-bit Multiprocessor Variable Baud Rate (Can be set


1 1 3
Comm. mode by Timer 1)
SCON (Serial Control)
• REN (Receive Enable) also referred as SCON.4.

• 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.

• This can be achieved as below:


SETB SCON.4
CLR SCON.4
SCON (Serial Control)
• TI (Transmit interrupt) is the D1 bit of SCON register.

• 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.

• RI (Receive interrupt) is the D0 bit of the SCON register.

• 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

You might also like