er - RA4 on the PIC isn't an analogue pin...... ----- Original Message ----- From: Ed Roberts To: Sent: Wednesday, July 19, 2000 7:52 AM Subject: [PIC]: ADC Channel Selection on 16F877 I am trying to read ADC channels 0-4 (pins A0 - A4) on a 16F877 / 20, from a chain of resistors to set test voltages. Attempting to read Channel 0 gives a low, and variable, value. Reading 'Ch 1' gives the result corresponding to the voltage on Pin A0 (measured at the PIC chip pin), and so on to 'Ch 4' (ie value on A3). Extending the reads to channels 5,6,7 (which are not connected) give noise, ie none show the value on A4. The chip is in a Dontronics DT106 board. The result is the same with or without the pullup resistor on Pin A4. I have swapped chips with no difference, and tried ADC settling times from 5us to 25 ms. An extract from the code is attached, using Baranov's P2C Pascal compiler and inserted assembler. Any suggestions appreciated, Thanks, Ed Roberts procedure setup_ADC; begin output_high_port_b(1); asm { include "d:\arpldata\pic_code\mpasm\p16f877.inc" ; initial setup of ADC ; Setup ADCON1 (data format) bsf STATUS, RP0 ; set bank 1 movlw 0x80 ; 0x|10000000 = right justified, 8 input, Vss, Vdd movwf ADCON1 ;Setup ADCON0 (clock rate, input channel, go bit, status) bcf STATUS, RP0 ; set bank 0 movlw 0x80 ; osc/32, channel 0 input movwf ADCON0 bsf ADCON0, ADON ; turn ADC on ; Interrupt Setting bcf PIR1, ADIF ; clear AD interrupt flag bit ;bsf INTCON, PEIE ; Enable peripheral interrupts; ;bsf INTCON, GIE ; Enable all interrupts } delay_ms(200); output_low_port_b(1); end; procedure read_ADC_channels; begin output_high_port_b(2); delay_ms(20); if ADC_ch = 0 then asm { bcf STATUS, RP0 ; set bank 0 bcf ADCON0, CHS0 bcf ADCON0, CHS1 bcf ADCON0, CHS2 } else if ADC_ch = 1 then asm { bcf STATUS, RP0 ; set bank 0 bsf ADCON0, CHS0 bcf ADCON0, CHS1 bcf ADCON0, CHS2 } else if ADC_ch = 2 then asm { bcf STATUS, RP0 ; set bank 0 bcf ADCON0, CHS0 bsf ADCON0, CHS1 bcf ADCON0, CHS2 } else if ADC_ch = 3 then asm { bcf STATUS, RP0 ; set bank 0 bsf ADCON0, CHS0 bsf ADCON0, CHS1 bcf ADCON0, CHS2 } else if ADC_ch = 4 then asm { bcf STATUS, RP0 ; set bank 0 bcf ADCON0, CHS0 bcf ADCON0, CHS1 bsf ADCON0, CHS2 } else; output_low_port_b(2); delay_us(50); // acquisition delay asm { bcf STATUS, RP0 ; set bank 0 bsf ADCON0, GO ; initiate conversion } asm: loop1 btfss ADCON0, GO asm { goto loop1 ; loop while bit is set (ie converting) } output_low_port_b(2); asm { bcf STATUS, RP0 ; Set Bank 0 movf ADRESH, w ; load W with MSB result movwf _input_high_bits ; output W to final pascal variable bsf STATUS, RP0 ; set Bank 1 movf ADRESL, w ; load W with LSB result movwf _input_low_byte ; output W to final pascal variable } output_low_port_b(2); delay_ms(20); end; -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details. -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.