Hello Schalasa,
There are a number of operational issues that need to be sorted, and then see if your original problem still persists.
Since you are sequentially measuring two different channels, it is quite inappropriate to select continuous conversion mode. The usual method would be to start the first conversion, and on its completion to start the second conversion. Additionally, the APCTL1 setting appears to disable the GPIO pin for only one of the ADC channels.
Some of the code you have included is quite unnecessary, for example explicitly writing zero to the COCO bit. The flag variable also does nothing. You might need a flag to keep track of the current channel only if you were to use interrupts. I arrived at the following simplified code snippet.
int rssi_r, rssi_l;int diff, diff1;ADCSC1 = 0x01; // Start conversion ADC channel 1while (ADCSC1_COCO == 0) ; // Wait for completionrssi_r = ADCR;ADCSC1 = 0x02; // Start conversion ADC channel 2while (ADCSC1_COCO == 0); // Wait for completionrssi_l = ADCR; diff = rssi_l - rssi_r;diff1 = (diff/2) - 5; // offset
Did you previously get the correct signed result within diff? With the division by 2, I can see no reason why the diff1 calculation should overflow.
The ADC initialisation might be modified to the following:
void ADC_init( void){ ADCCFG = 0xF8; // low power, long samp, 10 bit, bus clock/8 ADCSC2 = 0x00; // compare fn disabled ADCSC1 = 0x1F; // ADC module disabled APCTL1 = 0x06; // GPIO disabled channels 1 & 2}
Regards,
Mac