Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Strange behavior on analogue inputs of PIC16F887

Status
Not open for further replies.

I2O

New Member
There is a strange behavior on the analogue inputs of the PIC16F887 I am using.
I wrote a program to read an analogue value.
If I read only one analogue value, all goes well. The analogue value is read and stored in the corresponding register.
In the example the value at RA1 read by AN1 and is stored in register named AnalogueValue01

Analogue value at => read by => stored in
RA1 => AN1 => AnalogueValue01

Now when I read more than one analogue value, the analogue value is readout by the next analogue 'port' and the value is not stored in corresponding register.
In the example I get:

Analogue value at => read by => stored in
RA1 => AN2 ??? => AnalogueValue02
RA2 => AN3 ??? => AnalogueValue03
RA3 => AN1 ??? => AnalogueValue01

Who can explain this strange behavior?

Thanks,

Nijn


Code:
#include <p16F887.inc>

;external clock AT 20MHz

	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC
	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V

     cblock 0x20
ActivePort01
AnalogueValue01
ID01
Delay_1
     endc
     cblock 0x30
ActivePort02
AnalogueValue02
ID02
     endc
      
     cblock 0x40
ActivePort03
AnalogueValue03
ID03
	endc

	
	org 0
Start:
	bsf       STATUS,RP0				; select Register Bank 1
	movlw     0xFF
	movwf     TRISA						; Make PortA all input
	clrf      TRISB						; Make PortB all output
	clrf      TRISD						; Make PortD all output
	movlw     0x00						; Left Justified, Vdd-Vss referenced
	movwf     ADCON1	
	bsf       STATUS,RP1				; select Register Bank 3
	movlw     0xFF						; we want all Port A pins Analog
	movwf     ANSEL
	bcf       STATUS,RP0				; back to Register Bank 0
	bcf       STATUS,RP1
  
Main


Part1   
	movlw		.01
	movwf		ID01	
	movlw     	0x85					; configure A2D for Fosc/32, Channel 0 (RA0), and turn on the A2D module
	movwf     	ADCON0         
	movwf		ActivePort01 
	bsf			PORTB, 4				; check delay		
	movlw		.10						; wait minimal 5µs  to load the internal condenator( .10 = about 10µs)
	call 		DELAY
	bcf			PORTB, 4				; check delay
	bsf       	ADCON0,GO_DONE			; start conversion
	btfss     	ADCON0,GO_DONE			; this bit will change to zero when the conversion is complete
	goto      	$-1
	movf      	ADRESH,w       ; Copy the display to the LEDs
	movwf	  	AnalogueValue01 
	movwf     	PORTD
	movlw		.100
	call		DELAY	

;	goto 		Main  = test only one analogue value
	
Part2 
	movlw		.02
	movwf		ID02  
	movlw     	0x89
	movwf     	ADCON0         
	movwf		ActivePort02 
	bsf			PORTB, 4				
	movlw		.10
	call 		DELAY
	bcf			PORTB, 4
	bsf       	ADCON0,GO_DONE 
	btfss     	ADCON0,GO_DONE 
   	goto      	$-1
   	movf      	ADRESH,w       
  	movwf	  	AnalogueValue02 
	movwf     	PORTD
	movlw		.100
	call		DELAY

Part3 
	movlw		.03
	movwf		ID03  
	movlw     	0x8d
	movwf     	ADCON0         
	movwf		ActivePort03 
	bsf			PORTB, 4				
	movlw		.10
	call 		DELAY
	bcf			PORTB, 4
	bsf       	ADCON0,GO_DONE 
	btfss     	ADCON0,GO_DONE 
	goto      	$-1
	movf      	ADRESH,w       
	movwf	  	AnalogueValue03 
	movwf     	PORTD
	movlw		.100
	call		DELAY

	goto      	Main



DELAY
	movwf		Delay_1	
LOOP
	nop
	decfsz		Delay_1, f   ; delay
	goto LOOP		 
return

     end
 
Last edited:
What is the source impedance feeding the pins? - it needs to be pretty low (2K max) - otherwise you need to increase the delay time between input changes.
 
You are not waiting for the conversion to complete.
Code:
Part1			 
		movlw	.01
		movwf	ID01
		movlw	0x85		; configure A2D for Fosc/32, Channel 0 (RA0), and turn on the A2D module
		movwf	ADCON0         
		movwf	ActivePort01 
		bsf	PORTB,4		; check delay
		movlw	.10		; wait minimal 5µs  to load the internal condenator( .10 = about 10µs)
		call	DELAY
		bcf	PORTB,4		; check delay
		bsf	ADCON0,GO_DONE	; start conversion
		[COLOR="red"]btfss[/COLOR]	ADCON0,GO_DONE	; this bit will change to zero when the conversion is complete
		goto	$-1
		movf	ADRESH,w	; Copy the display to the LEDs
The btfss should be btfsc.

Mike.
 
The btfss should be btfsc.

Pommie, you are 100% correct.
I have copied this part from an exercise of Microchip, never thought I had check it.
Not clever of me.

Many thanks!
 
What is the source impedance feeding the pins? - it needs to be pretty low (2K max) - otherwise you need to increase the delay time between input changes.

An interesting thing to think about, probably the reason why I had to put a delay between every readout.
For testing I have a pot of 4K7 to VCC, other pin is not connected. On the input there is also a 4K7 pull down.

For the final solution I will have a pull down of 4k7 and the output of a hall sensor A1321
What would be the minimum delay between the read outs? and how should I calculate them?

Is it correct to do it like this? and should I add a condensator to eliminate interference between different analogue inputs?
I will read up to 8 analogue inputs?

Thanks,

Nijn
 

Attachments

  • analogue inputs.gif
    analogue inputs.gif
    5 KB · Views: 184
Last edited:
With 4.7K you're pretty well OK - the two 4.7K's give a source of half of that figure - 2.35K - which is close enough.

There's a sample and hold capacitor (condensor disappeared decades ago) inside the chip, it the charge/discharge time of that which makes the channel switching time dependent.
 
Is the scheme of the final application, the one with the A1321 OK?
Would I need any condensator to eliminate interference between different analogue inputs?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top