Hi,
Code shown in message #1 seem like a mixup with some MCC generated code,
so there are a lot of details that are not shown.
There is no need to enable Global interrupts in order to wake up from sleep when ADC conversion is complete.
Device Interrupt from the ADC module must be Enabled in order to wake up.
Do Not go to sleep before the acquisitiion delay,
there is no way for __delay_us(20); to wake the processor from Sleep mode,
so it will sleep until some other event wake up the processor, or it is Reset.
Go to SLEEP() after the Start Conversion bit have been Set:
ADIF = 0; /* Make sure ADC Interrupt flag from previous conversion have been cleared. */
ADIE = 1; /* Make sure that ADC interrupt is enabled. */
__delay_us(20); // wait for acquisition time.
ADCON0bits.GOnDONE = 1; // start the conversion
SLEEP();
You cannot perform calculations, or test results while the processor is in Sleep mode.
The device have to wake up after ADC conversion is done,
to test result and light the LED.
If you want some time to sleep between measurements,
then it is possible to arrange another sleep with Watchdog timer,
or another timer clocked from LPRC, to wake the device.
Mysil
post edited by Mysil - 2020/11/23 09:59:24