View previous topic :: View next topic |
Author |
Message |
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
ADC on 18f2420... |
Posted: Wed Jul 31, 2013 6:15 am |
|
|
Hi,
I can't read a correct value with the adc...My code is extremely basic, i don't understand...
Code: |
#include <18F2420.H>
#device ADC=10
#FUSES INTRC_IO,MCLR,NOWDT,NOWRT,NOLVP
#use delay(internal=8000000)
#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7,parity=N,bits=8,stop=1)
//----------------------------------------------------------------
void main()
{
int16 value;
setup_oscillator(OSC_8MHZ);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0|VSS_VDD);
set_adc_channel(0);
for(;;)
{
value=read_adc();
printf("Value : %lu\n\r",value);
delay_ms(500);
}
}
//----------------------------------------------------------------
|
If someone see a big mistake... thanks in advance
CCS version 4.130 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Jul 31, 2013 6:36 am |
|
|
here's one...
setup_adc(ADC_CLOCK_INTERNAL);
...
According to typical ADC setup specs in datasheets, 'internal' is NOT recommended any speed over 1MHZ(unless in sleep mode).
Fine the ADC section of your PIC and there should be a chart with 'adc clock vs PIC speed'.some will be highlighted meaning 'do not use'.
hth
jay |
|
|
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
|
Posted: Wed Jul 31, 2013 6:59 am |
|
|
Hi,
You have right ! (Page 229)
I have replace
Code: |
setup_adc(ADC_CLOCK_INTERNAL);
|
by
Code: |
setup_adc(ADC_CLOCK_DIV_16);
|
But not better... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Jul 31, 2013 9:13 am |
|
|
What's the ADC connected to ?
A basic test would be a simple voltage divider from Vss to Vdd, with a small 'filter' cap (say 1mfd), maybe giving say 2 volts input (measure with DVM).
'play' computer, do the math to see if the ADC reading is correct.
Also test at zero volts and Vss ( +5 ?).
You should read a fairly stable reading ( +-2-3 bits).
hth
jay |
|
|
|