este ejemplo està en inglès, pero si le echas cabeza creo que algo puedes obtener, lo saquè de la misma ayuda de protòn.
Código:
12F675 ADC Example
' Perform an ADC conversion on the 12F675 8-pin PICmicro device
Device = 12F675
XTAL = 4
SERIAL_BAUD = 9600
RSOUT_PIN = GPIO.0
RSOUT_MODE = TRUE
RSOUT_PACE = 10
'-------[DEFINE SOME ALIAS'S TO ADC REGISTERS]---------------------------
' ANSEL register's bits
Symbol ANS0 = ANSEL.0
Symbol ANS1 = ANSEL.1
Symbol ANS2 = ANSEL.2
Symbol ANS3 = ANSEL.3
Symbol ADCS0 = ANSEL.4 ' ADC conversion clock select bit
Symbol ADCS1 = ANSEL.5 ' ADC conversion clock select bit
Symbol ADCS2 = ANSEL.6 ' ADC conversion clock select bit
' ADCON0 register's bits
Symbol ADFM = ADCON0.7
Symbol VCFG = ADCON0.6
Symbol CHS2 = ADCON0.4 ' ADC channel select bit
Symbol CHS1 = ADCON0.3 ' ADC channel select bit
Symbol CHS0 = ADCON0.2 ' ADC channel select bit
Symbol GO_DONE = ADCON0.1 ' ADC Conversion status/ plus enable conversion-bit
Symbol ADON = ADCON0.0 ' ADC Enable bit: 1 = enabled, 0 = disabled.
'-------[ASSIGN A VARIABLE FOR THE ADC RESULT]---------------------------
Dim AD_RESULT as ADRESL.WORD ' Convert the ADRESL register into a WORD variable
'-------[INITIALISE THE PICMICRO]----------------------------------------
Delayms 500 ' Wait for the PICmicro to stabilise
Goto OVER_ADC_SUBS ' Jump over the subroutines
'-------[START AN ADC CONVERSION]----------------------------------------
GET_ADC:
ADON = 1 ' Enable the ADC
Delayus 50 ' Wait for sample/hold capacitors to charge
GO_DONE = 1 ' Start conversion
While GO_DONE = 1 : Wend ' Poll the GO_DONE flag for completion of conversion
ADON = 0 ' Disable the ADC, to save power
Return
'-------[INITIALISE THE ADC REGISTERS]----------------------------------
' Standard procedures for setting up the ADC
OVER_ADC_SUBS:
TRISIO = %11111111 ' All pins set for input
ADCS0 = 1 ' \
ADCS1 = 1 ' Setup ADC's clock for FRC
ADCS2 = 0 ' /
VCFG = 0 ' VREF is set to VDD of PICmicro
ADFM = 1 ' Right justify the ADC result
ANS0 = 0 ' Set AN0 (GPIO.0) as Digital input
ANS1 = 0 ' Set AN1 (GPIO.1) as Digital input
ANS2 = 1 ' Set AN2 (GPIO.2) as Analogue input
ANS3 = 1 ' Set AN3 (GPIO.3) as Analogue input
'-------[MAIN PROGRAM LOOP STARTS HERE]----------------------------------
' Perform ADC conversions and display the result serially
While 1 = 1 ' Create an infinite loop
ADCON0 = ADCON0 | (2 << 2) ' Select the channel to read
Gosub GET_ADC ' Perform an ADC conversion
Rsout "CHANNEL 2 = " , DEC AD_RESULT,13 ' Display the result serially
Delayms 500 ' Wait for half a second
Wend ' Do another conversion