This program generates a square wave on pin P1.5 using Timer 0 for precise timing.

Description

The program uses Timer 0 in mode 1 (16-bit timer) to create accurate delays between pin toggles, generating a square wave output.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
ORG 0000H
	
	MOV TMOD , #00000001B;
	CLR P1.5
	
	HERE:
	MOV TL0, #0FFH
	MOV TH0 , #00H
	CPL P1.5
	ACALL DELAY
	SJMP HERE
	
	DELAY:
	SETB TR0
	NEXT: JNB TF0, NEXT
	CLR TF0
	RET
	
END

Explanation

Timer Configuration

  1. MOV TMOD, #00000001B: Configure Timer 0 in mode 1
    • 16-bit timer mode
    • No counter mode
  2. CLR P1.5: Initialize output pin to 0

Main Loop

  1. MOV TL0, #0FFH: Load timer low byte
  2. MOV TH0, #00H: Load timer high byte
    • Initial count = 00FFH = 255
  3. CPL P1.5: Toggle the output pin
  4. ACALL DELAY: Wait for timer overflow
  5. SJMP HERE: Repeat

Delay Subroutine

  1. SETB TR0: Start Timer 0
  2. JNB TF0, NEXT: Wait for timer overflow flag
    • TF0 = 1 when timer overflows from FFFF to 0000
  3. CLR TF0: Clear overflow flag
  4. RET: Return

Frequency Calculation

With 11.0592 MHz crystal:

  • Machine cycle = 12/11.0592 MHz = 1.085 µs
  • Timer counts from 00FFH to FFFFH = 65535 - 255 = 65280 counts
  • Time per half cycle = 65280 × 1.085 µs ≈ 70.8 ms
  • Full period = 141.6 ms
  • Frequency ≈ 7.06 Hz

To change frequency, modify TH0 and TL0 values:

  • Lower initial value → Longer delay → Lower frequency
  • Higher initial value → Shorter delay → Higher frequency

Example: For higher frequency

1
2
3
MOV TL0, #00H
MOV TH0, #0FCH  ; Count from FC00H to FFFFH (1024 counts)
; Frequency  450 Hz