//Instance file di adc.h : adc.c
//Libreria per gestione della scheda 8 channel adc
//Andrea Cipriani Ago 2004
//a71cip@tiscali.it



void AdcChannelInit (unsigned int datadd)
//Inizializza per la scheda adc 8 channel
//datadd = DATA address
{
 unsigned int data_address     =datadd;
 unsigned int status_address   =datadd+1;
 unsigned int control_address  =datadd+2;

 //Channel address = 000
 ControlClearBit (control_address,1);
 ControlClearBit (control_address,2);
 ControlClearBit (control_address,3);

 //Write = 0
 DataClearBit (data_address,0);

 //Read = 0
 DataClearBit (data_address,1);
}



char AdcChannelRead	(unsigned int datadd, char ch)
//Converte il canale prescelto sulla scheda adc 8 channel e restituisce una char
//datadd = DATA address, ch = canale da convertire (0..7)

{
 unsigned int data_address     =datadd;
 unsigned int status_address   =datadd+1;
 unsigned int control_address  =datadd+2;
 char channel=ch;
 char last_cnt = ControlIn(control_address);
 unsigned char low_nibble,high_nibble,overflow,read;

 //Imposta il canale prescelto sulle apposite linee di decodifica
 channel = channel << 1;
 channel = channel & 0xfe;
 ControlOut(control_address,channel);
 sleep(1);

 //Start of conversion : Write
 DataSetBit (data_address,0);
 sleep(1);
 DataClearBit (data_address,0);

 //Lettura del valore convertito

 // Output enable
 DataSetBit (data_address,1);


 //Strobe a 0 : read lower nibble
 ControlClearBit (control_address,0);
 low_nibble = StatusIn(status_address);

 low_nibble = low_nibble & 0xbf;

 if (low_nibble & 0x80)
  {
   overflow = 0x80;
  }
  else
  {
   overflow = 0x00;
  }

 low_nibble = low_nibble<<1;

 low_nibble = low_nibble & 0x7f;

 low_nibble = low_nibble | overflow;

 low_nibble = low_nibble>>4;
 low_nibble = low_nibble & 0x0f;

//Strobe a 1 : read upper nibble
 ControlSetBit (control_address,0);
 high_nibble = StatusIn(status_address);
 high_nibble = high_nibble & 0xbf;

 if (high_nibble & 0x80)
  {
   overflow = 0x80;
  }
  else
  {
   overflow = 0x00;
  }
 high_nibble = high_nibble<<1;
 high_nibble = high_nibble & 0x7f;

 high_nibble = high_nibble | overflow;
 high_nibble = high_nibble & 0xf0;

 read = high_nibble | low_nibble;

 //Output disable
 DataClearBit (data_address,1);

 //Control port resume
 ControlOut(control_address,last_cnt);
 return(read);
}

float AdcVoltage (unsigned char readed,float vref)
// Restituisce un float con la tensione letta
// unsigned char = valore passato da AdcChannelRead
// vref = Vref del circuito
{
 return (readed * vref)/256;
}


float AdcTemperatureLM35D (unsigned char readed, float vref)
// Restituisce un float con la temperatura letta
// unsigned char = valore passato da AdcChannelRead
// vref = Vref del circuito
{
 return (readed * vref)/12.8;
}

float AdcTemperatureAD590 (unsigned char readed, float vref)
// Restituisce un float con la temperatura letta con sensore ad590
// unsigned char = valore passato da AdcChannelRead
// float = Vref
{
 return ((readed * vref)*0.16)-55;
}

