MCP3208 Single acquisition time
TCP71
Posts: 38
I'm trying to get 100 samples from a single channel of the MCP3208 using the following code:
PRI GetHP | C
C := 0
repeat
hp[C] := ADC.in(0)
C++
while C < 100
The number of samples is less important than the time over which they are acquired. I'm trying to discover how long, in time, the above code will acquire. Is there a known length of time a single "ADC.in(0)" type cycle takes?
Thanks.
PRI GetHP | C
C := 0
repeat
hp[C] := ADC.in(0)
C++
while C < 100
The number of samples is less important than the time over which they are acquired. I'm trying to discover how long, in time, the above code will acquire. Is there a known length of time a single "ADC.in(0)" type cycle takes?
Thanks.
Comments
easy to measure empirically
_start := cnt
hp[C] := ADC.in(0)
_stop := cnt
time := _stop - _start
compare this time with
_start := cnt
_stop := cnt
time := _stop - _start
to extract the execution time of command hp[C] := ADC.in(0)
If the systemcounter cnt has wrapped around from max to zero between _start and _stop the result will be wrong
To avoid this you can use
a calculation like in the method below
best regards
Stefan
TCP71, what rate do you want to sample at? There are objects already for that chip that sample nice and fast. There is a MCP3208_fast_multi somewhere.
The first type runs constantly in the background, cycling thru the requested ports. It always returns the 'current' value of the adc port. If you call this repeatedly, you may end up getting duplicate adc values, because it hasn't had time to complete the next iteration between calls. These values may skew your average calculation.
The second type of driver calculates a new value each time it is called. This is the true time needed to retrieve a value from the chip. So if you call this repeatedly, you will get a new value each time. This will give you a true average over a number of samples. I believe the time to calculate a sample is around 6000 machine cycles.
I can give you examples of each type of driver if you're interested.
Jim
as long as you simply watch for a difference of just a second everything stays alright.
Try the different repeat-loops in the following code and watch the resulting values for at least a minute.
You will see that at some point the values turn negative and that they get smaller and smaller.
I tried different versions (calling the method elapsed-time with and without caclculating milliseconds, using local and global variables
but with all these versions if you wait long enough the values turn negative.
If you or somebody else can explain to me why this happends only after a certain amount of seconds I appreciate it very much.
best regards
Stefan