/****************************************************************************/
/* Copyright 2004 MBARI.                                                    */
/* Monterey Bay Aquarium Research Institute Proprietary Information.        */
/* All rights reserved.                                                     */
/****************************************************************************/
#if defined(__PCM__)
#include <16f873.h>
#fuses XT,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=3580000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use fast_io(C)
#endif

#include <stdlib.h>
#include <string.h>
#include "freq_gen.h"

/********************************** GLOBS ***********************************/

long rtccTicks = 0;
int freqDuration = 0;
int freqEnabled = TRUE;
#byte portC = 0x07

/********************************** GLOBS ***********************************/

#INT_RTCC
rtccIsr()
{
    if ( !freqEnabled )
    {
        rtccTicks = 0;
        /* stay in process commands */
        freqDuration = 3;
        return;
    }
    
    rtccTicks++;
    
    if ( FREQ_DURATION == rtccTicks )
    {
        rtccTicks = 0;
        ++freqDuration;
    }
}

void processCmds()
{
    int c;

    if ( kbhit() )
    {
        c = getc();

        
        switch ( toupper(c))
        {
            case '1':   freqEnabled = TRUE;
                        printf("freq gen enabled\r\n");
                        break;
            case '2':   freqEnabled = FALSE;
                        printf("freq gen disabled\r\n");
                        break;
            case '?':   printf("1 - enable freq gen\r\n");
                        printf("2 - disable freq gen\r\n");
                        printf("? - help menu\r\n");
                        break;
        }
    }

    return;
}

main()
{
    /* setup port c as all outputs */
    set_tris_c(0x00);
    
    /* init freq pins */
    portC = PORTC_CLEAR_ALL;
    
    /* setup the system clock */
    setup_counters(RTCC_INTERNAL, RTCC_DIV_64);

    /* make sure interrupts are enabled */
    enable_interrupts(INT_RTCC);
    enable_interrupts(GLOBAL);

    /* main loop */
    for(;;)
    {
        /* 100 Hz */
        while ( freqDuration == 0 )
        {
            /* pin hi */
            portC = PORTC_100HZ_FREQ_HI;
            delay_ms(5);
            /* pin lo */
            portC = PORTC_100HZ_FREQ_LO;
            delay_ms(5);
        }

        /* 300 Hz */
        while ( freqDuration == 1 )
        {
            /* pin hi */
            portC = PORTC_100HZ_FREQ_HI;
//            portC = PORTC_300HZ_FREQ_HI;
            delay_us(1580);
            /* pin lo */
            portC = PORTC_100HZ_FREQ_LO;
//            portC = PORTC_300HZ_FREQ_LO;
            delay_us(1572);
        }

        /* 1000 Hz */
        while ( freqDuration == 2 )
        {
            /* pin hi */
            portC = PORTC_100HZ_FREQ_HI;
//            portC = PORTC_1000HZ_FREQ_HI;
            delay_us(500);
            /* pin lo */
            portC = PORTC_100HZ_FREQ_LO;
//            portC = PORTC_1000HZ_FREQ_LO;
            delay_us(490);
        }

        /* clear all the pins */
        portC = PORTC_CLEAR_ALL;
        
        /* process any queued cmds */
        while ( freqDuration != 6 )
            processCmds();
        
        /* do it again ! */
        freqDuration = 0;
    }
}
