PDA

View Full Version : 12Bit ADC with PIC18F2523



Kuba230
- 13th May 2016, 06:22
Hello,
I have code for 10 bit ADC which is normally working with PIC18F2455 and I want to use this code for 12 bit ADC with PIC18F2523.
I changed Define ADC_BITS 10 to ADC_BITS 12, but results are sill 10 Bits.
I using 20 MHz external oscillator.
How to configure this code for 12 bit ADC with PIC18F2523 ?

Thank you


Here is my code:

Define LOADER_USED 1
DEFINE OSC 48

' Define LCD registers and bits
Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTC
Define LCD_RSBIT 7
Define LCD_EREG PORTC
Define LCD_EBIT 6
DEFINE LCD_BITS 4 ' 4-bit data bus
DEFINE LCD_LINES 2 ' LCD - 2 lines


' Define ADCIN parameters

Define ADC_BITS 12 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS


adval var word ' Create adval to store result

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00001010 ' Set PORTA analog...
ADCON2 = %10000000 ' ...and right justify result

Pause 500 ' Wait .5 second

loop: ADCIN 0, adval ' Read channel 0 to adval
Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value
Pause 100 ' Wait .1 second
Goto loop ' Do it forever
End

richard
- 13th May 2016, 07:38
ADCON2 = %10000000 ' ...and right justify result


how many times do we see this totally wrong statement


ADCON2 = %10000000 ' ...and right justify result and set the conversion clock to fosc/2
which is way out of limits for a 48mhz osc and contradicts the define
Define ADC_CLOCK 3 ' Set clock source (3=rc)


why not just
ADCON2.7=1
or
ADCON2 = ADCON2 | %10000000 ' ...and right justify result


other than that i have nothing , never tried a 12 bit adc chip.
you may need to do it manually

Kuba230
- 13th May 2016, 08:16
Hello richard,
I configured my code for 12 Bit AD, now I get 12 Bits results, but it is working only with 4MHz external oscillator.
When I replace extenal oscillator wit 20MHz and DEFINE OSC 20, it is not working, what is wrong? :confused:


Define LOADER_USED 1
DEFINE OSC 4

' Define LCD registers and bits
Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTC
Define LCD_RSBIT 7
Define LCD_EREG PORTC
Define LCD_EBIT 6
DEFINE LCD_BITS 4 ' 4-bit data bus
DEFINE LCD_LINES 2 ' LCD - 2 lines


' Define ADCIN parameters

Define ADC_BITS 12 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS


adval var word ' Create adval to store result

advalLow VAR WORD ' make it available to use in PBP
advalHigh VAR WORD ' make it available to use in PBP
advalLow = ADRESL
advalHigh = ADRESH


TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00001010 ' Set PORTA analog...
ADCON2.7=1 ' ...and right justify result

Pause 500 ' Wait .5 second

loop: ADCIN 0, adval ' Read channel 0 to adval
adval = ADRESL + (ADRESH * 256)
Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value
Pause 100 ' Wait .1 second
Goto loop ' Do it forever
End

richard
- 13th May 2016, 08:46
in what way does it not work ?
where and what are your config settings

advalLow and advalHigh only need to be bytes

advalLow VAR byte ' make it available to use in PBP
advalHigh VAR byte ' make it available to use in PBP


since ADRESH and ADRESHL are sequential in memory for that chip its possible to save a bit of typing by :-


@my_adresult =ADRESHL
my_adresult var word ext

;.............


ADCIN 0, adval ' Read channel 0 to dummy adval
adval = my_adresult

Kuba230
- 13th May 2016, 09:19
Ok,
I directly saving the ADRESL and ADRESH register which is used to store the end results of an analog-to-digital conversion (ADC) to results adval and sending LCD. My problem is, the code is working properly only with 4MHz oscillator, when I define 20MHz occillator, the conversion is very slow, waiting few second, when start conversion.
Whay the conversion is fast for 4Mhz oscillator and very slow for 20MHz oscillator

adval = ADRESL + (ADRESH * 256)

and sending LCD

Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value

richard
- 13th May 2016, 09:26
where and what are your config settings ?

the default osc is 1mhz which may be the problem

Kuba230
- 13th May 2016, 09:39
config setting:


DEFINE OSC 20

richard
- 13th May 2016, 09:50
DEFINE OSC 20 does nothing but tell the compiler to base timed sequences on that assumption

if you need an external xtaL or anything eles then the config fuses need to set appropriately
eg

;----[18F2523 Hardware Configuration]-------------------------------------------
#CONFIG
CONFIG OSC = HS
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRT = OFF
CONFIG BOREN = SBORDIS
CONFIG BORV = 3
CONFIG WDT = ON
CONFIG WDTPS = 512
CONFIG CCP2MX = PORTC
CONFIG PBADEN = OFF
CONFIG LPT1OSC = OFF
CONFIG MCLRE = ON
CONFIG STVREN = ON
CONFIG LVP = OFF
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG

Kuba230
- 13th May 2016, 12:16
richard,
you are true, problem was in config fuses.
Default configuration is: OSC = XT, after change to: OSC = HS is OK:)


richard, thank for your help :)