Paul Duffy wrote: > Hi > > I am using the 16F684 and want to read 2 different A/D inputs to set 2 di= fferent time delays in my widget. I have 2 potentiometers, set up as volta= ge dividers, connected to the A/D inputs. My attempts to read the A/D are = failing. Here is the relevant code snippet: > ..... > ..... > off =3D 0; > on =3D 0; > ansel =3D 0b0000011; //select AN0, AN1 (pins 12, 13) as A/D i= nputs > adcon1 =3D 0b01010000; //select 16*Tosc clock - gives TAD of 4uS = > adcon0 =3D 0b00000001; //turn on A/D, select AN0 as input, don't s= tart conversion > > while(1) //Main Loop - continuously update = porta and portc. = > { > porta =3D off; //display via leds connected to a= vailable port pins > portc =3D on; //display via leds connected to av= ailable port pins > = > adcon0 =3D 0b00000001; //turn on A/D, select AN0 as inp= ut, don't start conversion > delay_ms(10); //delay each time through loop= , = > set_bit(adcon0,1); //start a/d conversion > while (!test_bit(adcon0,1)); //wait for completion > on =3D adresh; //store on_time - AN0 > if(on=3D=3D0)on =3D on+1; //ensures on_time !=3D0 > = > adcon0 =3D 0b00000101; //Change the input to AN1, don't= start conversion > delay_ms(10); //delay each time through loo= p, = > set_bit(adcon0,1); //start a/d conversion > while (!test_bit(adcon0,1)); //wait for completion > off =3D adresh; //store off_time - AN1 > if(off=3D=3D0)off =3D off+1; //ensures off_time != =3D0 > } > > As it is, this does not work at all. I see no change on the leds connect= ed to portc or porta when I change the potentiometers. If I remove one of = the "adcon0=3D..." statements, then one of the potentiometers is read and d= isplayed in both ports. Depending on which of the 2 "adcon0=3D..." stateme= nts I delete, I can read either potentiometer. How do I switch between the= a/d inputs so that I can read both potentiometers? > > Thanks in advance for any help. > > Paul Duffy > > > = Hi Paul, I think the TRIS bits also have to be set so the AD channels are read as = inputs. The PIC16F684 Datasheet has a code snippet in assembly language = (below). The command to notice is BSF TRISA, 0, in other words, set = TRISA bit 0 to 1. In your case, I think you'll need to set TRISA bit 1 = also. Sorry not to be more help. I'm struggling with the A/D converter of the = PIC16F684 right now, and can't get it to read any input other than AN0. Tony Russell EXAMPLE 9-1: A/D CONVERSION ;This code block configures the ADC ;for polling, Vdd reference, Frc clock ;and AN0 input. ; ;Conversion start & polling for completion ; are included. ; BANKSEL ADCON1 ; MOVLW B=9201110000=92 ;ADC Frc clock MOVWF ADCON1 ; BANKSEL TRISA ; BSF TRISA,0 ;Set RA0 to input BANKSEL ANSEL ; BSF ANSEL,0 ;Set RA0 to analog BANKSEL ADCON0 ; MOVLW B=9210000001=92 ;Right justify, MOVWF ADCON0 ; Vdd Vref, AN0, On CALL SampleTime ;Acquisiton delay BSF ADCON0,GO ;Start conversion BTFSC ADCON0,GO ;Is conversion done? GOTO $-1 ;No, test again BANKSEL ADRESH ; MOVF ADRESH,W ;Read upper 2 bits MOVWF RESULTHI ;store in GPR space BANKSEL ADRESL ; MOVF ADRESL,W ;Read lower 8 bits MOVWF RESULTLO ;Store in GPR space -- = http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist