/****************************************************************************
underground_locator_tx.c

This program is a transmitter for an underground locator that operates at 512Hz or 8192Hz.

Transformer coupled, modified sine wave

WORKING CODE


                    +5
                     |
                    14
                ----------
               |      RA0 |-17-- output 1
  MODE   ----7-| RB1      |
               |      RA1 |-18-- output 2
               |          |
               |  16F628  |-1--- LED
               |          |
               |      RB6 |-12-- PGD
               |      RB7 |-13-- PGC
 20MHz XTAL-15-|     MCLR |-4--- MCLR
       XTAL-16-|          |
                ----------
                     5
                     |
                    Gnd

***************************************************************************/

#include < 16F628a.h >
#fuses HS, NOPROTECT, PUT, NOWDT, BROWNOUT, NOMCLR, NOLVP
#include < jonsinc.h >

#use standard_io ( A )
#use standard_io ( B )
#use delay ( clock = 20000000 )
// one cycle = 0.2uS @ 20MHz

#define OUTPUT1   PIN_A0
#define OUTPUT2   PIN_A1
#define LED       PIN_A2
#define MODE_SW   PIN_B1

#define HIGH_FINE_ADJ  93
#define LOW_FINE_ADJ   80

void main ( void )
        {
        char cCnt, cLedFlag;

        delay_ms ( 100 );
        port_b_pullups ( ON );
        output_high ( LED );
        delay_ms ( 500 );       // wait for mode switch to stabilize after power-on
        output_low ( LED );
        delay_ms ( 500 );
        cCnt = 0;
        cLedFlag = 0;

        if ( input ( MODE_SW ) == HIGH )
            {
            while ( TRUE )
                {
                // 8192Hz = 122uS period
                output_low ( OUTPUT2 );
                delay_us ( 16 );
                output_high ( OUTPUT1 );
                delay_us ( 25 );
                delay_cycles ( HIGH_FINE_ADJ );
                output_low ( OUTPUT1 );
                delay_us ( 16 );
                output_high ( OUTPUT2 );
                delay_us ( 25 );
                delay_cycles ( HIGH_FINE_ADJ - 1 - 21 );
                if ( cCnt++ == 0 )
                    {
                    cLedFlag ^= 1;
                    }
                else
                    {
                    delay_cycles ( 4 );
                    }
                output_bit ( LED, cLedFlag );
                }
            }
        else
            {
            while ( TRUE )
                {
                // 512Hz = 1953uS period
                output_low ( OUTPUT2 );
                delay_us ( 380 );
                output_high ( OUTPUT1 );
                delay_us ( 580 );
                delay_cycles ( LOW_FINE_ADJ );
                output_low ( OUTPUT1 );
                delay_us ( 380 );
                output_high ( OUTPUT2 );
                delay_us ( 580 );
                delay_cycles ( LOW_FINE_ADJ - 1 - 21 );
                if ( cCnt++ == 0 )
                    {
                    cLedFlag ^= 1;
                    }
                else
                    {
                    delay_cycles ( 4 );
                    }
                output_bit ( LED, cLedFlag );
                }
            }
        }