View previous topic :: View next topic |
Author |
Message |
Macas
Joined: 09 Jun 2009 Posts: 13
|
ADC sampling |
Posted: Tue Jun 09, 2009 4:06 pm |
|
|
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 |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Tue Jun 09, 2009 4:13 pm |
|
|
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
|
|
Posted: Tue Jun 09, 2009 4:24 pm |
|
|
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
|
|
Posted: Wed Jun 10, 2009 7:24 am |
|
|
bump! |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jun 10, 2009 9:30 am |
|
|
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
|
|
Posted: Wed Jun 10, 2009 9:49 am |
|
|
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: 1933 Location: Norman, OK
|
|
Posted: Wed Jun 10, 2009 1:34 pm |
|
|
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!!!! |
|
|
|