PDA

View Full Version : 10bits adc using 16f877



sixty9sandals
- 5th March 2007, 22:33
hi..
i have a problem..im trying to display a digital value in a row of 10leds. as example i supplied 1.5v as a analog input with the Vref 5V. but y is that the value display on the leds are not the right value.is there something wrong with my codes.my codes are shown below:

define ADIN_RES 10
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

MEASURED_VALUE VAR word
REMAINDER0 VAR word
MEASURED0 VAR word
REMAINDER1 VAR word
MEASURED1 VAR word
REMAINDER2 VAR word
MEASURED2 VAR word
REMAINDER3 VAR word
MEASURED3 VAR word
REMAINDER4 VAR word
MEASURED4 VAR word
REMAINDER5 VAR word
MEASURED5 VAR word
REMAINDER6 VAR word
MEASURED6 VAR word
REMAINDER7 VAR word
MEASURED7 VAR word
REMAINDER8 VAR word
MEASURED8 VAR word
REMAINDER9 VAR word
MEASURED9 VAR word

TRISA = %00000001
TRISB = %00000000
TRISD.0 = %0
TRISD.1 = %0
ADCON0 = %11000101
ADCON1 = %10000000


START:


ADCIN 0 , MEASURED_VALUE

REMAINDER0 = MEASURED_VALUE // 2
PORTB.0 = ~REMAINDER0
MEASURED0 = ( MEASURED_VALUE / 2 )

REMAINDER1 = MEASURED0 // 2
PORTB.1 = ~REMAINDER1
MEASURED1 = ( MEASURED0 / 2 )

REMAINDER2 = MEASURED1 // 2
PORTB.2 = ~REMAINDER2
MEASURED2 = ( MEASURED1 / 2 )

REMAINDER3 = MEASURED2 // 2
PORTB.3 = ~REMAINDER3
MEASURED3 = ( MEASURED2 / 2 )

REMAINDER4 = MEASURED3 // 2
PORTB.4 = ~REMAINDER4
MEASURED4 = ( MEASURED3 / 2 )

REMAINDER5 = MEASURED4 // 2
PORTB.5 = ~REMAINDER5
MEASURED5 = ( MEASURED4 / 2 )

REMAINDER6 = MEASURED5 // 2
PORTB.6 = ~REMAINDER6
MEASURED6 = ( MEASURED5 / 2 )


REMAINDER7 = MEASURED6 // 2
PORTB.7 = ~REMAINDER7
MEASURED7 = ( MEASURED6 / 2 )

REMAINDER8 = MEASURED7 // 2
PORTD.0 = ~REMAINDER8
MEASURED8 = ( MEASURED7 / 2 )

REMAINDER9 = MEASURED8 // 2
PORTD.1 = ~REMAINDER9
GOTO START


END

anyone can help me?pls...thank you

mister_e
- 5th March 2007, 22:49
i don't know what you expect, but it display something, it's just reverted. Let's say when the value=0, all i/o=1... but it's working.

anyways, here's a shorter method


DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

MEASURED_VALUE VAR word
TRISA = %00000001
TRISB = %00000000
TRISD.0 = %0
TRISD.1 = %0
ADCON0 = %11000101
ADCON1 = %10000000


START:
ADCIN 0 , MEASURED_VALUE
PORTB= MEASURED_VALUE.LOWBYTE
PORTD.0= MEASURED_VALUE.8
PORTD.1= MEASURED_VALUE.9
GOTO START

sixty9sandals
- 5th March 2007, 23:15
i don't know what you expect, but it display something, it's just reverted. Let's say when the value=0, all i/o=1... but it's working.

anyways, here's a shorter method


DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

MEASURED_VALUE VAR word
TRISA = %00000001
TRISB = %00000000
TRISD.0 = %0
TRISD.1 = %0
ADCON0 = %11000101
ADCON1 = %10000000


START:
ADCIN 0 , MEASURED_VALUE
PORTB= MEASURED_VALUE.LOWBYTE
PORTD.0= MEASURED_VALUE.8
PORTD.1= MEASURED_VALUE.9
GOTO START


hi..
i've try out ur code..when i supplied 1.5v as an input then the leds displayed 0100010010 instead of 100110011...why is this happen? any suggestion to solve it?

thanks a lot

mister_e
- 5th March 2007, 23:35
274 instead of 304... it's not as this far. Everything is related on acurracy of the VREF. IF it's REALLY 5.000000000V then you should have
(1.5/5)*1024=307

If my maths are good after 20 hours awake, ((307-274)/1024)*100=3.22 % of error. so your supply line should be around... 5-(3.22%)=4.9844 Volt

If i use a serial communication here and if i inject 1.499 volt, VDD=5.014, i should read... (1.499/5.016)*1024=306 right? i have 305 wich is real darn close.

If your ADC input is a little bit noisy, it may change the results, same thing with the VDD line. If you respect the minimum acquisition time and the maximum input impedance... you will have the nice results.

If you need accuracy, you may decide to use a precision voltage source.

mmm, maybe 1023 instead of 1024....

sixty9sandals
- 5th March 2007, 23:51
thanks a lot for ur help even after 20hours awake..its realy help me..possibly its caused by the noise..im gonna check it again..thanks a lot

sixty9sandals
- 6th March 2007, 09:11
i've check it..but the regulator input is 4.8V..is there posibbly coz by my wrong ADCON1 register?..

thank you

mister_e
- 6th March 2007, 16:43
Regulator INput or OUTput?

if it's output, the results make sense. use the previous calc i did.

If it's INput... and if it's a simple 7805... the output signal will be pretty bad :eek:

Sean_Goddard
- 8th March 2007, 19:23
If you have a noise problem, try setting the settling time on the ADC to a few microseconds, then initiating a conversion. At the same time, make the PIC SLEEP for around the same length of time as the conversion. This should SIGNIFICANTLY reduce the noise across the PIC silicon substrate and give you a lot better accuracy.