; ------------------------------------------------------------------------
;
; Title:
;
;   PD22 -- PIC "4-pin" 3 MHz to 1 Hz frequency divider
;
; Function:
;
;   This PIC program implements a digital frequency divider: the external
;   input clock is divided by a factor of 3000000.
;
;                               ---o---
;              (Vdd) +5 V  ++++|1     8|====  ground (Vss)
;             input clock  >---|2 12F 7|--->  output clock
;                             *|3 6xx 6|*
;                             -|4     5|-
;                               -------
; Notes:
;
;   Only 4 pins are required: power (2-5V), ground, input and output.
;   Output frequency accuracy is the same as clock input accuracy.
;   * For added flexibility GP1(pin6) and GP4(pin3) match GP0(pin7).
;   Output drive current is 25 mA maximum per pin.
;   Coded for PIC 12F675 but any '609 '615 '629 '635 '675 '683 works.
;
; Version:
;
;   10-Oct-2012  Tom Van Baak (tvb)  http://www.LeapSecond.com/pic
;
; ------------------------------------------------------------------------

;
; Microchip MPLAB IDE assembler code (mpasm).
;
        list        p=pic12f675
        include     "p12f675.inc"
        __config    _EC_OSC & _MCLRE_OFF & _WDT_OFF
;
; Register definitions.
;
        cblock 0x20
            gpcopy              ; shadow of output pins
        endc
;
; One-time PIC 12F675 initialization.
;
        org     0               ; power-on entry here
        bcf     STATUS,RP0      ; bank 0
        clrf    GPIO            ; set all pins low
        movlw   07h             ; set mode to turn
        movwf   CMCON           ;   comparator off
        bsf     STATUS,RP0      ; bank 1
        clrf    ANSEL-0x80      ; set digital IO (no analog A/D)
        movlw   b'101100'       ; set GP0,GP1,GP4 as output(0) and
        movwf   TRISIO-0x80     ;   other pins are input(1)
        bcf     STATUS,RP0      ; bank 0
        clrf    gpcopy          ; initialize shadow output
;
; With an external 3 MHz clock (and 4:1 PIC execution ratio), a
; total of 375,000 instructions takes exactly 1/2 second per loop.
; Output pins are toggled once per loop creating a 1 Hz square wave.
;
Loop:   movlw   0xFF            ; (      1) -1
        xorwf   gpcopy,F        ; (      1) toggle bits
        movf    gpcopy,W        ; (      1) gpcopy -> W
        movwf   GPIO            ; (      1) W -> output pin(s)

        movlw   d'37'           ; (      1)
        call    DelayW10k       ; ( 370000) delay W*10000
        movlw   d'49'           ; (      1)
        call    DelayW100       ; (   4900) delay W*100
        movlw   d'91'           ; (      1)
        call    DelayW1         ; (     91) delay (15 <= W <= 255)
        goto    Loop            ; (      2)

        include "delayw.asm"
        end
