Experiment 7

AIM: Write a Program to generate a square wave of 10 kHz using Timer 1 in mode 1(using 8051)

Objective:

The objective of this experiment is to:

  1. Understand the difference between Timer and Counter operation
  2. To Understand and analyze the use of different SFRs registers e.g. TMOD, TCON, IE
  3. To calculate the value of count to be set in TH and TL SFR
  4. To develop the algorithm for generating the square wave pulse
  5. To develop the program to generate the square wave pulse.

System Requirement:

8051 microcontroller Kit, CRO/DSO

Setting up the System:

  1. Ensure that standard IBM PC/AT system keyboard is connected
  2. System is Powered ON
  3. Student has a handy opcode table for programming
  4. CRO Connected to Port pin 2.3

Theory

The 8051 has two timers: Timer 0 and Timer 1. Both are 16-bit and can work as a timer or counter. The selection can be made by C/T’ pin; if this pin is grounded, it acts as timers. If this pin is connected to high, it is used as counter.

  • Timer: In timer mode, it is used to provide hardware delay.

  • Counter Mode: In counter mode, it is used to count the events at the external input.

Figure 7.1: Timer Control

Figure-1 shows that internally the clock frequency is divided by 12 (because 8051 take 12 clock pulse to complete one machine cycle.  So if 12MHz crystal is attached as the clock to 8051, internally only 12MHz/12 = 1MHz goes to the clock circuit.

The clock input to the timers They can be used either as timers or as event counters. 8051 use the following registers for the setting the operation of the timers/counters.

  • T1/0 Pin is an external input when used as counter
  • Gate pin decides whether external control is required for time/counter operation
  • INT0’ is an interrupt
  • TR0/TR1 is used to start/stop the timer/counter

SFR for timer/counter Operation

8051 uses the following SFR for its operation, these are:

  • Timer Mode (TMOD) Register
  • Timer Control (TCON) Register
  • IE (Interrupt Enable ) Register
  • 16-bit Count Register (TH1/TH0, TL1/TL0)
  • These are explained below:

TMOD Register: This SFR is an 8-bit register, the upper and the lower nibble of this register is used to set varios mode of operation for both timers. Upper nibble is used for Timer-1 and lower nibble sets the Timer-0 mode. Figure 2 shows the configuration of TMOD register and it is to be noted that this register is not bit addressable, it is byte addressable

Figure 7.2 TMOD Register

Input Function
GATE Gate Control Input. When set (Gate=’1’), then when C/T’=’1’, counter operation is controlled external events and not by TR bit.

When Gate is cleared ie. =’0’, then, timer/counter is controlled (start/stop) by software instruction When TR is set the timer starts and when clear timer stops. The instructions are SETB TR1/TR0  AND CLRB TR1/TR0

C/T’ When ‘1’ act as counter, when ‘0’ act as timer
M1M0 M1   M0    Mode

0         0     Mode-0, 13-bit, TH used as 8-bit timer and TL as 5-bit prescalar

0         1     Mode-1, 16-bit timer/counter

1         0     Mode-2, 8-bit Auto reload

1         1     Mode-3, split mode

Timer Control (TCON) Register (SFR Address- 88h)

This is the timer control register. It is a bit addressable; individual bit of this registers can be set or cleared by accessing the bits by their name or their address. The register configuration is shown in figure-3

Figure 7.3: TCON Register

Bit Function
TF1 Timer-1 Overflow Flag, Set when counter from FFFF to 0000h. Its ISR is at 001Bh, TF1 cleared, when processors executes the ISR from address 001Bh
TR1 This bit is used to start / stop the timer-1. Start when set, stops when cleared.
TF0 Timer-0 Overflow Flag, Set when counter from FFFF to 0000h. Its ISR is at 000Bh, TF0 cleared, when processors executes the ISR from address 000Bh
TR0 This bit is used to start / stop the timer-0. Start when set, stops when cleared.
IE1 Interrupt1 edge flag, set by hardware when interrupt on INT1 pin occurred and cleared by hardware when an interrupt is processed . It is external input, its ISR is located at 0013h
IT1 This bit selects external interrupt event type on INT1 pin. ‘1’ sets interrupt on falling edge, and ‘0’ to set on low level
IE0 Interrupt0 edge flag, set by hardware when interrupt on INT0 pin occurred and cleared by hardware when an interrupt is processed. It is external input, its ISR is located at 0003h
IT0 This bit selects external interrupt event type on INT0 pin. ‘1’ sets interrupt on falling edge, and ‘0’ to set on low level

 

Timer Count Register

8051 has two 16-bit count register each on for Timer-1 and timer-0. As 8051 is a 8-bit microcontroller two 8-bit registers are used for 16-bit count. These registers are TH1/0 and TL1/0.

Figure 7.4: Timer Count Register

Figure 7.5: Counter TH and TL

Calculation of Count Value

Assuming XTAL = 12 MHz, write a program to generate a square wave of 1KHz frequency on pin P2.5.

  • Internally the 8051 divides the frequency by 12, So internally clock frequency = 12MHz/12= 1MHz
  • Time of each tick(Clock Pulse)= 1/1M = 1micro seconds
  • Frequency Required: 1KHz
  • Time for each pulse = 1/1K = 1milli seconds
  • As the duty cycle for square wave is 50%, so, ON time=OFF time= 1ms/2 = 500 micro seconds
  • Value of Count = clock tick time / required time

= 500 micro seconds / 1 micro seconds = 500

  • Value to be set in counter = 65535 – 500 = 65035

= FE0Bh

  • Therefore : TH = FEh;     TL = 0Bh

Flow Chart (For Timer-0 Mode-1)

Figure 7.6: Flowchart of timer/counter

Algorithm

  1. Initialize the Mode of the timer
  2. Enable Interrupt bit
  3. Set the count value in TH and TL
  4. Start the timer by setting corresponding TR bit
  5. Is Counter Rolls Over
    1. Program will automatically jump to ISR at address 001B or 000Bh depending on timer-1 or timer-0 was selected.
    2. And execute the ISR instruction
      1. Stop the timer (instruction CLRB TR1/TR0)
      2. Complement the bit to get square wave affect.
  • Return from Interrupt to jump to start address of main program

Explanation of the Code

 

Directive /Label
ORG 1000h
LJMP Main
ORG 000Bh ; ISR for TF0
CPL P2.1 ; complement
CLR TR0 ;Stop timer
RETI ; return
ORG 0100h ;main program
MOV IE, #82h Enable interrupt
MOV TMOD, #01H Set timer-0 mode-1
Again: MOV TL0,#0Ch Set TL0
MOV TH0, #0FEh Set TH0
SETB TR0 ;Start timer-0
SJMP Again ; set count again

 

 

Execution:

 

Connect the probe crocodile clip of CRO to port pin 2.1

Execute the program

Read the pulse shown on CRO

Execute the program

Viva Questions:

Q1. Name the SFR register used for timer/counter operation

Answer: Following SFR are used for timer/counter operation

  1. Timer Mode (TMOD) Register
  2. Timer Control (TCON) Register
  3. Interrupt Enable Register
  4. TH1/TH0 and TL1/TL0 register

Q2. What is the function of TMOD register

TMOD register : upper and lower nibble of this register controls the Timer-1 and Timer-0 respectively. The function of the four bits in each nibble is given in table:

  1. Gate, C/T’, M1, M0. Gate is used to set external control for counter, When no Gate is required, TR bit of TCON is used to start/stop the timer.
  2. C/T’ Used to Enable the Timer orcounter
  3. M1, M0 these two bit are used to set the different mode of operation of Timers. These modes are Mode-0: 13bit; Mode-1: 16bit rollover; Mode-2:: 8-bit auto reload; Mode-3 : Split mode.

  Q.3 : For each of the following crystal input to 8051, find the value of the count to be set in counter TH1 and TL1 to generate 50Hz square wave pulse (i) 12MHz (ii) 11.0592MHz

Answer:

Look at the following steps.

Freq Required 1KHz. T = 1/F = 1/50 = 20ms

For Square wave ON time = OFF time = ½* 20ms= 10ms

  • Crystal frequency = 12MHz, Internal Freq =12MHz/12=1MHz

Time of each tick = T=1/F = 1/1M = 1microseconds

Count required= 10ms  / 1 micro = 10000

Value to be set in TH and TL = 65535+1 -10000 = 55536

55536= D8F0; So, TH =D8    and   TL = F0

  • Clock Freq= 0592 MHz

Internal Freq= 11.0592M/12 = 921.6KHz

Time of each pulse = 1/F 1/(921.6×1000) = 1.085 us

Count = 10/1.085 = 9216

Count = 65536-9216 = 56320 in decimal

=      DC00h, So,  TH = DC,   TL =00

 

Q4: For a XTAL of 11.0592 MHz connected to 8051 controller, what value is required to be loaded into the timer’s registers to have a time delay of 5 ms?

Answer:

XTAL Frequency = 11.0592 MHz

8051 take 12 clocks for a machine cycle o count. Therefore internal frequency = 11.0592/12 = 921.6KHz

Time for counting, the counter counts up = 1/921.6K = 1.085 us.

N of clocks required to count 5ms pulse = 5 ms / 1.085 us = 4608 clocks.

Value to be loaded in Timer register

= 65536 – 4608 = 60928 = EEOOH.

Therefore, we load          TH = EE and TL =00

 

Q5: For 8051 controller operating at external XTAL frequency of 11.0592MHz, write a program to generate 50Hz square pulse on pin 2.3 using programmed i/o method

Ans: In programmed the TF flag is continuously monitored by the controller under program control.

T=1 / 50 Hz = 20 ms, the period of the square wave

=1/2 * 20ms = 10 ms

Count required for 10ms out of 1.085us =10 ms / 1.085 us = 9216 Count to be loaded =   65536 – 9216 = 56320 in decimal

Count in Hex = DCOOH. Therefore, TL = 00 and TH = DC (hex)

MOV TMOD, #10h

Again:      MOV TL1, #00h               ; set TL Count

MOV TH1, #0DCh   ; set TH Count

SETB TR1

Back:       JNB TF1, Back        ; Monitor flag TF1, count up until TF1 set

CLR TR1         ; stop timer

CPL P2.3         ; complement port pin 2.3

CLR TR1         ; clear overflow flag

SJMP Again    ; repeat

Q6: For the following program, find the time delay in generated by the time. Assume the XTAL frequency of 8051 is 11.0592MHz

            MOV TMOD, #10h

Again: MOV TH1, #C0h

MOV TL1,#05h

SETB TR1

Back : JNB TF1, Back

CLR TR1

CLR TF1

SJMP Again

 

            MOV TMOD, #10h

MOV R3, #200

Again: MOV TH1, #C0h

MOV TL1,#05h

SETB TR1

Back : JNB TF1, Back

CLR TR1

CLR TF1

DJNZ R3 Again

 

Ans:

THTL = C005 = 49157

Delay = count * time of pulse

=49157*1.085us

=53335.35us =0.053 sec

Ans:

Loop Counter = 200

THTL = C005 = 49157

Delay = count * time of pulse

=49157*1.085us

=53335.35us =0.53 sec

Total delay = 200* 0.053

= 10.66 seconds

 

 

 

Updated: April 19, 2019 — 11:01 am

Leave a Reply

Your email address will not be published. Required fields are marked *

care4you © 2014 care4you © Revision-1: 2016 care4you © Revision-2: 2019 Connect On Facebook DMCA.com Protection Status
error: Content is protected !!