---
title: "1 kHz square wave on P1.5 using timer"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-square-wave-timer"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-square-wave-timer
site: BCT Engineering
---

# 1 kHz square wave on P1.5 using timer

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2078 Chaitra
- Video: [8051 Square Wave Generation Using Timer | Complete Delay Calculation and assembly program](https://www.youtube.com/watch?v=JKEam3Rcij4) — Mahesh Pawaskar
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/8051-square-wave-timer
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Assume that XTAL = 11.0592 MHz, write a program to generate a square wave of 1 kHz frequency on pin P1.5 of 8051 microcontroller using timer.

## Short answer

With 11.0592 MHz, timer clock =921.6 kHz, half period 0.5 ms requires 461 counts. After compensating for instruction overhead, load TH0=FEH, TL0=3EH. Toggle P1.5 on TF0 overflow, producing approximately 1.002 kHz.

## Answer

#### Calculation

XTAL = 11.0592 MHz, so the timer clock = 11.0592 MHz / 12 = 921.6 kHz and one machine cycle = 1.0851 us.

- Frequency 1 kHz gives T = 1 ms, so each half period (high or low) = 0.5 ms.
- Count needed = 500 us / 1.0851 us = 460.8, about 461 counts.
- Timer 0 in mode 1 (16-bit): initial value = 65536 - 461 = 65075 = FE33H.
- The loop instructions (reload, JNB, CLR, CPL, SJMP) add about 11 machine cycles per half period. To compensate, use 461 - 11 = 450 counts: 65536 - 450 = 65086 = FE3EH, so TH0 = FEH, TL0 = 3EH.

#### Program

```asm
        ORG 0000H
        MOV TMOD,#01H       ; Timer 0, mode 1 (16-bit timer)
AGAIN:  MOV TH0,#0FEH       ; 65536 - 450 = 65086 = FE3EH
        MOV TL0,#3EH
        SETB TR0            ; start Timer 0
WAIT:   JNB TF0,WAIT        ; wait until TF0 = 1 (overflow)
        CLR TR0             ; stop timer
        CLR TF0             ; clear overflow flag
        CPL P1.5            ; toggle the output pin
        SJMP AGAIN          ; repeat: half period 0.5 ms
        END
```

Each time Timer 0 overflows (TF0 = 1), P1.5 is complemented and the timer is reloaded, so P1.5 gives a square wave. In a simulator the half period measured 499.1 us, i.e. about 1.002 kHz.

#### C version

```c
#include <reg51.h>
sbit wave = P1^5;
void main(void)
{
    TMOD = 0x01;                 /* Timer 0, mode 1 */
    while (1) {
        TH0 = 0xFE;  TL0 = 0x3E; /* 65086 */
        TR0 = 1;
        while (TF0 == 0);        /* wait for overflow */
        TR0 = 0;  TF0 = 0;
        wave = ~wave;            /* toggle P1.5 */
    }
}
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. For a 1 kHz square wave, how long is the timer interval before each toggle?
   A. 1 ms
   B. 0.5 ms
   C. 2 ms
   D. 0.25 ms
   Answer: B. Period is 1 ms, so each high or low half is 0.5 ms. (Key point: Half period)

2. With XTAL = 11.0592 MHz, what is the timer clock?
   A. 11.0592 MHz
   B. 1.3824 MHz
   C. 921.6 kHz
   D. 46 kHz
   Answer: C. The timer clock is XTAL/12 = 921.6 kHz, one cycle = 1.0851 us. (Key point: Machine cycle)

3. How is the Timer 0 mode 1 initial value obtained?
   A. Count x 256
   B. 65536 minus the number of counts needed
   C. Counts needed directly
   D. XTAL / 12
   Answer: B. The timer counts up to overflow at 65536, so initial = 65536 - counts (here 450 = FE3EH). (Key point: Reload value)

4. What produces the square wave on P1.5?
   A. CPL P1.5 each time TF0 shows an overflow
   B. SETB P1.5 once
   C. MOV TMOD,#01H
   D. Timer 0 directly drives the pin
   Answer: A. Each overflow the program stops the timer, clears TF0 and complements P1.5, then reloads. (Key point: Producing the wave)

## Related in Embedded System

- [8051 pin diagram; ports 1 and 2](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-pin-diagram-ports-1-2) — Embedded System, chapter 9
- [Microcontroller vs microprocessor; 8051 features](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Amicro-vs-microprocessor-8051-features) — Embedded System, chapter 9, asked 2×
- [Microprocessor vs microcontroller; 8051 addressing](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-addressing-modes-micro-vs-processor) — Embedded System, chapter 9
- [8051 minimum circuit and 75% duty pulse](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-min-config-pwm-75) — Embedded System, chapter 9
- [Precise 5 ms delay in 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-5ms-delay) — Embedded System, chapter 9
- [Addressing modes of 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-addressing-modes) — Embedded System, chapter 9, asked 4×

## Related in other subjects

- [DAC square wave generation at 1 kHz](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Adac-square-wave-1khz) — Instrumentation II, chapter 4
- [Generate FM (50 MHz, β = 1) from NBFM](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Anbfm-to-fm-50mhz) — Communication Systems, chapter 3
- [FM numerical: 102.4 MHz carrier, 75 kHz deviation](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Afm-102-4mhz-75khz) — Communication Systems, chapter 3
- [FM numerical: 10 kHz baseband, 100 MHz carrier, 75 kHz deviation](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Afm-10khz-100mhz-75khz) — Communication Systems, chapter 3
