I am using ATmega32-A microcontroller and CodeVisionAVR compiler. I am able to read values from an AD7798 external ADC successfully. I am also generating wave from with an AD9833 using SPI communication. I am passing the generated signal to a sensor as well as an ADG1408-EP analog multiplexer. I use the multiplexer to send either the waveform input to the sensor or the output of the sensor to the external ADC for conversion.
Here is a simplified schematic:
I have written the following code:
unsigned adc,RawData, WaveFreq,WavePhase;
unsigned int mux1,mux2;
float v1,v2,Analog_Voltage;
void runCom(void)
{
switch(Command){
case(INF):
RawData=readADC();
printf("ADC RawData:%d\r\n", RawData);
Analog_Voltage = voltage(RawData);
printf("Demodulator Voltage Level:%f [v]\r\n",Analog_Voltage);
Command = 0;
break;
case(WGF):
if(Param < 500)
SetWGFreq(Param);
WaveFreq = Param;
Command = 0;
break;
case(MUXSEL):
printf("MUX selection. ");
selcase(WaveFreq);
Command = 0;
break;
default:
Command = 0;
break;
}
}
void selcase(unsigned int arg)
{
unsigned char c;
c = getchar();
switch(c){
case '1':
PORTB=0x00;
SetWGFreq(arg);
Delay(1000);
mux1 = readADC();
printf("muxchanel 1 ADC RawData:%d\r\n",mux1);
v1 = voltage(mux1);
printf("Muxchanel 1 Demodulator Voltage Level:%f [v]\r\n",v1);
Command = 0;
break;
case '2':
PORTB=0x04;
SetWGFreq(arg);
Delay(1000);
mux2 = readADC();
printf("muxchanel 2 ADC RawData:%d\r\n",mux2);
v2 = voltage(mux2);
printf("Muxchanel 2 Demodulator Voltage Level:%f [v]\r\n",v2);
Command = 0;
break;
default:
Command = 0;
break;
}
}
From the above code I am able to read a converted value from the ADC correctly. But the problem is when I switch the multiplexer to channel 2 and read the ADC value after I've already read the ADC value of channel 1, I read back the same value I received for channel 1.
If I read the ADC value of channel 2 twice in a row, the second time I read it, it is the correct value.
I have tried to print readADC
value in the INF
command. It was printing exactly, no need to enter case twice. In INF
command it was giving exactly the right value when I change the multiplexer channel and checked with the INF
command. Values are changing so quickly when I enter the multiplexer selection.
The only problem is in the nested switch case I am trying to print ADC values but I have to enter the case twice to get the correct value. I have tried with delay function also but still the problem is same.
Why do I need to perform two reads to obtain the correct multiplexer channel conversion result?
PORTB
for? If you have the input going to one ADC channel and the output going to another ADC channel, you would select which channel is returned via SPI. Can you post a schematic of how these devices are connected together? – embedded.kyle Oct 23 '12 at 20:58