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

ADC sampling

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



Joined: 09 Jun 2009
Posts: 13

View user's profile Send private message

ADC sampling
PostPosted: Tue Jun 09, 2009 4:06 pm     Reply with quote

hi, I'm newbie with this and my english isn't the best but hope I can learn something with you.

the question is, I'm using PIC18F2580 and want to do an ADC with 8 bits, but my problem is that I want to have an ADC with 1kHz sample rate = 1000 samples per second. I already made this code,
Code:
#include <18F2580.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main()
{   
  int valueX, valueY;
  setup_adc_ports( AN0_TO_AN1_ANALOG | VSS_VREF  );
  setup_adc( ADC_CLOCK_DIV_32 );

 
  while(1){
 
      set_adc_channel( 0 );
      valuex = read_ADC();
      delay_us(10);
     
      set_adc_channel( 1 );
      valueY = read_ADC();
      delay_us(10);
     
      printf("ValueX: %u || ValueY: %u", valueX, valueY);
  }
}

What should I do to have 1000 samples per second? and what about the
Code:
setup_adc( ADC_CLOCK_DIV_32 );

what means the ADC_CLOCK_DIV_32? Means that the ADC take 20Mhz/32=1.6us to convert the voltage?

Thanks in advance Wink
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 4:13 pm     Reply with quote

First of all read the datasheet for your PIC, if it is capable of doing that or not.

What will you do with those samples? If you'll write them to LCD there is no point in 1k samples per sec.
Macas



Joined: 09 Jun 2009
Posts: 13

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 4:24 pm     Reply with quote

Thanks for your help but I already read it. I want to send the samples via CAN bus in order to manage a vehicle acceleration.
Macas



Joined: 09 Jun 2009
Posts: 13

View user's profile Send private message

PostPosted: Wed Jun 10, 2009 7:24 am     Reply with quote

bump!
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Jun 10, 2009 9:30 am     Reply with quote

I don't know much about the CAN bus but to simply read 1000 samples the code could be done two ways. The simplest is:

Read first channel
Read second channel
Do something with the data
Wait until the next millisecond
loop to the start.

This requires you to find how long the first three steps will take and be careful that it always takes the same time.

The second way is to set up an interrupt once each millisecond using a timer. For good form the interrupt should set a flag. The main loop polls this flag and when it finds the flag set it clears the flag, takes the readings and processes the data. Then it goes back to polling the flag.
_________________
The search for better is endless. Instead simply find very good and get the job done.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jun 10, 2009 9:49 am     Reply with quote

Another idea is to set a timer for 0.5ms. Everytime it overflows you take an adc reading, then change the adc channel, and keep track of the overflow count. This way you are taking a reading every 0.5ms but on alternating adc channels (zero and one in your case) so your sampling rate per channel is 1ms apart or 1Khz.
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Jun 10, 2009 1:34 pm     Reply with quote

I feel sure dumping 1000 samples per second on the CAN bus in the vehicle will adversely impact the car system and may swamp the ECM!
_________________
Google and Forum Search are some of your best tools!!!!
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