PIC16F877 Program for 1 KHz square Wave Generation: Here is the steps for PIC16F877 Program for Generating a square wave of 1 KHz .
Square Wave Generation using PIC16F877

1 kHz Square Wave with PIC16F877 (Assembly)
Assumptions:
• Oscillator Fosc=4 MHzF_{osc} = 4 \, \text{MHz}Fosc=4MHz → instruction cycle Tcy=1 μsT_{cy} = 1 \, \mu sTcy=1μs.
• We want 1 kHz square wave → Period = 1 ms → Half-period = 0.5 ms = 500 µs.
• Timer0 with prescaler 1:4 → tick = 4 μs4 \, \mu s4μs.
• Required ticks = 500/4=125500 / 4 = 125500/4=125.
• Preload value = 256−125=131(0x83)256 – 125 = 131 (0x83)256−125=131(0x83).
So → On every Timer0 overflow, we toggle the pin (RB0).
;===========================================
; PIC16F877 – 1 kHz square Wave, 50% duty
; Using Timer0 interrupt, output on RB0
; Fosc = 4 MHz
;===========================================
LIST P=16F877
INCLUDE <P16F877.INC>
__CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF
CBLOCK 0x20
; No variables needed
ENDC;——————————————-
ORG 0x00
GOTO MAIN
ORG 0x04 ; Interrupt vector
GOTO ISR;——————————————-
MAIN:
BSF STATUS, RP0 ; Bank1
CLRF TRISB ; PORTB as output
BCF STATUS, RP0 ; Back to Bank0
CLRF PORTB ; Clear PORTB
; Setup Timer0
MOVLW b'00000001' ; PSA=0 (assign prescaler), PS=001 (1:4), T0CS=0 (internal clk)
OPTION_REG = 0x81 ; Actually OPTION<2:0>=001 -> PS=1:4, bit PSA=0
; T0CS=0 (internal clock)
MOVLW 0x83 ; Preload value = 131
MOVWF TMR0
; Enable interrupts
BSF INTCON, T0IE ; Enable TMR0 overflow interrupt
BSF INTCON, GIE ; Global interrupt enableLOOP:
GOTO LOOP ; Main loop does nothing
;——————————————-
; Interrupt Service Routine
ISR:
BCF INTCON, T0IF ; Clear TMR0 interrupt flag
MOVLW 0x83 ; Reload TMR0 with 131
MOVWF TMR0
; Toggle RB0
MOVF PORTB, W ; Read PORTB
XORLW 0x01 ; Toggle bit0
MOVWF PORTB
RETFIE;——————————————-
END
- Timer0 is configured with prescaler 1:4.
- TMR0 is preloaded with 0x83 (131) so that after 125 ticks (500 µs), it overflows.
- On each overflow, the ISR reloads TMR0 and toggles RB0.
- Thus, RB0 goes HIGH for 500 µs, LOW for 500 µs → 1 kHz square wave, 50% duty cycle.
Join EEE Made Easy Telegram channel
Join EEE Made Easy Whatsapp Channel
Download EEE Made Easy Ebook PDF Free
Read Also
- Features of AVR|AVR Microcontrollers
- 8051 ARCHITECTURE|8051 Microcontroller Architecture
- 8051 Microcontroller Architecture|RISC and CISC CPU Architectures|HARVARD & VON- NEUMANN CPU Architecture
- Microprocessor VS Microcontroller| Comparison of Microprocessor and Microcontroller|Difference between Microprocessor and Microcontroller
- Addressing modes of 8051 Microcontroller|8051 Addressing Modes
- 8051 Microcontroller Instruction Set|KSEB Sub Engineer Notes
- 8051 Microcontroller MCQ|8051 Questions and Answers
- Microcontroller|Types of microcontroller
- 8051 Microcontroller Notes pdf|8051 microcontroller pdf
- Interrupts in 8051|Interrupts in 8051 Microcontroller
Latest Posts in EEE Made Easy
- Energy Efficient Lamps|CFL, LED lampsEnergy Efficient Lamps: CFL (Compact Fluorescent lamp ) and LED (light emitting diodes) lamp are Energy Efficient Lamps. Energy Efficient … Read more
- [New] AE KWA Kerala PSC 2026AE KWA Kerala PSC 2026: DETAILED SYLLABUS FOR SELECTION TO THE POST OF ASSISTANT ENGINEER IN KERALA WATER AUTHOERITY AE … Read more
- [New] Electricity Worker KSEB Syllabus 2026|021/2026 Syllabus Kerala PSCElectricity Worker KSEB Syllabus 2026- 021/2026 Syllabus Kerala PSC: DETAILED SYLLABUS FOR THE POST OF ELECTRICITY WORKER IN KERALASTATE ELECTRICITY … Read more
- [Latest]KWA Operator Syllabus 2026|734,735/2025 Kerala PSC syllabusKWA Operator Syllabus 2026 – 734,735/2025 Kerala PSC syllabus: KERALA PUBLIC SERVICE COMMISSIONDETAILED SYLLABUS FOR THE POST OF OPERATOR (DIRECT) … Read more
- [New]Inspector of Legal Metrology Syllabus|613/2025 Kerala PSC SyllabusInspector of Legal Metrology Syllabus– 613/2025 Kerala PSC Syllabus: DETAILED SYLLABUS FOR THE POST OF INSPECTOR OF LEGAL METROLOGY IN … Read more
- [PDF]IE Rules|Indian Electricity Rules 1956IE Rules:The Indian Electricity Rules 1956 was made under section 37 of the Indian Electricity Act, 1910 and redefined after … Read more
- [Latest]Syllabus Sub Engineer LSGD Electricity Wing Thrissur Corporation| 770/2025 Syllabus Kerala PSCSyllabus Sub Engineer LSGD Electricity Wing Thrissur Corporation: DETAILED SYLLABUS FOR THE POST OF SUB ENGINEER IN LSGD(ELECTRICITY WING OF … Read more

