PDA

View Full Version : how to read 10 bit value of adc in pic 16f877a



asifiqbal
- 1st November 2014, 12:07
hello everyone I have this code and tried it and its working perfectly to read 8 bit value of an0 ,it shows 0 to 255 value on lcd but i am not being able to read 10bit value of channel please help
To derieve 8 bit value my code is
device is 16f877a



CLEAR
DEFINE OSC 4
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
DEFINE LCD_LINES 2
DEFINE LCD_BITS 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50


Low porte.2
pause 500


DEFINE ADC_BTTS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50


TRISA = %11111111
TRISD = %00000000
ADCON1 = %00000111 'set port a digital

A2D_VALUE0 VAR byte


LCDOUT $FE, 1
MAINLOOP:
ADCIN 0, A2D_VALUE0


LCDOUT $FE, $80, DEC A2D_VALUE0,
PAUSE 10
GOTO MAINLOOP
END


For ten bit value my code is below but it still shows 8 bit value ,please guide me to generate 10 bit value of AN0 channel



CLEAR
DEFINE OSC 4
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
DEFINE LCD_LINES 2
DEFINE LCD_BITS 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50


Low porte.2
pause 500


DEFINE ADC_BTTS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50


TRISA = %11111111
TRISD = %00000000
ADCON1 = %00000111 'set port a digital

A2D_VALUE0 VAR WORD


LCDOUT $FE, 1
MAINLOOP:
ADCIN 0, A2D_VALUE0


LCDOUT $FE, $80, DEC A2D_VALUE0,
PAUSE 10
GOTO MAINLOOP
END

HenrikOlsson
- 1st November 2014, 13:36
DEFINE ADC_BTTS 10 should be DEFINE ADC_BITS 10 (the typo is there in the 8-bit version of the code as well.

If that alone doesn't do it then try changing left/right justification (ADCON1.7). I can never remember when to use what....

/Henrik.

Amoque
- 1st November 2014, 14:03
As I always do, let me preface by saying I'm a noob with little surety, a modest desire to help, and an immense desire learn. It is my hope that by making a sincere effort to solve your problem, it will be one I do not have to make for myself :)

Looking at page 128 of the datasheet, it would appear your ADCON1 register is bunged:

Your setting: ADCON1 = %000111, appears to me to left justify the two high bits with ADCON1.7 = 0; I should think it would be easier to manipulate an ADC value with all the bits right justified (ADCON1.7 = 1). Also, the right bits (0-3) are A/D port configuration control bits - they set which channels are ADC, which are the Vref+/- channels, and which are digitals. There are two settings which RESEMBLE your configuration:

'011X', which disables ADC (making all channels digital) and '1111' which sets AN0 as analog, AN2 as vRef- and AN3 as vRef+. This appears to be what you want...

So, if my evaluation is correct, 001111 is the correct ADCON1 register configuration setting for 1 ADC channel, 10 bit resolution, right justified. Do you read it this way as well?

Actually, in better looking at the choices (doh) it appears that 001110, sets AN0 analog - without vRefs. Time to edit had expired...