; ------------------------------------------------------------------------
;
; Title:
;
;   PD40 -- Free-running RC clock with 1 MHz, 100 Hz, 1 Hz, 10 s outputs
;
; Function:
;
;   This PIC program implements a low accuracy, low stability, frequency
;   generator with four different square wave outputs.
;
;   To facilitate frequency tuning, calibration, or experimenting, each
;   pulse to pin5/GP2 steps the 6-bit (64 step) OSCCAL register by one.
;   The step is increment if pin4/GP3 is high or decrement if low.
;
; Diagram:
;                                ---__---
;                5V (Vdd)  +++++|1      8|=====  Ground (Vss)
;                1 Hz out  <----|2  pD  7|---->  100 Hz out
;               1 MHz out  <----|3  40  6|---->  10 s out
;                 up/down  o--->|4      5|<+---  step
;                                --------
; Notes:
;
;   o Tie or pull pin4/GP3 to Vdd/Vss (do not leave open).
;   Output drive current is 25 mA maximum per pin.
;   Coded for Microchip 12F675.
;
; Version:
;
;   05-Apr-2014  Tom Van Baak (tvb)  www.LeapSecond.com/pic
;
; ------------------------------------------------------------------------

; Microchip MPLAB IDE assembler code (mpasm).

        list        p=pic12f675
        include     p12f675.inc
        __config    _INTRC_OSC_CLKOUT & _MCLRE_OFF & _WDT_OFF

; Register definitions.

        cblock  0x20            ; define register base
            gpcopy              ; shadow of output pins
            dig6, dig5, dig4, dig3, dig2, dig1, dig0
            osc                 ; value for OSCCAL
        endc

; Define entry points.

        org     0               ; power-on entry
        goto    init            ;
        org     4               ; interrupt entry
        goto    step            ;

; If interrupt occurs, adjust 6-bit OSCCAL value up or down by one.

step:   bcf     STATUS,RP0      ; force bank 0
        movlw   4               ; increment by one (GP3 high)
        btfss   GPIO,GP3        ;   or
          movlw -4              ; decrement by one (GP3 low)
        addwf   osc,F           ;
        bcf     INTCON,INTF     ; reset interrupt flag
        goto    loop            ; goto, not return

; One-time PIC 12F675 initialization.

init:   bcf     STATUS,RP0      ; bank 0
        movlw   0x07            ; turn comparator off
        movwf   CMCON           ;
        call    clear           ; initialize counter and pins

        bsf     STATUS,RP0      ; bank 1
        errorlevel -302
        clrf    ANSEL           ; all digital (no analog) pins
        movlw   ~(1<<GP5 | 1<<GP4 | 1<<GP1 | 1<<GP0)
        movwf   TRISIO          ; set pin directions (0=output)
        movlw   1<<GP2
        movwf   WPU             ; enable weak pullup (1=enable)
        movlw   1<<INTEDG       ; WPU, GP2/INT rising edge trigger
        movwf   OPTION_REG      ;
        errorlevel +302
        bcf     STATUS,RP0      ; bank 0

        call    0x3FF           ; use default calibration value
        movwf   osc             ;   until stepping begins

        movlw   1<<GIE | 1<<INTE | 0<<INTF
        movwf   INTCON          ; enable GP2 edge-trigger interrupt

; To create multiple frequency outputs the PIC increments a virtual
; '7490-style decade counter chain in a continuous isochronous loop.
; Clocking the counter at twice the output rate allows each LSB to
; generate a square wave at the desired decade frequency.
;
; A 500 us (2 kHz) toggle loop can generate a 1 kHz square wave.
; With a 4 MHz clock (1 us Tcy) 500 instructions is 500 us.

loop:   movf    gpcopy,W        ; gpcopy -> W
        movwf   GPIO            ; W -> GPIO

        ; Set RC oscillator rate.

        movf    osc,W           ;
        bsf     STATUS,RP0      ; bank 1
        errorlevel -302
        movwf   OSCCAL          ; W -> OSCCAL
        errorlevel +302
        bcf     STATUS,RP0      ; bank 0

        ; Update counter and map each output pin to decade LSB.

        call    count           ; increment counter
        clrf    gpcopy          ;
        btfss   dig1,0          ; 100 Hz decade LSB
          bsf   gpcopy,GP0      ;
        btfss   dig4,0          ; 10 s decade LSB
          bsf   gpcopy,GP1      ;
        btfss   dig3,0          ; 1 Hz decade LSB
          bsf   gpcopy,GP5      ;

        ; Pad loop for exactly 500 instructions (use MPLAB SIM).

        movlw   d'3'            ;
        call    DelayW100       ; delay W*100
        movlw   d'136'          ;
        call    DelayW1         ; delay (15 <= W <= 255)
        goto    loop            ;

; Initialize counter (zero) and output pins (high).

clear:  clrf    dig0            ; 1000 Hz
        clrf    dig1            ;  100 Hz
        clrf    dig2            ;   10 Hz
        clrf    dig3            ;    1 Hz
        clrf    dig4            ;   10 s
        clrf    dig5            ;  100 s
        clrf    dig6            ; 1000 s
        movlw   1<<GP5 | 1<<GP1 | 1<<GP0
        movwf   gpcopy
        clrf    osc             ; default rate
        return

; Increment 7-digit decimal counter (isochronous code).

count:  incf    dig0,F          ; always increment LSDigit
        movlw   d'10'           ;
        subwf   dig0,W          ; check overflow
        skpnz                   ;
          clrf  dig0            ; reset to zero

        skpnz                   ;
          incf  dig1,F          ; apply previous carry
        movlw   d'10'           ;
        subwf   dig1,W          ; check overflow
        skpnz                   ;
          clrf  dig1            ; reset to zero

        skpnz                   ;
          incf  dig2,F          ; apply previous carry
        movlw   d'10'           ;
        subwf   dig2,W          ; check overflow
        skpnz                   ;
          clrf  dig2            ; reset to zero

        skpnz                   ; apply debounce logic
          bsf   INTCON,GIE      ; [re]enable interrupts only every 0.5 s

        skpnz                   ;
          incf  dig3,F          ; apply previous carry
        movlw   d'10'           ;
        subwf   dig3,W          ; check overflow
        skpnz                   ;
          clrf  dig3            ; reset to zero

        skpnz                   ;
          incf  dig4,F          ; apply previous carry
        movlw   d'10'           ;
        subwf   dig4,W          ; check overflow
        skpnz                   ;
          clrf  dig4            ; reset to zero

        skpnz                   ;
          incf  dig5,F          ; apply previous carry
        movlw   d'10'           ;
        subwf   dig5,W          ; check overflow
        skpnz                   ;
          clrf  dig5            ; reset to zero

        skpnz                   ;
          incf  dig6,F          ; apply previous carry
        movlw   d'10'           ;
        subwf   dig6,W          ; check overflow
        skpnz                   ;
          clrf  dig6            ; reset to zero

        return

        include delayw.asm      ; precise delay functions
        end
