; ------------------------------------------------------------------------
;
; Title:
;
;   PG40 -- 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 or experimenting, each pulse to pin5/GP2
;   steps the 5-bit (32 step) OSCTUNE 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  pG  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 12F683.
;
; Version:
;
;   07-Apr-2014  Tom Van Baak (tvb)  www.LeapSecond.com/pic
;
; ------------------------------------------------------------------------

; Microchip MPLAB IDE assembler code (mpasm).

        list        p=pic12f683
        include     p12f683.inc
        __config    _INTRC_OSC_CLKOUT & _MCLRE_OFF & _FCMEN_OFF & _WDT_OFF

        ; _INTRC_OSC_CLKOUT -- use internal RC oscillator
        ; _MCLRE_OFF        -- make GP3 an input pin rather than master clear
        ; _FCMEN_OFF        -- no need for failsafe clock monitor
        ; _WDT_OFF          -- no need for watchdog timer

; Register definitions.

        cblock  0x20            ; define register base
            gpcopy              ; shadow of output pins
            dig6, dig5, dig4, dig3, dig2, dig1, dig0
            osc                 ; value for OSCTUNE
        endc

; Define entry points.

        org     0               ; power-on entry
        goto    init            ;
        org     4               ; interrupt entry
        goto    step            ;

; If interrupt occurs, adjust 5-bit OSCTUNE value up or down by one.

step:
        movlw   1               ; increment by one
        btfss   GPIO,GP3        ;
          movlw -1              ; decrement by one
        addwf   osc,F           ;
        bcf     INTCON,INTF     ; reset interrupt flag
        goto    loop            ; goto, not return

; PIC 12F683 power-on hardware initialization.

init:
        errorlevel -302
        bsf     STATUS,RP0      ; bank 1
        clrf    ANSEL           ; all digital (no analog) pins
        movlw   1<<INTEDG       ; WPU(NOT_GPPU=0), GP2/INT rising edge trigger
        movwf   OPTION_REG      ;
        movlw   1<<GP2
        movwf   WPU             ; activate weak pullup input pin(s)
        movlw   ~(1<<GP5 | 1<<GP4 | 1<<GP1 | 1<<GP0)
        movwf   TRISIO          ; set pin directions (0=output)
        movlw   b'01100001'     ; 4 MHz HFINTOSC, SCS
        movwf   OSCCON          ; configure internal oscillator

        errorlevel +302
        bcf     STATUS,RP0      ; bank 0
        movlw   0x07
        movwf   CMCON0          ; disable comparator
        call    clear           ; initialize counter and pins
        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

        ; Update counter and map each output pin to decade LSB.

        call    tune            ; set oscillator rate
        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'132'          ;
        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

; Set RC oscillator rate.

tune:   movf    osc,W           ;
        bsf     STATUS,RP0      ; bank 1
        errorlevel -302
        movwf   OSCTUNE         ; W -> OSCTUNE
        errorlevel +302
        bcf     STATUS,RP0      ; bank 0
        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                   ;
          bsf   INTCON,GIE      ; [re]enable interrupts (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
