|
|
View previous topic :: View next topic |
Author |
Message |
pfo Guest
|
mcp3208 samples too slow (w. fast io and no delays in drvr)? |
Posted: Fri Feb 20, 2004 9:04 am |
|
|
hi!
i'm using a modified sw driver from CCS(all the delays taken out)
to read my mcp3208, an now i benchmarked the time it takes to convert one value.
this is the code used:
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000) // 20MHz OSC
#use rs232(baud=115400, xmit=PIN_C6, rcv=PIN_C7)
//#include <mcp3208.c>
#include "D:\pcc_work\drivers\MCP3208.C"
#use fast_io (B)
static int16 overflow;
static int16 value=0;
static int16 ticks;
#int_timer1
void tick_handler(void)
{
overflow++;
}
void main(void)
{
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
set_tris_b(0x40); // TRIS B ist aut 01000000 gesetzt -> B1 = input, restliches port B inst output
setup_timer_1 (T1_INTERNAL);
while(TRUE)
{
set_timer1(0);
value /= 4;
ticks=get_timer1();
printf("\r\ntime taken: %lu, ohne tick is 1.6uS, overflows: %lu ", ticks,overflow);
}
}
|
the output:
Quote: |
time taken: 511, one tick is 1.6uS, overflows: 0
time taken: 511, one tick is 1.6uS, overflows: 0
time taken: 511, one tick is 1.6uS, ocerflows: 0
|
this means that the resulting sample rate is approx.:
511*1.6 => 817.6 us / per sample
-> ~ 1200 samples/s
but the datasheet claims up to 100Ksmpls/s
how could i improve the performance, or is this the maximal sample rate that's achievable with the sw driver and the mcp? would hw SPI results look any better? |
|
|
Vector Research
Joined: 16 Feb 2004 Posts: 10 Location: Kelowna B.C
|
re:mcp3208 samples too slow (w. fast io and no delays in drv |
Posted: Fri Feb 20, 2004 12:50 pm |
|
|
i cant see in you code where you actualy get the value from the adc, however you also must take into concideration the time it takes to output you diagnostic string.. let see here..
set_timer1(0); 2 cycles
value /= 4; 6 cycles
ticks=get_timer1(); 4 cycles
printf("\r\ntime taken: %lu, ohne tick is 1.6uS, overflows: %lu ", ticks,overflow); 38 cycles
total 50 cycle, or 10 uS @ 200nS per cycle
also you cycle time assumption is wrong. With a Clock of 20 mhz, the internal clock will be 5mhz or .2uS per count assuming no prescale.. X 512 = 102.4uS
A common mistake when making when making mesurments of program run times, is not taking into concideration the time it takes to run the code that measures the run time.. Heisenberg's Uncertainty Principle applies to software also..
Vince |
|
|
pfo Guest
|
|
Posted: Sun Feb 22, 2004 7:48 am |
|
|
thx!
but i acoutally pasted the wrong code, the line value /= 4 should be replaced by read_analog_mcp();
Code: |
////////////////// Driver for MCP3208 A/D Converter ////////////////////////
//// ////
//// adc_init() ////
//// Call after power up ////
//// ////
//// value = read_analog_mcp( channel, mode ) ////
//// Read an analog channel ////
//// 0 through 7 and select ////
//// differential (0) or ////
//// single (1) mode ////
//// ////
//// value = read_analog( channel ) ////
//// Read an analog channel ////
//// 0 through 7 in single mode ////
//// ////
//// // MODIFIED by PFO for hihgest sample rate - all delays taken out 15.02.2004
#ifndef MCP3208_CS
#define MCP3208_CLK PIN_B0
#define MCP3208_DOUT PIN_B1
#define MCP3208_DIN PIN_B2
#define MCP3208_CS PIN_B3
#endif
void adc_init() {
output_high(MCP3208_CS);
delay_ms(1);
}
void write_adc_byte(BYTE data_byte, BYTE number_of_bits) {
BYTE i;
//delay_us(2);
for(i=0; i<number_of_bits; ++i) {
output_low(MCP3208_CLK);
if((data_byte & 1)==0)
output_low(MCP3208_DIN);
else
output_high(MCP3208_DIN);
data_byte=data_byte>>1;
// delay_us(50); //was 50
output_high(MCP3208_CLK);
// delay_us(50); //was 50
}
}
BYTE read_adc_byte(BYTE number_of_bits) {
BYTE i,data;
data=0;
for(i=0;i<number_of_bits;++i) {
output_low(MCP3208_CLK);
// delay_us(50); //was 50
shift_left(&data,1,input(MCP3208_DOUT));
output_high(MCP3208_CLK);
// delay_us(50); //was 50
}
return(data);
}
long int read_analog_mcp(BYTE channel, BYTE mode) {
int l;
long int h;
BYTE ctrl_bits;
// delay_us(20); // WAS 200 !!
if(mode!=0)
mode=1;
output_low(MCP3208_CLK);
output_high(MCP3208_DIN);
output_low(MCP3208_CS);
if(channel==1) // Change so MSB of channel #
ctrl_bits=4; // is in LSB place
else if(channel==3)
ctrl_bits=6;
else if(channel==4)
ctrl_bits=1;
else if(channel==6)
ctrl_bits=3;
else
ctrl_bits=channel;
ctrl_bits=ctrl_bits<<1;
if(mode==1) // In single mode
ctrl_bits |= 1;
else // In differential mode
ctrl_bits &= 0xfe;
ctrl_bits=ctrl_bits<<1; // Shift so LSB is start bit
ctrl_bits |= 1;
write_adc_byte( ctrl_bits, 7); // Send the control bits
h=read_adc_byte(8);
l=read_adc_byte(4)<<4;
output_high(MCP3208_CS);
return((h<<8)|l);
}
|
|
|
|
Guest
|
Re: re:mcp3208 samples too slow (w. fast io and no delays in |
Posted: Sun Feb 22, 2004 7:50 am |
|
|
Vector Research wrote: | i cant see in you code where you actualy get the value from the adc, however you also must take into concideration the time it takes to output you diagnostic string.. let see here..
set_timer1(0); 2 cycles
value /= 4; 6 cycles
ticks=get_timer1(); 4 cycles
printf("\r\ntime taken: %lu, ohne tick is 1.6uS, overflows: %lu ", ticks,overflow); 38 cycles
total 50 cycle, or 10 uS @ 200nS per cycle
also you cycle time assumption is wrong. With a Clock of 20 mhz, the internal clock will be 5mhz or .2uS per count assuming no prescale.. X 512 = 102.4uS
A common mistake when making when making mesurments of program run times, is not taking into concideration the time it takes to run the code that measures the run time.. Heisenberg's Uncertainty Principle applies to software also..
Vince |
but why the hell is the internal clock only 5MHz when i use a 20MHz crystal? |
|
|
pfo Guest
|
Re: re:mcp3208 samples too slow (w. fast io and no delays in |
Posted: Sun Feb 22, 2004 7:54 am |
|
|
Vector Research wrote: | i cant see in you code where you actualy get the value from the adc, however you also must take into concideration the time it takes to output you diagnostic string.. let see here..
set_timer1(0); 2 cycles
value /= 4; 6 cycles
ticks=get_timer1(); 4 cycles
printf("\r\ntime taken: %lu, ohne tick is 1.6uS, overflows: %lu ", ticks,overflow); 38 cycles
total 50 cycle, or 10 uS @ 200nS per cycle
also you cycle time assumption is wrong. With a Clock of 20 mhz, the internal clock will be 5mhz or .2uS per count assuming no prescale.. X 512 = 102.4uS
A common mistake when making when making mesurments of program run times, is not taking into concideration the time it takes to run the code that measures the run time.. Heisenberg's Uncertainty Principle applies to software also..
Vince |
i only wanted to know how long it takes to get one conversion, not the whole programm. this has _nothing_ 2 do with heisenberg's uncertainty principle ... |
|
|
Guest
|
Re: re:mcp3208 samples too slow (w. fast io and no delays in |
Posted: Sun Feb 22, 2004 8:22 am |
|
|
Vector Research wrote: | i cant see in you code where you actualy get the value from the adc, however you also must take into concideration the time it takes to output you diagnostic string.. let see here..
set_timer1(0); 2 cycles
value /= 4; 6 cycles
ticks=get_timer1(); 4 cycles
printf("\r\ntime taken: %lu, ohne tick is 1.6uS, overflows: %lu ", ticks,overflow); 38 cycles
total 50 cycle, or 10 uS @ 200nS per cycle
also you cycle time assumption is wrong. With a Clock of 20 mhz, the internal clock will be 5mhz or .2uS per count assuming no prescale.. X 512 = 102.4uS
A common mistake when making when making mesurments of program run times, is not taking into concideration the time it takes to run the code that measures the run time.. Heisenberg's Uncertainty Principle applies to software also..
Vince |
sorry, for the tripple post, bute i assumed that Timer1 hat a clock cycle of 1.6uS, i didn't mention the instruction cycle period ... |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
Sample too slow |
Posted: Sun Feb 22, 2004 12:07 pm |
|
|
The PIC hardware instruction clock is a divide by 4 derivative of the CLK0 (XTAL) freq (this is mentioned several places in the PIC data sheets).
I have alway use the data sheets and become familiar with the processor I am planning to use before I start working with it. This makes it much easier to understand the resources available and the coding required. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Sun Feb 22, 2004 12:59 pm |
|
|
I'm stating this from memory but I don't think you can get a single sample from the MCP320x parts in 10uS. But if you run it with a continuous clock (and read the data stream) you can get a sample every 10uS after the first.
I might be wrong or confusing the MCP320x with an Analog Devices or Burr-Brown ADC. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|
|
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
|