This program generates a square wave on pin P1.3 using a software delay loop.

Description

The program creates a square wave by repeatedly toggling P1.3 with a delay between each toggle. This method uses software loops for timing.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
ORG 0000H
	
	CLR P1.3
	BACK:
	SETB P1.3
	ACALL DELAY
	ACALL DELAY
	
	CLR P1.3
	ACALL DELAY
	
	SJMP BACK
	
	DELAY: MOV R1 , #200
	HERE: DJNZ R1 , HERE
	RET
	
END

Explanation

Main Loop

  1. CLR P1.3: Initialize output pin to 0
  2. SETB P1.3: Set pin high (rising edge)
  3. ACALL DELAY (2 times): Keep pin high for 2 delay periods
  4. CLR P1.3: Set pin low (falling edge)
  5. ACALL DELAY: Keep pin low for 1 delay period
  6. SJMP BACK: Repeat

Delay Subroutine

  1. MOV R1, #200: Load counter with 200
  2. DJNZ R1, HERE: Decrement and loop until R1 = 0
    • Creates a delay of 200 × 2 machine cycles
  3. RET: Return to caller

Waveform Characteristics

  • High time: 2 × delay = 2 × 200 × 2 cycles
  • Low time: 1 × delay = 1 × 200 × 2 cycles
  • Duty cycle: 66.67% (high for 2/3 of period)

Timing Analysis

With 11.0592 MHz crystal:

  • Machine cycle = 1.085 µs
  • Single delay = 200 × 2 × 1.085 µs = 434 µs
  • High time = 2 × 434 µs = 868 µs
  • Low time = 1 × 434 µs = 434 µs
  • Period = 868 + 434 = 1302 µs
  • Frequency ≈ 768 Hz

Symmetric Square Wave

For 50% duty cycle, modify the main loop:

1
2
3
4
5
6
BACK:
    SETB P1.3
    ACALL DELAY
    CLR P1.3
    ACALL DELAY
    SJMP BACK

Adjusting Frequency

  • Increase R1 value → Lower frequency
  • Decrease R1 value → Higher frequency
  • For more precise timing, use Timer-based approach