I removed the CCP stuff to leave just the ADC and still the value jumps around by +- 6 or more:
Code:DEFINE OSC 16 ' Set oscillator 16Mhz ' For production version of this code, comment out the line below re: ' LCD debugging and also update config fuses to have code protect on #DEFINE USE_LCD_FOR_DEBUG ; comment out for non-debug use ' *************************************************************** ' Pin Connections ' *************************************************************** ' VDD -> pin 1 -> +5V ' RA5/Rc -> pin 2 -> EUSART receive ' RA2 -> pin 5 -> 1kohm -> 2N2222A transistor -> LEDs ' RA1 -> pin 6 -> Trim pot input ' RA0/Tx -> pin 7 -> EUSART transmit (LCD) ' VSS -> pin 8 -> GND ' *************************************************************** ' EUSART Settings for Tx/Rc (e.g. LCD) ' *************************************************************** ' > use Mister E PIC Multi-Calc application to get register/DEFINE settings ' > as the values are dependent on the OSC and desired baud rate DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1 DEFINE HSER_CLROERR 1 ' Clear overflow automatically DEFINE HSER_SPBRG 160 ' 9600 Baud @ 16MHz, -0.08% ' *************************************************************** ' Device Fuses ' *************************************************************** ' PIC chip data sheets can be found here: C:\Program Files\Microchip\MPASM Suite #CONFIG __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF __config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF #ENDCONFIG ' *************************************************************** ' Initialization ' *************************************************************** OSCCON = %01111000 ' 16MHz internal osc pause 100 ' for EEPROM issue APFCON.2 = 0 ; Tx on RA0 for LCD display APFCON.7 = 1 ; Rc on RA5 APFCON.0 = 0 ; CCP1 on RA2 ' Some LCD serial modules need inverted data, some do not ' Enable the line below if needed, but for SparkFun SerLCD it should be ' commented out 'BAUDCON.4 = 1 ; Transmit inverted data to the Tx pin ' From Mister E's Multi-Calc (EUSART): ' ***************************************************************** SPBRGH = 1 BAUDCON.3 = 1 ' Enable 16 bit baudrate generator ' ***************************************************************** FVRCON = 0 ' Fixed Voltage Reference is disabled ANSELA = %00000010 ; Analog on PORTA.1 (AN1) only ADCON0 = %00000101 ' ADC (analog-to-digital) is enabled on AN1 (RA1) only PAUSEUS 20 ; wait for the analog switch 'glitch' to die down ADCON1 = %10010000 ; Right-justified results in 10-bits; Fosc/8 as timer; ; VREF is connected to VDD TRISA = %00100010 ' Make all pins output except for RA1 (trim pot input) ' and RA5 (EUSART Rc) ADCInVal VAR WORD ; stores ADCIN result read from trim pot compVal VAR WORD #IFDEF USE_LCD_FOR_DEBUG ' Display control codes for SerLCD serial LCD (SparkFun part #LCD-09395) ' (see 'Dropbox\PBP Projects\PIC Datasheets\SerLCD_V2_5 Datasheet.pdf' ' for list of control codes) LCD_INST CON 254 ' instruction LCD_CLR CON 1 ' Clear screen LCD_L1 CON 128 ' LCD line 1 LCD_L2 CON 192 ' LCD line 2 LCD_BR_CMD CON 124 ' command character for adjusting backlight brightness LCD_BR_LVL CON 140 ' 140==40% #ENDIF ' Should only need to do this one time to adjust backlight brightness '#IFDEF USE_LCD_FOR_DEBUG ' HSEROUT [LCD_BR_CMD, LCD_BR_LVL] ' PAUSE 5 '#ENDIF #IFDEF USE_LCD_FOR_DEBUG pause 1000 HSEROUT [LCD_INST, LCD_CLR, "LCD Init"] pause 5 HSEROUT [LCD_INST, LCD_L2, "SerLCD_V2_5"] pause 500 #ENDIF GOSUB DO_ADCIN_Chk compVal = ADCInVal #IFDEF USE_LCD_FOR_DEBUG HSEROUT [LCD_INST, LCD_CLR] pause 5 HSEROUT ["ADCInVal=",DEC ADCInVal," "] pause 500 #ENDIF Main: GOSUB DO_ADCIN_Chk PAUSE 100 IF ADCInVal <> compVal THEN #IFDEF USE_LCD_FOR_DEBUG HSEROUT [LCD_INST, LCD_CLR] pause 5 HSEROUT ["new ADCInVal", LCD_INST, LCD_L2, DEC ADCInVal, " "] #ENDIF compVal = ADCInVal ENDIF GOTO Main '*********** Read ADC or USART Rc inputs ******************************* Do_ADCIN_Chk: ' Read trim pot Vref to set LED brightness PAUSEUS 50 ' Wait for A/D channel acquisition time ADCON0.1 = 1 ' Start conversion WHILE ADCON0.1 = 1 ' Wait for it to complete WEND ADCInVal.HighBYTE = ADRESH ADCInVal.LOWBYTE = ADRESL return




Bookmarks