--=-oVMhoUsXysqqP7USzBzX Content-Type: text/plain Content-Transfer-Encoding: 7bit Hello, I am trying to understand how to use the A/D converter on the PIC 16F676. To do so, I am measuring several known voltages derived from a few voltage dividers giving me 5, 3.3, 2.5, 1.7, and 0 volts. The numbers I am getting from the ADC are V ADC 0 0 1.67 257 2.5 257 3.33 514 5 771 With the exception of the 2.5 volt measurement, I get a reading of about 154 per volt. However that 2.5V one makes no sense, leading me to believe I've set up the ADC incorrectly somehow. Most problems in the archives have been either people missing the right vs left justification, or not waiting enough time for the sample to be taken, both of which I think I've handled. ADC setup is below, the whole code is sent as an attachment. The delay sub is a 16 bit loop. I'm using the internal 4MHz clock. ; Set up ports clrf PORTA bsf STATUS, RP0 ; Bank 1 movlw B'11111100' ; RA0/1 output for LEDs movwf TRISA ; RA4 is an input for AD movlw B'00001000' ; AN3 is Analogue, the rest are digital movwf ANSEL ; Set up AD controller bsf STATUS, RP0 ; Bank 1 movlw B'00010000' ; x001xxxx Fosc/8 movwf ADCON1 bcf STATUS, RP0 ; Bank 0 movlw B'10001101' ; 1 Right Justified ; 0x Use Vdd ; 011 AN3 ; 0 GO off ; 1 AD on movwf ADCON0 read movlw B'00000011' ; Flash both lights to indicate movwf PORTA ; start of conversion call delay call delay call delay clrf PORTA call delay bsf ADCON0, GO ; Start conversion Wait btfsc ADCON0, NOT_DONE goto Wait ; Done call delay At this point I flash the result of ADRESL and the low two bis of ADRSH on some LEDs. Any advice would be appreciated. Thanks, Sean -- Sean A. Walberg http://www.ertw.com -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details. --=-oVMhoUsXysqqP7USzBzX Content-Disposition: attachment; filename=adtest.asm Content-Type: text/plain; name=adtest.asm; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit PROCESSOR p16f676 INCLUDE p16f676.inc __CONFIG _CP_OFF&_WDT_OFF&_PWRTE_ON&_INTRC_OSC_NOCLKOUT ERRORLEVEL -302 ; Don't care about the "bank bits" messages CBLOCK 0x20 ; Declare variable addresses starting at 0x20 COUNT1 COUNT2 COUNT3 shadow ENDC ; A couple of macros to display a number on two LEDs ; the first LED is zero, the second is one show_zero macro movf PORTA, w movwf shadow bsf shadow, 0 bcf shadow, 1 movf shadow, w movwf PORTA call delay show_blank call delay endm show_one macro movf PORTA, w movwf shadow bcf shadow, 0 bsf shadow, 1 movf shadow, w movwf PORTA call delay show_blank call delay endm show_blank macro movf PORTA, w movwf shadow bcf shadow, 0 bcf shadow, 1 movf shadow, w movwf PORTA endm ORG 0x000 ; Program starts at 0x000 goto main main ; Set up ports clrf PORTA bsf STATUS, RP0 ; Bank 1 movlw B'11111100' ; RA0/1 output for LEDs movwf TRISA ; RA4 is an input for AD movlw B'00001000' ; AN3 is Analogue, the rest are digital movwf ANSEL ; Set up AD controller bsf STATUS, RP0 ; Bank 1 movlw B'00010000' ; x001xxxx Fosc/8 movwf ADCON1 bcf STATUS, RP0 ; Bank 0 movlw B'10001101' ; 1 Right Justified ; 0x Use Vdd ; 011 AN3 ; 0 GO off ; 1 AD on movwf ADCON0 read movlw B'00000011' movwf PORTA call delay call delay call delay clrf PORTA call delay bsf ADCON0, GO ; Start conversion Wait btfsc ADCON0, NOT_DONE goto Wait ; Done call delay ;testbit ADRESL, 0 test0 btfss ADRESL, 0 goto bit0_0 goto bit0_1 bit0_0 show_zero goto test1 bit0_1 show_one test1 btfss ADRESL, 1 goto bit1_0 goto bit1_1 bit1_0 show_zero goto test2 bit1_1 show_one test2 btfss ADRESL, 2 goto bit2_0 goto bit2_1 bit2_0 show_zero goto test3 bit2_1 show_one test3 btfss ADRESL, 3 goto bit3_0 goto bit3_1 bit3_0 show_zero goto test4 bit3_1 show_one test4 btfss ADRESL, 4 goto bit4_0 goto bit4_1 bit4_0 show_zero goto test5 bit4_1 show_one test5 btfss ADRESL, 5 goto bit5_0 goto bit5_1 bit5_0 show_zero goto test6 bit5_1 show_one test6 btfss ADRESL, 6 goto bit6_0 goto bit6_1 bit6_0 show_zero goto test7 bit6_1 show_one test7 btfss ADRESL, 7 goto bit7_0 goto bit7_1 bit7_0 show_zero goto test8 bit7_1 show_one test8 btfss ADRESH, 0 goto bit8_0 goto bit8_1 bit8_0 show_zero goto test9 bit8_1 show_one test9 btfss ADRESH, 1 goto bit9_0 goto bit9_1 bit9_0 show_zero goto test10 bit9_1 show_one test10 show_blank ;testbit ADRESL, 1 ;testbit ADRESL, 2 ;testbit ADRESL, 3 ;testbit ADRESL, 4 ;testbit ADRESL, 5 ;testbit ADRESL, 6 ;testbit ADRESL, 7 goto read delay movlw D'3' movwf COUNT3 loop1 decfsz COUNT1, f goto loop1 decfsz COUNT2, f goto loop1 decfsz COUNT3, f goto loop1 return END -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details. --=-oVMhoUsXysqqP7USzBzX--