;************************************************************************
;   Filename:       pic12c508_ctcss_03.asm                              *
;   Date:           2022-01-05                                          *
;   File Version:   3.0                                                 *
;                                                                       *
;   Author:     Harry Lythall, SM0VPO,      http://www.sm0vpo.com       *
;                                                                       *
;               harry.lythall@sm0vpo.com    +46736295002                *
;               All rights reserved                                     *
;************************************************************************
;   Architecture:   Baseline PIC                                        *
;   Processor:      12C508 / 12F509                                     *
;                                                                       *
;************************************************************************
;   Description: 123Hz/77Hz CTCSS/AFSK generatorator (programable)      *
;                                                                       *
;   Pinout:     Pin 1   Vdd +5 V DC                                     *
;               Pin 4   GP3 (tone select) High (N/C) = Del6             *
;               Pin 7   GP0 (tone out)                                  *
;               Pin 8   Vss  Ground -ve                                 *
;                                                                       *
;   Vcc +5V to Pin 2 and program Del6 (77Hz) for the required tone      *
;   Ground Pin 2 to select Del5 as secondary tone (123Hz)               *
;                                                                       *
;   Output requires LP filter and high resistor to rig microphone       *
;                                                                       *
;************************************************************************
;  Timing values for tones:                                             *
;     Scale = 1 + Int(165450 / (freq * 256))                            *
;     Count = 165450 / (freq * 256) - Scale * 255 + 6                   *
;                                                                       *
;     Example: 77.0Hz                                                   *
;     Scale = 165450 / (77.0 * 256) = 8.393364 ; Integer = 8 + 1 = 9    *
;     Count = 0.393364 * 255 +6 = 155 +6 = 163.44 (164 is exact value)  *
;                                                                       *
;     Scale = .9      Count = .164                                      *
;************************************************************************

    list        p=12C508      
    #include    <p12C508.inc>

                ; ext reset, no code protect, no watchdog, 4Mhz int clock
    __CONFIG    _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC


;***** VARIABLE DEFINITIONS
        UDATA
dc1     res 1               ; Count loop counter value
dc2     res 1               ; Scale loop counter value

;***** OSCCAL AND PORT CONFIGURATION
RESET   CODE    0x000       ; effective reset vector
        movwf   OSCCAL      ; update OSCCAL with factory cal value 

start   movlw   b'111110'   ; configure GP0 as output
        tris    GPIO

;***** MAIN PROGRAM ****************************

;**************************** Switch GP0=ON
Main    movlw	b'000001'   ; set w to 1 in bit0
        movwf	GPIO        ; set GP0 high

;**************************** Test 77/123 select and wait
        btfsc   GPIO,3      ; Test GP3 - skip if 0
        call    Del5        ; GOSUB for 123Hz
        btfss   GPIO,3      ; Test GP3 - skip if 1
        call    Del6        ; GOSUB for 77Hz

;**************************** Switch GP0=OFF
        movlw   b'000000'   ; set w to 0 in bit0
        movwf   GPIO        ; set GP0 low

;**************************** Test 77/123 select and wait
        btfsc   GPIO,3      ; Test GP3 - skip if 0
        call    Del5        ; GOSUB for 123Hz
        btfss   GPIO,3      ; Test GP3 - skip if 1
        call    Del6        ; GOSUB for 77Hz

;**************************** Start again
        goto    Main


;***** SUBROUTINES ****************************

;***** Delay loop for 123Hz
Del5    movlw   .060        ; Seed Count Lo (Count value)
        movwf   dc1         ; Store it
        movlw   .006        ; Count Hi (Scale value)
        movwf   dc2         ; Store it

Del5a   decfsz  dc1         ; Decrement Lo
        goto    Del5a       ; Repeat if not zero
        decfsz  dc2         ; Decrement Hi, skip if zero
        goto    Del5b       ; Reseed Lo and start again
        goto    Del5c       ; Both zero so return

Del5b   movlw   .000        ; Re-seed Count-Lo register
        movwf   dc1         ; Store it
        goto    Del5a       ; Continue counting

Del5c   retlw   0           ; Done

;***** Delay loop for 77Hz
Del6    movlw   .094        ; Seed Count Lo (Count value)
        movwf   dc1         ; Store it
        movlw   .009        ; Count Hi (Scale value)
        movwf   dc2         ; Store it

Del6a   decfsz  dc1         ; Decrement Lo
        goto    Del6a       ; Repeat if not zero
        decfsz  dc2         ; Decrement Hi, skip if zero
        goto    Del6b       ; Reseed Lo and start again
        goto    Del6c       ; Both zero so return

Del6b   movlw   .000        ; Re-seed Count-Lo register
        movwf   dc1         ; Store it
        goto    Del6a       ; Continue counting

Del6c   retlw   0           ; Done

        END
