// Please first read ADC0832 datasheet
#define AD_CS pin_A0 // Chip Select
#define AD_CK pin_A1 // Clock
#define AD_DI pin_A2 // Data In (MUX Channel Selector)
#define AD_DO pin_A3 // Data Out
int adc_result; // Variable that stores the result of the conversion ( 8 bits ==> int type)
void delay() { // Delay rutine
delay_us(3);
}
void Clock() { //Signal Clock, to read or put data from/to ADC0832
output_high(AD_CK);
delay();
output_low(AD_CK);
}
// Set_adc(); ==> Rutine for set MUX word to send towards ADC0832 (measure mode and channel selected)
void set_adc(short ch) { //ch corresponds to the channel to measure, is a "short" data type. (one bit)
if(ch==0){
//ch=0. We select channel 0 of the ADC.
output_high(AD_CS); //It initiates transition to enable ADC.
delay(); //Necessary delay to not overcome 200 KHz of frequency of the ADC.
output_low(AD_CS); //It activates the line /CS (chip select) on the ADC to initiate conversion
delay(); //It must be like that during the whole conversion,
//Now the chip waits for a "Start bit" and the "MUX word setting" bits ( two bits)
output_high(AD_DI); //It puts in 1 the bit of game SAY of the MUX of selection of port
delay();
Clock(); //We put this start bit towards the MUX of the ADC0832
//The bit DI is put into MUX in every Rising Edge of the Clock
output_high(AD_DI); //DI=SGL/DIF=1 we select "Single-Ended MUX mode"
delay(); //We stay DI in high state (DI=1)
Clock(); //we put SGL/DIF into MUX of ADC0832
output_low(AD_DI); //With DI=ODD/SIGN=0 we select Channel 0.
delay();
Clock(); //With the latter clock it finishes the allocation of the channel to convert
}
else {
//ch=1. we select channel 1 of ADC
output_high(AD_CS); //It initiates transition to enable ADC
delay(); //Necessary delay to not overcome 200 KHz of frequency of the ADC.
output_low(AD_CS); //It activates the line /CS (chip select) on the ADC to initiate conversion
delay(); //It must be like that during the whole conversion,
//Now the chip waits for a "Start bit" and the "MUX word Setting" bits ( two bits)
output_high(AD_DI); //It puts in 1 the bit of game SAY of the MUX of selection of port
delay();
Clock(); //We put this start bit towards the MUX of the ADC0832
//The bit DI is put into MUX in every Rising Edge of the Clock
output_high(AD_DI); //With DI=SGL/DIF=1 we select "Single-Ended MUX mode"
delay(); //We stay DI in high state (DI=1)
Clock(); //we put SGL/DIF into MUX of ADC0832
output_high(AD_DI); //With DI=ODD/SIGN=1 we select Channel 1.
delay();
Clock(); //With the last clock it finishes the allocation of the channel to convert
}
}
// This function must be called after selecting the channel
// of the ADC0832 to be used for conversion.
// CS must be active (low)
// The ADC0832 delivers two stream out in DO:
// - MSB first
// - LSB first
// Therefore, we read the MSB firts, shift it to the left side of adc_result
// and as soon as the byte was completed, we reject the stream LSB and disable
// the ADC0832 doing CS=1 (high)
void read_adc() {
int i;
for (i=0;i<8;i++) { // Read the result buyte of ADC0832 conversion.
Clock(); // and store it into adc_result
shift_left(&adc_result, 1, input(AD_DO));
delay();
}
output_high(AD_CS); //After read the finished byte,disable ADC0832
//doing CS=1 (high).
//With this it finishes the reading of the ADC0832 out stream,
//and we can process the result in main.c
}