/***
 * Little test program to check the adc functions
 * Copyright (c) 2003, Inventronik
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the Inventronik nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: adconv.c,v 1.1 2005/01/27 18:14:08 jensi Exp $ *
 */

#if defined(__PCM__)
#include <16f876.h>
#fuses XT, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOLVP
#elif defined(__PCH__)
#include <18f252.h>
#fuses EC, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOLVP
#endif

#zero_ram // erase all used ram cells at initialisation.
#device adc=10

#use delay(clock=6000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define MAX_PLUS_PLUS 10 
#define MAX_PLUS 5

/***
 * initialize adc 
 */
void init_adc(void)
{
    setup_adc_ports(ALL_ANALOG);
    setup_adc(ADC_CLOCK_INTERNAL);
}

/***
 * just a little test program to check the a/d converter on the
 * PICUSB. All (RA0-RA5) A/D converter inputs are usable.
 */
void main(void)
{
    int i=0;
    long adc;
    long adcs[5] = {0, 0, 0, 0, 0};
    char str[3];
    signed int diff;

    init_adc();
    printf("\r\n\Run A/D convertion in a loop ...\r\n");

    // The program will delay for 10 seconds and then display
    // any data that came in during the 10 second delay
    do {
        set_adc_channel(i);
        delay_ms(1000);
        adc = read_adc();
        if(adcs[i]) {
            diff = adc - adcs[i];
            if(diff<=MAX_PLUS && diff>=-MAX_PLUS)
                strcpy(str, "0");
            else if(diff<-MAX_PLUS && diff>=-MAX_PLUS_PLUS)
                strcpy(str, "-");
            else if(diff<-MAX_PLUS_PLUS)
                strcpy(str, "--");
            else if(diff>MAX_PLUS && diff<=MAX_PLUS_PLUS)
                strcpy(str, "+");
            else
                strcpy(str, "++");
        }
        adcs[i] = adc;
        printf("\r\nchannel (%d)=> %lx (%s: %d)", i, adc, str, diff);
        i = (i+1)%5;
   } while(TRUE);
}

