That won't work, since it will have to wake from sleep before running the ADCIN command.
I might suggest something like this...
It doesn't work in the simulator, but hopefully it will work on real hardware.
It uses the ADIF interrupt to wake from sleep. With the FRC clock, it only takes 48uS to complete a conversion.
Waiting for the WDT is going to take 1000uS or more.
The WDT is enabled just in case the conversion doesn't complete, but the ADIF should always wake it up first.
It assumes you are using other interrupts, and disables them accordingly.
Code:
#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
__config _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_ON & _BORV_19 & _LVP_OFF
#ENDCONFIG
OSCCON = %11110000 ; 32Mhz Internal
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 25 ' 19200 Baud @ 32MHz, 0.16%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
ADON VAR ADCON0.0 ; A/D converter Enable
GoDone VAR ADCON0.1 ; A/D conversion in progress
ADIE VAR PIE1.6 ; A/D Interrupt Enable
ADIF VAR PIR1.6 ; A/D Interrupt Flag
GIE VAR INTCON.7 ; Global Interrupt Enable
GIEsave VAR BIT ; Saves state of GIE
ADchan VAR BYTE ; Current A/D channel
ADvalue VAR WORD ; A/D result
ADCON1 = %10110000 ; Right Justify, FRC clock
ADON = 1
Main:
FOR ADchan = 0 TO 16 ; Loop through all A/D channe
ADCON0 = (ADCON0 & %11) | (ADchan << 2) ; Set Analog Channel
PAUSEUS 20 ; Acquisition time
GIEsave = GIE ; Save state of Global Interrupts
GIE = 0 ; Disable Global interrupts
ADON = 0 ; reset A/D module
ADON = 1
ADIF = 0 ; Clear Analog Interrupt Flag
ADIE = 1 ; Enable Analog Interrupt
GoDone = 1 ; Start Analog conversion
@ SLEEP ; go to SLEEP
ADIE = 0 ; Disable Analog Interrupt
GIE = GIEsave ; Restore Global Interrupt state
ADvalue.HighByte = ADRESH ; Get A/D result
ADvalue.LowByte = ADRESL
HSEROUT [DEC2 ADchan," = ",DEC ADvalue,13,10]
PAUSE 200
NEXT ADchan
GOTO Main
Bookmarks