Hi I am using the ADC1241. I am using it to read 2 thermocouples. I have 1 main loop in my program. It does a bunch of stuff and then reads thermocouple 1 and then thermocouple 2. The problem is it is the outputs are swapped: it is giving me the TC2 data reading as TC1 temperature and vice versa. If I comment out the reading of one or the other, then it gives me the correct TC temperature for that specific TC#.
Basically, if I put TC1 in icewater and TC2 in room temperature, the program will tell me
TC1=78°F and TC2=34°F.
Can you help me find what I am doing wrong?
My program flow is this:
int main ()
{
loop_start:
do a bunch of stuff;
read_TC1(); //read thermocouple 1 value
read_TC2(); //read thermocouple 2 value
goto loop_start;
}
//////////Function Descriptions below:
void read_TC1()
{
if (ADC_waiting==0)
{
ADC_channel(4,5); //change MUX to channel 4 and 5
}
else if ((ADC_waiting==1)&&(ADC_chan=0x45))
{
ADC_check_drdy(); //check the DRDY bit
if (ADC_drdy==0) //data is ready
{
ADC_val=ADC_read();
}
}
}
void ADC_channel(uint8_t Pchan, uint8_t Nchan)
{
uint8_t MUX_chan;
MUX_chan=((Pchan<<4)&(0xF0))|((Nchan)&(0x0F)); //combine the two channels into 1 byte
If (ADC_waiting==0)
{
spi_init_ADC();
DELAY_US(100);
//Update MUX register
spi_ss(1);
spi_write(0x51); //WREG - write to MUX register
spi_write(0x00); //only write to 1 register (no additional registers, see pg. 19 for WREG command)
spi_write(MUX_chan); //MSbits select positive channel, LSbits select negative channel
spi_ss(31);
DELAY_US(100);
ADC_waiting=1; //now we ARE waiting on a conversion to complete because one just started
ADC_chan=MUX_chan;
//--------------------------------------
//DSYNC, see pg. 14 of ADS1241 datasheet
//--------------------------------------
spi_ss(1);
spi_write(0xFC); //DSYNC command - see pg. 18 and 14
spi_ss(31);
//--------------------------------------
//dummy write
//See pg. 14 of datasheet (DSYNC OPERATION section)
//after DSYNC command, it stays in reset until next SCLK pulse
//So I believe this dummy read will get it out of reset
spi_ss(1);
spi_write(0x00); //NOP command (NOP) - see pg. 18
spi_ss(31);
spi_init(); //go back to regular SPI
}
}