/*
Diplomova prace : Palubni pocitac

adc.c

Atmega128A 10 MHz XTAL

Autor: Ondrej Lutera

2011/2012

*/
#include "adc.h"

uint8_t curr_chnl=NO_CH;

inline void initAD(void)
{

    ADCSRA|= 1<<ADEN | 1<< ADPS2 | 1<<ADPS0;

}

inline uint8_t StartAD(uint8_t chan)
{

    if(chan == NO_CH) return chan;
    if(curr_chnl!=NO_CH) return AD_BUSY;

    while(bit_is_set(ADCSRA,ADSC));     /*  pockej pokud bezi prevod */

    ADMUX = INT_REF | chan;

    ADCSRA|= 1<<ADSC;


    curr_chnl = chan;

    return chan;
}

inline uint16_t ReadAD(uint8_t chan)
{


    if(curr_chnl == chan)
    {

        while(bit_is_clear(ADCSRA,ADIF));

        curr_chnl = NO_CH;

        return ADC;
    }
    else return BAD_CH;

}
