You are on page 1of 48

# Module 4 Machine cycle

TIMER Timer0 & Timer1 - TMOD SFR - mode0,1, 2, 3TCON SFR Programming examples.

Machine Cycle
CPU executing an instruction takes a certain number of clock cycles referred as machine cycles The length of machine cycle depends on the frequency of the crystal oscillator connected to 8051 Frequency of crystal may vary from 4MHz to 30MHz Very often 11.0592MHz crystal oscillator is used to make 8051 based system compatible with serial port of IBM PC One machine cycle lasts 12 oscillator periods To calculate machine cycles, 1/12 of crystal frequency is taken and with its inverse

## where C is the number of machine cycles

For 12 MHz crystal and for 1 machine cycle, Tinst = 1s. Generally we use 11.0592MHz crystal frequency.
Tinst = (1*12)/11.0592MHz = 1.085 s

To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one

Find the period of the machine cycle for a)11.0592 MHz b)16MHz c) 20MHz crystal frequency
Solution: a) 11.0592/12 = 921.6 kHz; machine cycle is 1/921.6 kHz = 1.085s

b) 0.75s

c) 0.60s

For 8051 system of 11.0592 MHz, find how long it takes to execute each instruction. (a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target (d) LJMP (e) SJMP (f) NOP (g) MUL AB Solution: Machine cycles Time to execute (a) 1 1x 1.085s 1.085s (b) 1 1x 1.085s 1.085s (c) 2 2x 1.085s 2.17s (d) 2 2x 1.085s 2.17s (e) 2 2x 1.085s 2.17s (f) 1 1x 1.085s 1.085s (g) 4 4x 1.085s 4.34s

Find the size of the delay in following program, if the crystal frequency is 22MHz.

## MOV A,#55H MOV R0,#40H ADD @R0,A INC R0 MOV @R0,A

1 1 1 1 1

Find the size of the delay in following program, if the crystal frequency is 11.0592MHz. Machine Cycle DELAY: MOV R3,#250 1 HERE: NOP NOP NOP NOP DJNZ R3,HERE RET 1 1 1 1 2 2

Solution: The time delay inside HERE loop is [250(1+1+1+1+2)]x1.085s 1627.5s. Adding the two instructions outside loop we have 1627.5s + 3 x 1.085s 1630.755s

TIMER

Timers are used to generate time delay or can be used as Event counters to count events happening outside the microcontroller

Timers are for timing operations which measure the time between the events Counters are for event counting which is used to count the no. of occurrences of an event Timers are also used to provide clock

TIMERS Timer 0
Mode 0 Mode 1 Mode 2 Mode 3

Timer 1
Mode 0 Mode 1 Mode 2

## TIMERS SFR TMOD & TCON

Both timers use the same register, called TMOD (timer mode), to select timer/counter operation. Byte addressable TMOD is a 8-bit register
The lower 4 bits are for Timer 0 The upper 4 bits are for Timer 1

In each case,
The lower 2 bits are used to set the timer mode The upper 2 bits to specify the operation

## SFRs Related to TIMER TMOD

Gate C/ M1 M0 Gate C/ M1 M0

Timer 1

Timer 0

TCON
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

Timers

Interrupt

TMOD Register:

Gate :
- If 1, the timer will start running only when INTx is high & TRx is set - If 0, the timer will start running when TRx is set (no need for INTx to be high)

C/T(bar) : Counter/Timer select bit. T=0 used as timer M1 : Mode select bit 1. M0 : Mode select bit 0.

The timer/counter is started or enabled using TRx bit of TCON register. Setting TRx bit starts the timer. Clearing TRx bit stops the timer. SETB TR1 CLR TR0

## When GATE-1 &INT-1 &TRx-1, starts When GATE-0 &TRx-1, starts

TRx=0, stops

TCON Register:

TF1: Timer 1 overflow flag. TR1: Timer 1 run control bit.(set by program) TF0: Timer 0 overflag. TR0: Timer 0 run control bit. IE1: External interrupt 1 edge flag. IT1: External interrupt 1 type flag. IE0: External interrupt 0 edge flag. IT0: External interrupt 0 type flag.

TF1: set when timer rolls from all1s to 0s. Cleared when processor vectors to execute ISR located at address 001Bh. TF0: set when timer rolls from all1s to 0s. Cleared when processor vectors to execute ISR located at address 000Bh. IE1/IE0: set when a low to high edge signal is received on INT1/INT0 pin. Cleared when processor vectors to ISR located at address 0013h/0003h. IT1/IT0: set to 1 by program to enable external interrupt 1 to be triggered by a falling edge signal. set to 0 by program to enable a low level signal on external interrupt 1 to generate an interrupt.

There are two timers Timer 0 and Timer 1 which are 16 bit wide 16-bits timer is accessed as two separate registers of low byte and high byte The low byte register is called TL0/TL1 and high byte register is called TH0/TH1 which is accessed like any other register Eg: MOV TL0,#4FH MOV R5,TH0

Eg: If a 16 bit value of say FFBAh is to be given to the timer for a particular delay, we give it as TH1=FFh TL1=BAh The instructions are MOV TH1,#FFh MOV TL1,#BAh

Indicate which mode and which timer are selected for each of the following. (a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H

Solution: (a) TMOD = 00000001, mode 1 of timer 0 is selected. (b) TMOD = 00100000, mode 2 of timer 1 is selected. (c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.

timer needs a clock pulse to tick if C/T = 0, the crystal frequency attached to the 8051 is the source of the clock for the timer frequency for the timer is always 1/12th the frequency of the crystal attached to the 805 During timing operations the timer registers (THx/TLx) are incremented at a clock rate of 1/12th the frequency of oscillator

In counting operations, the no of events is determined by reading TLx/THx registers as its value s incremented for each event. These registers are incremented when a 1 to 0 transition occurs at external input pin (T0 or T1 pin) When the pin is high in one cycle and low in next cycle, 2 machine cycles are required , so the clock rate is 1/24th of oscillator frequency

## Mode 1 Programming 16 bit (0000-FFFFH)

Timer is started Continues till it reaches the limit FFFFH And it rolls over to 0000H To restart the timer reload TH and TL and Make TF=0

## TF=1 Timer Stops

Mode 1 Operation
1. It is a 16 bit timer, whose value from 0000H to FFFFH is loaded into the timers register TL and TH 2. Once TH and TL are loaded, the timer will be started by SETB TR0 for timer 0 and SETB TR1 for timer 1 3. After the timer is started, it starts to count up & continues until it reaches its limit of FFFFH
When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag) & this timer flag can be monitored When this timer flag is raised, we would be able to stop the timer with the instructions CLR TR0 or CLR TR1

4. After the timer reaches its limit and rolls over, in order to repeat the process TH and TL must be reloaded with the original value, and TF must be reloaded to 0

## Steps to program MODE 1

1. Load the TMOD register 2. Load registers TL and TH with initial count value 3. Start the timer

MOV TMOD, #01 MOV TL0, #F2H MOV TH0,#FFH SETB TR0
L1: JNB TF0, L1

4. Keep monitoring the timer flag (TF) with the JNB TFx,target instruction to see if it is raised 5. Get out of the loop when TF becomes high 6. Stop the timer 7. Clear the TF flag for the next round 8. Go back to Step 2 to load TH and TL again

RET

## Calculation of the values to be loaded into the TL and TH registers

Assuming XTAL = 11.0592 MHz, following steps are used for finding the TH, TL registers values 1. Divide the desired time delay by 1.085 s 2. Perform 65536 n, where n is the decimal value we got in Step1 3. Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded into the timers register 4. Set TL = xx and TH = yy

Assume that XTAL = 11.0592 MHz. What value do we need to load the timers register if we want to have a time delay of 5 ms ? Show the program for timer 0 to create a pulse width of 5 ms on P2.3.

1. Since XTAL = 11.0592 MHz, the counter counts up every 1.085 s. 2. To get that a time delay of 5 ms,
5 ms / 1.085s = 4608 clocks.

## To Achieve TL and TH value

65536 4608 = EE00H.

## 1. Therefore, we have TH = EE and TL = 00.

AGAIN:SETB P2.3 CALL DELAY CLR P2.3 CALL DELAY SJMP AGAIN DELAY:MOV TMOD,#01H MOV TL0,#00 MOV TH0,#0EEH SETB TR0 BACK: JNB TF0,BACK CLR P2.3 CLR TR0 CLR TF0 RET

Assume XTAL = 11.0592 MHz, write a program to generate a square wave of 50 Hz frequency on pin P2.3.With 50% duty cycle (Ton = Toff). Select timer 1, mode1.

1. T = 1 / 50 = 20 ms, the period of square wave. 2. 1 / 2 of it for the high and low portion of the pulse is 10 ms. 3. 10 ms / 1.085 us = 9216 and 65536 9216 = 56320 in decimal, and in hex it is DC00H. 4. TL = 00 and TH = DC (hex).

AGAIN:

## CLR P2.3 MOV TMOD,#10H MOV TL1,#00 MOV TH1,#0DCH

BACK:

SETB TR1 JNB TF1,BACK CLR TR1 CPL P2.3 CLR TF1 SJMP AGAIN

;Timer 1, mod 1 ;TL1=00,low byte of timer ;TH1=DC, the high byte ;Start timer 1 ;until timer rolls over ;Stop the timer 1 ;Comp. p2.3 to get hi,lo

Assume that XTAL = 11.0592 MHz, write a program to generate a square wave of 2 kHz frequency on pin P1.5.

(a) T = 1 / f = 1 / 2 kHz = 500 us the period of square wave. (b) 1 / 2 of it for the high and low portion of the pulse is 250 us. (c) 250 us / 1.085 us = 230 and 65536 230 = 65306 which in hex is FF1AH. (d) TL = 1A and TH = FF, all in hex.

MOV TMOD,#01 ;Timer 0, 16-bitmode AGAIN: MOV TL1,#1AH ;TL1=1A, low byte of timer MOV TH1,#0FFH ;TH1=FF, the high byte SETB TR1 ;Start timer 1 BACK: JNB TF1,BACK ;until timer rolls over CLR TR1 ;Stop the timer 1 CPL P1.5 CLR TF1 ;Clear timer 1 flag SJMP AGAIN ;Reload timer

1.Generate a square wave with an ON time of 3ms and an OFF time of 10ms on all pins of P0. XTAL =16MHz.timer 0 and mode1 is selected.
2. Generate a square wave with an 60% DUTY CYCLE on all pins of P2. XTAL =16MHz.timer 0 and mode1 is selected.

Mode 0 Programming
Mode 1 is exactly like Mode 0, except that it is a 13 bit timer instead of 16 bit timer. 13 bit counter can hold values between 0000 to 1FFFH When timer reaches its maximum value it rolls over and TF is raised.

## Mode 2 Programming 8 BIT

TH is loaded and its value is Stored in TL also

Mode 2 Programming
1. It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timers register TH 2. After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL Then the timer must be started This is done by the instruction SETB TR0 for timer 0 and SETB TR1 for timer 1 3. After the timer is started, it starts to count up by incrementing the TL register It counts up until it reaches its limit of FFH When it rolls over from FFH to 00, it sets high the TF (timer flag)

4. TL is reloaded automatically with the original value kept by the TH register 5. To repeat the process, we must simply clear TF and let it go without any need by the programmer to reload the original value 6. This makes mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to reload TH and TL

## To generate a time delay

1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used, and the timer mode (mode 2) is selected 2. Load the TH registers with the initial count value 3. Start timer 4. Keep monitoring the timer flag (TF) with the JNB TFx,target instruction to see whether it is raised Get out of the loop when TF goes high 5. Clear the TF flag 6. Go back to Step4, since mode 2 is auto reload

## Timer 0 in Mode 3 (Split Timer)

Mode 3 configures registers TL0 and TH0 to operate as separate 8-bit timers. Ie) 16-bit timer consisting of two registers TH0 and TL0 is split into two independent 8-bit timers. This mode is provided for applications requiring an additional 8-bit timer or counter.

The TL0 timer turns into timer 0, while the TH0 timer turns into timer 1. In addition, all the control bits of 16-bit Timer 1 (consisting of the TH1 and TL1 register), now control the 8-bit Timer 1. Even though the 16-bit Timer 1 can still be configured to operate in any of modes (mode 1, 2 or 3), it is no longer possible to disable it as there is no control bit to do it. Thus, its operation is restricted when timer 0 is in mode 3. The only application of this mode is when two timers are used and the 16-bit Timer 1 the operation of which is out of control is used as a baud rate generator.