On this page
Square Wave Generation using Software Delay
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
| |
Explanation
Main Loop
- CLR P1.3: Initialize output pin to 0
- SETB P1.3: Set pin high (rising edge)
- ACALL DELAY (2 times): Keep pin high for 2 delay periods
- CLR P1.3: Set pin low (falling edge)
- ACALL DELAY: Keep pin low for 1 delay period
- SJMP BACK: Repeat
Delay Subroutine
- MOV R1, #200: Load counter with 200
- DJNZ R1, HERE: Decrement and loop until R1 = 0
- Creates a delay of 200 × 2 machine cycles
- 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:
| |
Adjusting Frequency
- Increase R1 value → Lower frequency
- Decrease R1 value → Higher frequency
- For more precise timing, use Timer-based approach