Sometimes it needs to read one ADC channel, other times it needs to read the other channel, but my code does not work.
Can someone suggest why this code does not work? I realize there is a mode for sequences etc but it looks complicated and my code must be very close to working.
Thanks in advance! below is the code...
void init_temp_monitor_adc(void){
PM5CTL0 &= ~LOCKLPM5;
// By default, REFMSTR=1 => REFCTL is used to configure the internal reference
while(REFCTL0 & REFGENBUSY);
// voltage reference control
REFCTL0 = REFVSEL_2 | REFON_L | REFOUT_L;
// Configure ADC12
ADC12CTL0 = ADC12SHT0_2 | ADC12ON; // Cycle Sample Time, ADC On
ADC12CTL1 = ADC12SHP; // Source clock is sample timer
ADC12CTL2 |= ADC12RES_2; // 12-bit conversion
// ADC12IER0 |= ADC12IE0; // Interrupt MEM0 -- Not Used --
// ********* This Next Line seems to select forever either ADC12INCH_12 or ADC12INCH_13, but I want to select it when I call read_temp_adc() *******
ADC12MCTL0 |= ADC12INCH_13 | ADC12VRSEL_1; // Select A13, Vref Source = internal VREF (set to 2.5V above)
// ADC12MCTL0 |= ADC12INCH_12 | ADC12VRSEL_1; // Select A12, Vref Source = internal VREF (set to 2.5V above)
while(!(REFCTL0 & REFGENRDY)); // Wait for reference generator to settle
}
// ----------------------------------
// the parameter selects which channel to read
u16int_t read_temp_adc(u16int_t which){
u16int_t val;
if(which == 1){
ADC12MCTL0 |= ADC12INCH_12 | ADC12VRSEL_1; // Select A12 Channel A
while(!(REFCTL0 & REFGENRDY)); // Wait for reference generator to settle
__delay_cycles(50);
ADC12CTL0 |= ADC12ENC | ADC12SC; // Start conversion
// wait for conversion.
//
while (!(ADC12IFGR0 & ADC12IFG0)); //AA
val = ADC12MEM0; //
ADC12MCTL0 &= ~ADC12INCH_12;
}
else if(which == 2){ // TTT
ADC12MCTL0 |= ADC12INCH_13 | ADC12VRSEL_1; // Select A13 Channel B
while(!(REFCTL0 & REFGENRDY)); // Wait for reference generator to settle
__delay_cycles(50);
ADC12CTL0 |= ADC12ENC | ADC12SC; // Start conversion
// wait for conversion.
//
while (!(ADC12IFGR0 & ADC12IFG0)); //AA
val = ADC12MEM0; //
ADC12MCTL0 &= ~ADC12INCH_13;
}
ADC12CTL0 &= ~ADC12ENC; // do we need this?
return val;
}