-----Original Message----- From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu]On Behalf Of Shawn Wilton Sent: Friday, November 03, 2006 12:23 PM To: Microcontroller discussion list - Public. Subject: [PIC] A/D on pic16f88 I'm using a PIC16F88, trying to read a voltage off of what is essentially a trim pot. It's actually a variably resistant touch panel. Pins should not be tied together unless pressure is being applied to the panel and then you slide your finger up and down the panel to adjust the resistance. This is a fabric panel. The Vref+ is set for AVdd which is tied to Vdd and Vref- is set for AVss which is tied to Vss. The board works, runs great. Code is below. Problem is that the sense pin (AN2, porta.2) is registering nearly the full AVdd when no panel is connected and then around 2-3V when the panel is connected . **** while disconnected what voltage do you have on porta.1 ? **** Is this typical for a PIC uC? The A/D conversion works, **** no. some things not right? **** but it seems like I'm going to have to tie my sense pin to a very weak pull down **** maybe you already have a pullup? **** in order for it to register properly. Does this sound right? **** no **** you need to add a delay to charge capacitors before conversion Andre Abelian Relevant code below in BoostC format: /******** Setup the I/O ports ********/ /* Set the io ports as follows: Port A: 7 nu 6 nu 5 \mclr 4 nu 3 SW2 (input) 2 SW1 (input) 1 LED (output) 0 nu */ // 76543210 trisa = 11111101b; porta = 0; ansel = 00000000b; /* Port B: 7 nu 6 nu 5 txd (output) 4 clock (output) 3 nu 2 rxd (input) 1 sdi (input) 0 pwm (output) */ // 76543210 trisb = 11011110b; /******** Setup the ADC ********/ //Set A/D ports 2 and 3 for analog inputs set_bit(ansel, ANS2); //set_bit(ansel, ANS3); /* ADCon0 7 AD conversion clock 6 5 Channel select bits (2) 4 3 2 Go/\Done 1 nu 0 ADON - Yes */ // 76543210 adcon0 = 11010001b; /* ADCon1 7 Justification (left) 6 AD clock divide by 2 (yes) 5 Vref config bits 4 3 nu 2 nu 1 nu 0 nu */ // 76543210 adcon1 = 11000000b; /** Read the specified ADC channel and return an unsigned int result */ unsigned int readADChannel(unsigned char channel) { //unsigned int result; unsigned int high = 0; unsigned int low = 0; volatile unsigned char addr_h@ADRESH; volatile unsigned char addr_l@ADRESL; //Set the ADC Channel //adcon0 &= 00 << 2; adcon0.CHS2 = 0; adcon0.CHS1 = 0; adcon0.CHS0 = 0; adcon0 |= channel << 3; //Turn on the ADC //adc_on = TRUE; //Delay to perform the acquisition delay_ms(2); //Begin a conversion adc_go = TRUE; //Begin the conversion and block until it's complete while(adc_go == TRUE){} //Clear the interrupt flag //clear_bit(pir1, ADIF); //Convert to 16 bit vars so we can shift properly high = addr_h; low = addr_l; //Turn off the ADC since we are no longer using it //adc_on = FALSE; //Shift the upper bits up and the lower bits down return (high << 8) | (low); } -- Shawn Wilton (b9 Systems) http://b9Systems.com <- New web page -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist