View previous topic :: View next topic |
Author |
Message |
louwi_138
Joined: 17 Nov 2012 Posts: 23
|
setting the adc clock in pic18f452 pcb 4.110 |
Posted: Fri Nov 29, 2013 12:56 am |
|
|
Hi,
I'm running my pic18F452 with an oscillator 10MHZ and I activate the pll which makes the pic run at 40Mhz.
But I noticed that the adc converter is not working well. I'm reading the analog data from 5 Sharp sensors which give a voltage between 0V and 3V % to the distance of an obstacle.
In fact it takes too much time to detect obstacle and sometimes it don't take it.
I did some search on the net and I heard speaking about adc clock and the time that it takes the adc to convert the data.
Actually I'm using Code: | setup_adc(adc_clock_internal); |
what should I put as adc_frequency to get my data ? and how can I calculate it? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Fri Nov 29, 2013 1:52 am |
|
|
Data sheet, data sheet, data sheet.....
For your chip (assuming you have the current data sheet)
Section 17-2.
Table 17-1
Seriously consider changing to a more modern chip. The 4520, is a direct replacement for your chip, cheaper, and allows twice the ADC clock rate to be used.
Also as a further comment, does the impedance of your source meet the requirements in section 17-1, and are you doing number '3' in the paragraph before?. |
|
![](templates/subSilver/images/spacer.gif) |
louwi_138
Joined: 17 Nov 2012 Posts: 23
|
|
Posted: Fri Nov 29, 2013 8:07 pm |
|
|
It's always the same problem :
Code: | #include <18f452.h>
#DEVICE ADC=8
#FUSES H4 //This is HS, with 4* PLL - H4 config bit.....
#fuses NOWDT
#use delay(clock=40M)
void config_adc(){
setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
setup_adc(ADC_CLOCK_DIV_64);
set_adc_channel(0);
}
int8 dist(int8 a){
set_adc_channel(a);
return(read_adc());
}
void main(){
set_tris_a(0xFF);
config_adc();
while(1){
if( (dist(0) > 80) || (dist(1) > 80)|| (dist(2) > 80)|| (dist(3) > 80) || (dist(4) > 80) ) output_high(Pin_b0);
else output_low(Pin_b0);
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Fri Nov 29, 2013 8:26 pm |
|
|
Have you run a simple 'read adc and display the data' program ?
Just a simple program to read each sensor and send to PC or LCD to confirm the sensors do work as expected?
You could easily have a wiring error,EMI,faulty sensor.
Start with one sensor, confirm it's correct,add another,test and continue until all 5 are working correctly.
Also how do you know what B0 is ? Using a DVM,LED or oscilloscope?
Without a delay(say 100ms) in the test-display loop it'll be very,very hard to confirm 'proper' operation.
and of course before all this, have you run the '1Hz blinking LED' program to confirm the PIC is wired and running correctly?
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
louwi_138
Joined: 17 Nov 2012 Posts: 23
|
|
Posted: Fri Nov 29, 2013 9:40 pm |
|
|
temtronic wrote: | Have you run a simple 'read adc and display the data' program ?
Just a simple program to read each sensor and send to PC or LCD to confirm the sensors do work as expected?
You could easily have a wiring error,EMI,faulty sensor.
Start with one sensor, confirm it's correct,add another,test and continue until all 5 are working correctly.
Also how do you know what B0 is ? Using a DVM,LED or oscilloscope?
Without a delay(say 100ms) in the test-display loop it'll be very,very hard to confirm 'proper' operation.
and of course before all this, have you run the '1Hz blinking LED' program to confirm the PIC is wired and running correctly?
hth
jay |
I did tested my code with one sensor it's work fine the led blink when I'm near the sensor and get off when I'm away.
the problem appear when I getting switching between the channel.
I lockeed on the net and I heard speaking about adc_frequency I make the same one in the datasheet div_by_64 and always the same problem. |
|
![](templates/subSilver/images/spacer.gif) |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Nov 29, 2013 11:22 pm |
|
|
You have to wait after changing the channel before you can read the ADC. Check your datasheet to see the minimum wait time.
Regards |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Sat Nov 30, 2013 12:26 am |
|
|
As I said. '3' in the paragraph I pointed you at.
When you select an ADC channel, you have to wait for Tacq, _before_ starting the reading. You are not doing so. It wouldn't have worked properly at the slower clock rate, but with the faster rate, it gets worse.
I see Alan has made the same point. |
|
![](templates/subSilver/images/spacer.gif) |
louwi_138
Joined: 17 Nov 2012 Posts: 23
|
|
Posted: Sat Nov 30, 2013 12:36 am |
|
|
alan wrote: | You have to wait after changing the channel before you can read the ADC. Check your datasheet to see the minimum wait time.
Regards |
yes it was that the problem. thank you. |
|
![](templates/subSilver/images/spacer.gif) |
|