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.

Adc Pic16f877a

Status
Not open for further replies.

derrick826

New Member
hi guys,

i have read the datasheet of the PIC16F877A from microchip regarding on the configuration of all the registers needed to program the ADC. i have used a potential resistor to vary different input voltages into my analogue pin RA0 starting from (0v - 4.80V) . my Vref+, voltage reference would be Vdd while Vref- would be Vss. The particular result which i get in ADRES is being displayed on 10 led lights to indicate that 10-bits are the result of the analogue being converted to digital. As i vary my pot , the results are in the following:

---------------9876543210
1v to pin RA0 = 0000000000
2v to pin RA0 = 0100000001
3v to pin RA0 = 1000000010
4v to pin RA0 = 1100000011

what may have gone wrong? how come the other LEDs of bit 7,6,5,4,3,2 is not lighting up? Must i use any component to fix to the pin RA0 of PIC16F877A?

here my coding:


org 0x000
goto start

int org 0x004
goto int


start
call initial
again
call startconv
call check
call result
goto again

initial
BCF STATUS,RP1
BSF STATUS,RP0
MOVLW b'00000001'
MOVWF TRISA
MOVLW b'00000000'
MOVWF TRISB
MOVLW b'00000000'
MOVWF TRISC
MOVLW b'00000000'
MOVWF TRISD
MOVLW b'10001110'
MOVWF ADCON1
BCF STATUS,RP1
BCF STATUS,RP0
MOVLW b'10000001'
MOVWF ADCON0
RETURN

startconv
BSF ADCON0,GO
RETURN


check
BTFSS PIR1,ADIF
goto check
MOVLW b'00000001'
MOVWF PORTC
BCF PIR1,ADIF
return

result
BCF STATUS,RP1
BSF STATUS,RP0
MOVFW ADRESL
MOVWF PORTB
BCF STATUS,RP1
BCF STATUS,RP0
MOVFW ADRESH
MOVWF PORTD
RETURN

END

Btw is there a specific calculation to get the digital values of 10bit?

what i know is this =


actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)


I dun really understand how this equation comes abt. I have read nigel-gwin's tutorial but still no improvement. Perhaps one to one attention right here might be a good help. thanks guys
 
Last edited:
A short delay is required before starting a new conversion. Check section 11.1 - "A/D acquistion requirements" in the datasheet. Also the maximum input impedance should be kept below 2.5 kΩ.
 
thanks for replying eng1

yea it mentioned here in the data sheet about time acquisition.. so before i start a conversion i have to delay at least about 20 micro second for the capacitor to charge up? and it says here that the maximum recommended impedance for analog sources is 10k ohm... so i should put a 10 Kohm in series along with the connection of the pot and RA0 pin?
 
derrick826 said:
yea it mentioned here in the data sheet about time acquisition.. so before i start a conversion i have to delay at least about 20 micro second for the capacitor to charge up?
Yes, at least 20 us.

derrick826 said:
and it says here that the maximum recommended impedance for analog sources is 10k ohm... so i should put a 10 Kohm in series along with the connection of the pot and RA0 pin?
No, that's the maximum allowed value for the analog source. The input impedance should be as low as possible, so don't add a series resistor.
From your first post I understand that you're using a pot as a voltage divider, what's its resistance?
 
derrick826 said:
thanks for replying eng1

yea it mentioned here in the data sheet about time acquisition.. so before i start a conversion i have to delay at least about 20 micro second for the capacitor to charge up? and it says here that the maximum recommended impedance for analog sources is 10k ohm... so i should put a 10 Kohm in series along with the connection of the pot and RA0 pin?

No, that will make it worse!, you would be adding an extra 10K to the existing impedance. If you're not switching channels it doesn't make that much difference, but what value is the pot you're using?.

As for the value conversion, 0V in gives a reading of zero, and Vref in (5V?) gives a reading of 1023.
 
i'm using 10K ohm pot.

This may sound silly but what's the difference if i put a 10kohm or any impedance below 2.5k ohm in series along with the connection of the pot and RA0 pin? is it important when comes to switching channels?

so with my code above.. just added with the 20micro second delay before starting the a/d conversion and that's it? this will give me a 10bit digital result from any analogue input i give to the assigned analogue pin?
 
Using a 10K pot it should be fine.

With a higher value source impedance it takes longer for the capacitor inside to charge, but as long as the input doesn't change very quickly then once it's initially charged there's no real problems about using a single input.

However, if you switch inputs then you can get instant large changes.

