|
|
View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
ADC interval time |
Posted: Mon Dec 28, 2015 7:07 pm |
|
|
Hai,
I'm using ADC to take a 256 sample. I would like to know, how many microsecond is the interval time between each sample? How to calculate ?
Below is my code:
Code: |
#include <18F4550.h>
#device adc=10
#fuses H4,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define SAMPLES 256
//======================================
void main(void)
{
int8 i;
int16 results[SAMPLES];
setup_adc_ports(AN0);
setup_adc( ADC_CLOCK_DIV_16 );
set_adc_channel(1);
output_low(PIN_B0);
for(i = 0; i < SAMPLES; i++)
{
delay_us(1);
results[i] = read_adc();
output_toggle(PIN_B0);
}
for(i = 0; i < SAMPLES; i++)
{
printf("%lu \n\r", results[i]);
}
printf("\n\r");
while(1);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 28, 2015 7:38 pm |
|
|
Setup an infinite loop. Put a scope on Pin B0 and measure the pulse width. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Mon Dec 28, 2015 8:17 pm |
|
|
The hardware side of me has to agree with PCM P, easy to do if you have a scope...
The software side of me says to dump the listing, grab the instruction set in datasheet of the PIC and manually calculate the time based on instruction execution.
If you 'play computer' and do the calculations you'll have a better appreciation as to what a PIC does and may be able to 'tweak' your code to get faster operation !
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Dec 29, 2015 1:55 am |
|
|
There are things 'screaming' at me about this code.
'H4'.
This was only present, on the very earliest releases of compiler supporting the 4550. Before CCS got their heads round the extra complexity of the timing setups needed to get the clocking to work correctly on these chips. Did a quick look back through my archives, and I have to go back to 2005, to find a compiler that uses this setting. Looking around there I've got versions I have tweaked myself to get the chip to work right at this time. So immediately, things looking a bit dangerous. Now looking at what these compilers 'did', the chip is unlikely to actually be running at 20MHz. Selecting H4, actually sets the fuses to no prescaler, CPU fed from PLL, PLLDIV1, CPUDIV1. Now there is no legal route by which this will give the CPU at 20Mhz.....
There is also a separate issue. The delay being allowed for Tacq, is only just about enough, if the source impedance feeding the ADC is zero. For a more typical 2.5KR source, the ADC _requires_ a minimum of 6.4uSec acquisition time between successive samples. The write to the array probably gives perhaps 2uSec, and the loop and delay, perhaps another two, but the timing is going to be very close to being less than the ADC actually needs....
So is the serial out working?. For real?. I'd suspect a Proteus project, which is not capable of knowing that this is an illegal clock setting.
So the poster needs to go and do the most basic 'flash an LED' test, and know what speed the CPU really is working. If they must stay with the old compiler, they are going to have to manually setup the fuses to get correct operation (this was what I did), or switch to a legal combination (for instance, 4MHz crystal, with the posted settings will give the CPU running at 48MHz). Preferably update to a compiler that is rather less than 10 years old.... |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Thu Dec 31, 2015 5:53 pm |
|
|
Hi Ttelmah
Thank you for the H4 explanation. Originally I get this test program from PCM programmer, however I just made a few modification with it.
Code: |
#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
|
After I change this code, is the acquisition time between successive samples still 6.4uSec or different? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Jan 01, 2016 1:31 am |
|
|
With any chip/clock setting, your first program should be the 'flash an LED' test, and verify your chip is running as you expect.
The Tacq, is unaffected by the clock rate. It is dependant on the impedance of your source feeding the ADC, and the internal impedances of the chip. The 6.4uSec, is the recommended figure from the data sheet for a source who's impedance is not above 2.5KR. _Read the data sheet_. It is _essential_ before you use the chip. |
|
|
|
|
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
|