PIC16F876 flashing problem

Thread Starter


Joined Apr 12, 2009
guys i dont know what going on with my pic. i am using pic16f876 with 4Mhz crystal oscillator, 22pF capacitor. i have written a flash code to test the output and it seems its not working for me. here is my code below.i am trying to measure the output voltage to be +5v. i have put Mclr to +5v.


org 0x000
goto Start
org 0x006
movlw 0x00
movwf TRISB
movwf TRISA
movwf TRISC
movlw 0x0F
movwf PORTA
movlw b'00000000'
movwf PORTB
movlw b'00000111'
movwf PORTC
clrf PORTB
movlw b'11111111'
movwf PORTB
;bsf PORTB
goto Main

END ; directive 'end of program'


Joined May 11, 2009
What is your setup. If you use say a Pickit programmer and let this unit also supply power during runtime. You must check both that power is turned on, and that the MCLR pin is not set to low level.
Verify the registers in your equates section are correct. I've spent an hour or two banging my head against the keyboard before only to discover I equated things like "PORTB" to the wrong register.


Joined Sep 7, 2009
PORTB, etc are all defined in the include file:
If you have the include line all is fine.
PORTB is flashing far too fast for the eye to see. Any LED connected to it should just look on.

Rich (BB code):
    list      p=16f876            ; list directive to define processor
    #include <p16f876.inc>        ; processor specific variable definitions

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

w_temp        EQU     0x7E        ; variable used for context saving 
status_temp   EQU     0x7F        ; variable used for context saving

        ORG     0x000             ; processor reset vector
        clrf    PCLATH            ; ensure page bits are cleared
          goto    main              ; go to beginning of program

        ORG     0x004             ; interrupt vector location
        movwf   w_temp            ; save off current W register contents
        movf    STATUS,w          ; move status register into W register
        movwf    status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere

        movf    status_temp,w     ; retrieve copy of STATUS register
        movwf    STATUS            ; restore pre-isr STATUS register contents
        swapf   w_temp,f
        swapf   w_temp,w          ; restore pre-isr W register contents
        retfie                    ; return from interrupt


; remaining code goes here

        END                       ; directive 'end of program'


Joined Nov 28, 2008
Your code is switching the port pins high/low repeatedly.

Is this what you have wanted in the first place? Or do you want to check the pin level to be steady?

Rich (BB code):
;*************************************************  ********
       clrf            PORTB
       movlw           b'11111111'
       movwf           PORTB
       ;bsf            PORTB
       goto            Main     ; // use "goto $" if you want steady pin level


Joined May 11, 2009
This code should work for chip. Unless your name is Clark Kent you will never see any blinking. Because the switch rate is way to high.
Rich (BB code):
list   p=16f876 
#include <p16F876.inc>
    cblock 0x20
Delay1                   ; Define two file registers for the
Delay2                   ; delay loop
     org 0
     bsf       STATUS,RP0          ; select Register Page 1
     movlw 0
     tris PORTC                ;test instr
;bcf       TRISC,0             ; make IO Pin B.0 an output
     bcf       STATUS,RP0          ; back to Register Page 0
     bsf       PORTC,0             ; turn on LED C0
     decfsz    Delay1,f            ; Waste time.  
     goto      OndelayLoop         ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions
     decfsz    Delay2,f            ; The outer loop takes and additional 3 instructions per lap * 256 loops
     goto      OndelayLoop         ; (768+3) * 256 = 197376 instructions / 1M instructions per second = 0.197 sec.
                                   ; call it a two-tenths of a second.
     bcf       PORTC,0             ; Turn off LED C0
     decfsz    Delay1,f            ; same delay as above
     goto      OffDelayLoop
     decfsz    Delay2,f
     goto      OffDelayLoop
     goto      MainLoop            ; Do it again...

Thread Starter


Joined Apr 12, 2009
guys i have solve the problem. the thing was the breadboard threads of which are loose and the crystal oscillator was floating. even though i have spent a lot of time trying to fix the problem. that is before you suspect your code try to use a continuity tester to track the voltage path, it will help you.