For example, you are reading 0.1V on one input, and switch to another with 4.8V on it, the capacitor has to charge from 0.1V to 4.8V before you can accurately take a reading from it. The same applies to switching to a lower voltage, the internal capacitor has to discharge.
 
oh i see, i think i have this concept right this time. so the equation which i mention is correct?

actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)

if i were to use my Vref+, voltage reference= Vdd (which is 5v) while Vref- = Vss (ground)

Any one more thing, the maximum voltage which can be applied to my AN0 pin
as analogue pin must not be more than 5v.. am i right?
 
hi,,

Try this, your original code is wrong.
Code:
 list p=16f877,f=inhX32, x=off
 include <p16f877.inc>
 

;free dev/debug regs 20 to 27

 errorlevel -302

 __CONFIG _CP_OFF & _XT_OSC & _PWRTE_ON  & _WDT_OFF & _LVP_OFF


	org 0x000
	goto start

	org 0x004


start
	call initial
again
	call startconv
	call check
	call result
	goto again

initial
	BCF STATUS,RP1
	BSF STATUS,RP0
	MOVLW b'00000001'
	MOVWF TRISA
	MOVLW b'00000000'
	MOVWF TRISB
	MOVLW b'00000000'
	MOVWF TRISC
	MOVLW b'00000000'
	MOVWF TRISD

	MOVLW b'10001110';rj,dddddd,a
	MOVWF ADCON1

	BCF STATUS,RP1
	BCF STATUS,RP0
	MOVLW b'10000001';osc/32,..... adon
	MOVWF ADCON0
	RETURN

startconv:
	BSF ADCON0,GO 
	RETURN 


check:
	BTFSS PIR1,ADIF
	goto check
	;;MOVLW b'00000001'
	;;MOVWF PORTC
	BCF PIR1,ADIF
	return

result: 
	BCF STATUS,RP1
	BSF STATUS,RP0 
	MOVF ADRESL,W; ****
	BCF STATUS,RP0;*****
	MOVWF PORTB
	BCF STATUS,RP1
	BCF STATUS,RP0
	MOVF ADRESH,W;*****
	MOVWF PORTD
	RETURN

	END

This now runs OK, in the Simulator.:)
 
Last edited:
oh yea... thanks a bunch eric... bank problems

MOVF ADRESL,W and MOVFW ADRESL is there any difference in that? can you tell me?
 
derrick826 said:
oh yea... thanks a bunch eric... bank problems

MOVF ADRESL,W and MOVFW ADRESL is there any difference in that? can you tell me?

MOVF ADRESL,W ; moves contents of ADRESL into W

MOVWF ADRESL; moves contents of W into ADRESL; edited: typo corrected.

I have now edited the code, it ALL in the code posted, paste and copy it, lets know.
 
Last edited:
derrick826 said:
i thought
MOVWF ADRESL ; moves contents of W into ADRESL ??

OMG i learned the wrong codes all these while..

Woops a typo!.

Should have read: MOVWF ADRESL
 
derrick826 said:
is there no such mnemonic of movfw ADRESL ? moves contents of ADRESL into W ?

I have seen MOVFW Reg, but I dont use it.

The assembler IIRC, MPLAB adds the extra lines of code.

EDIT:
Which assembler are you using??
 
Last edited:
lol okay then, i guess i have to test it .....my previous question
about the equation which i mention is it correct?

actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)

if i were to use my Vref+, voltage reference= Vdd (which is 5v) while Vref- = Vss (ground)

and the maximum voltage which can be applied to my AN0 pin
as analogue pin must not be more than 5v.... am i right?
 
derrick826 said:
lol okay then, i guess i have to test it .....my previous question
about the equation which i mention is it correct?

actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)

if i were to use my Vref+, voltage reference= Vdd (which is 5v) while Vref- = Vss (ground)

and the maximum voltage which can be applied to my AN0 pin
as analogue pin must not be more than 5v.... am i right?

Yes dont exceed +5Vdc input to the ADC when the Vdd =+5V

The equation is correct. The values in the ADC regs are binary:)
 
hi again, i have tried the codings and it works fine but it seems that the accuracy of 10bit- digital outputs on the LEDs after conversion when compare using the formula:

actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)

is kinda out...

for example: when i input 4.78v into RA0 it gives me 1111111111 (all leds light up)

when i input 0.02v into RA0 it gives me 0000000001 (first led light up)

but according to the formula which i mentioned, the first LED will light up which represent 1 bit if 4.8mv is being applied:

4.8mv = 1* (5/1023)

is there any method which I can do to improve its accuracy from my codings?
 
Status
Not open for further replies.

Latest threads

Back
Top