CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

setting the adc clock in pic18f452 pcb 4.110

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
louwi_138



Joined: 17 Nov 2012
Posts: 23

View user's profile Send private message

setting the adc clock in pic18f452 pcb 4.110
PostPosted: Fri Nov 29, 2013 12:56 am     Reply with quote

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?
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Fri Nov 29, 2013 1:52 am     Reply with quote

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?.
louwi_138



Joined: 17 Nov 2012
Posts: 23

View user's profile Send private message

PostPosted: Fri Nov 29, 2013 8:07 pm     Reply with quote

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);
   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9170
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Nov 29, 2013 8:26 pm     Reply with quote

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
louwi_138



Joined: 17 Nov 2012
Posts: 23

View user's profile Send private message

PostPosted: Fri Nov 29, 2013 9:40 pm     Reply with quote

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.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Fri Nov 29, 2013 11:22 pm     Reply with quote

You have to wait after changing the channel before you can read the ADC. Check your datasheet to see the minimum wait time.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Sat Nov 30, 2013 12:26 am     Reply with quote

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.
louwi_138



Joined: 17 Nov 2012
Posts: 23

View user's profile Send private message

PostPosted: Sat Nov 30, 2013 12:36 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group