Code:
'CONFIG settings in 18F2553.INC
'CONFIG PLLDIV=5,CPUDIV=OSC1_PLL2,USBDIV=2,FOSC=HS,FCMEN=OFF,IESO=OFF
'CONFIG VREGEN=OFF,CCP2MX=ON,WDT=OFF,WDTPS=32768,PBADEN=OFF,PWRT=OFF,MCLRE=OFF
'CONFIG LPT1OSC=OFF,BOR=ON,BORV=2,STVREN=ON,LVP=OFF,XINST=OFF,DEBUG=OFF,WRTB=ON
DEFINE OSC 20
DEFINE DEBUG_REG PORTC
DEFINE DEBUG_BIT 6 ' Hardware USART TX pin
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0 ' Non-inverted mode through MAX233
ADResult VAR WORD
Q CON 3126 ' (5/4095)*256=0.312576313. rounded up to 3126
ADCON0 = %00000001 ' A/D module enabled, channel 0
ADCON1 = %00001110 ' Vref = Vdd & Vss, AN0 = analog
ADCON2 = %10101011 ' Right justified, 12 Tad, Frc clock
INTCON = %01000000 ' Global ints disabled, enable peripheral interrupts
PIE1.6=1 ' A/D interrupt enabled (for wake from sleep)
' Note we don't use an interrupt routine since the A/D complete
' interrupt is only waking the PIC from sleep.
Main:
ADCON0.1=1 ' Start the A/D conversion
@ SLEEP ' Sleep until A/D conversion finished
WHILE ADCON0.1 ' A/D complete?
WEND '
PIR1.6=0 ' Clear A/D int flag
ASM
MOVFF ADRESL,_ADResult ; get low byte
MOVFF ADRESH,_ADResult+1 ; get high byte
ENDASM
DEBUG "Raw A/D val = ",DEC ADResult,13,10
ADResult = ADResult */ Q
DEBUG "ADResult */ ",DEC Q," = ",DEC ADRESULT,13,10
DEBUG "ADResult = ",DEC ADResult DIG 4,".",DEC ADResult DIG 3,_
DEC ADResult DIG 2,DEC ADResult DIG 1,"V",13,10
PAUSE 2000
GOTO Main
END
Bookmarks