LED flash program for 16F873

Function
This program makes a LED flash at 2 Hz.

Hardware
The testboard K3 is set up with reset circuit, crystal oscillator at 4 MHz and a LED.

Note: RA4 (pin 6) has an open drain output. If RA4 is going to be used, the LED and 470 ohm resistor must be connected between pin 6 and +5V.


Testboard K3 configured for 873flash.asm

Software in assembler

;**********************************************************
; Processor: PIC16F873 at 4 MHz
; Function: Flash a LED connected to RA0
; Hardware: Testboard K3a
; Filename: 873flash.asm
; Author: Lars Petersen, oz1bxm@pobox.com
; Credit: Tony Nixon's LED flasher
;**********************************************************

        LIST P=16F873, R=DEC    ; Use the PIC16F873 and decimal system

        #include "P16F873.INC"  ; Include header file

        __config  _CP_OFF & _XT_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF & _BODEN_ON

        CBLOCK 0x20             ; Declare variable addresses
          Loop1,Loop2
        ENDC
;
; -----------
; INITIALIZE
; -----------
;
        ORG    0x000           ; Program starts at 0x000
;
        CLRF   PORTA           ; Initialize port A
        CLRF   PORTB           ; Initialize port B
        CLRF   PORTC           ; Initialize port C
;
        BSF    STATUS,RP0      ; RAM bank 1
;
        CLRF   TRISA           ; All pins port A output
        CLRF   TRISB           ; All pins port B output
        CLRF   TRISC           ; All pins port C output
;
; ------------------------
; FUNCTION OF PORT A PINS
; ------------------------
;
        MOVLW    6
        MOVWF    ADCON1        ; All pins digital I/O
;
        BCF      STATUS,RP0    ; RAM bank 0
;
; ----------
; MAIN LOOP
; ----------
;
Main    BSF     PORTA,0        ; Turn on LED connected to RA0
        CALL    delay
        BCF     PORTA,0        ; Turn off LED connected to RA0
        CALL    delay
        GOTO    Main
;
; ---------------
; DELAY 250 MSEC
; ---------------
;
delay   MOVLW   250
        MOVWF   Loop1
Outer   MOVLW   200
        MOVWF   Loop2
Inner   NOP
        NOP
        DECFSZ  Loop2,F
        GOTO    Inner          ; Inner loop = 5 usec.
        DECFSZ  Loop1,F
        GOTO    Outer
        RETURN

        END

Remarks
I had initial troubles with the above circuit. The first version of the software apparently worked, but the oscillation stopped when I touched the 16F873 with my finger! The problem was caused by incorrect setting of the configuration fuse LVP. The fuse LVP is default on. LVP must be set off using the __config directive or in the programmer.

Do not forget to set the ADCON1 register, which must be loaded with value 6 or 7 for all port A pins to function as digital I/O.

All ports can be tested by substituting the main loop with the following lines:
;
; ----------
; MAIN LOOP
; ----------
; Apply 2 Hz to all ports
Main    MOVLW b'01010101'
        MOVWF portA
        MOVWF portB
        MOVWF portC
        CALL  delay
        MOVLW b'10101010'
        MOVWF portA
        MOVWF portB
        MOVWF portC
        CALL  delay
        GOTO  Main

Download
Download 873flash.zip containing the source code in assembler and the hexfile.

Credit
Tony Nixon's LED flasher at http://www.piclist.com/techref/microchip/16F877/flashled.htm inspired me. Thanks!



This page created November 18th, 2001 by Lars Petersen, oz1bxm@pobox.com
Revised September 28th, 2002.

Back to PIC experiments