How long does readadc command take?

cloudy

Member
Can anyone estimate how quickly a readadc10 command is able to sample? The pic is an 18M2 running at 8mhz. See code below, any idea of the frequency of sampling?


label1:

readadc10 B.2,w0
if w0 > 50 then goto label1
' else continue code

James
 
Last edited:

hippy

Ex-Staff (retired)
A rough estimate would be 375us for the READADC10 and 625us for the IF-THEN, a total of 1000us, 1ms - so a sampling frequency about 1kHz.

This is based on an estimate of 125us per 'token'. I haven't measured to see how accurate that is.
 

cloudy

Member
Fantastic, more than enough - thank you

Is this documented anywhere? I had a look but could not see anything...

James
 

Milos

Member
I tested it on my 28X2-5V@8MHz (AXE200 module, default speed) and it was about 2.1 msec. There was no difference between readadc and readadc10. I measured it reading timer before and after. Some time was spent on reading the timer, of course.
 

hippy

Ex-Staff (retired)
It's not documented extensively as it's very hard to give meaningful values for any PICAXE timing. You'll find past discussion on this in the forum and westaust55 has recently set about collating timing information into a single document.

Measuring the loop time on an 18M2, the result I got was around 960us so pretty close to guesstimate.
 

westaust55

Moderator
If you have a look at the document I posted here:
http://www.picaxeforum.co.uk/showthread.php?t=17782
In particular post 8 you will see how long various Picaxe commands take to execute. Still a close approximation as command placement in program memory will also have an influence.
The data in the table near the end is for an 18M2 at 4 MHz so around 310 to 350 used is the order of magnitude at 8 MHz.
 
Last edited:

hippy

Ex-Staff (retired)
@ Milos : I measured the loop on a 28X2 ( which should be comparable with 28X2-5V ) and it came up as 1070us. This perhaps shows it depends on how one measure things and how good the adjustment is to take out the time taken to do the actual measuring.

I measured by adding two TOGGLE commands on different pins (1520us), then taking one out (1295us). The time for a single TOGGLE is therefore 1520us - 1295us = 225us so the time with none is around 1295us - 225us = 1070us.

Results generally point to the rule of thumb execution of a simple command ( HIGH, LOW, TOGGLE ) to be 250us at default speed, and 125us per code 'token' which is what I do my guesstimates on.
 

westaust55

Moderator
The data I have suggests that for the same clock speed, the X2 parts take around 1.5 times the duration for the 18M2 and some other chips.
Likely due to the longer command tokens and complexities of greater versatility.
 

cloudy

Member
While on the subject of timings, I also have need to surround this adc read loop with a timeout (length in the 200 ms region) I've been told that timer1 counting in ms blocks will lose minor ticks and therefore be pretty inaccurate - is there a better way of doing this? Can I set set the major ticks to be every 200ms then just watch for the timer reaching 1 tick?

settimer off
settimer 65505 'Ticks in ms?

label1:

do while timer1 < 200
readadc10 B.2,w0
if w0 > 50 then goto label1
loop

'continue code
 
Last edited:

cloudy

Member
Been studying that intensely! 65505 was the preload someone else posted on here for 1ms ticks at 8mhz... Is the settimer off not correct for "resetting?"

James
 

inglewoodpete

Senior Member
Been studying that intensely! 65505 was the preload someone else posted on here for 1ms ticks at 8mhz... Is the settimer off not correct for "resetting?"

James
The command sequence is right. 100 forum members could tell you or you could work it out for yourself. At 8MHz, 1 minor tick = 32 uS.... The major tick will be set to.... and you will get interrupts every....
 

cloudy

Member
1 ms = 1000us 1000/32 = 31.25 therefore approx 31 minor ticks per ms.
Preload = 65535 - 31
Preload for 1ms ticks therefore = 65504 ?

I'm not looking to generate interupts, this code is already invoked by a pin high interrupt. Don't think I've just posted for the easy answer - I've spent the last 3 hours searching the forum and reading documentation but just can't get my head around it!

James
 

inglewoodpete

Senior Member
1 ms = 1000us 1000/32 = 31.25 therefore approx 31 minor ticks per ms.
Preload = 65535 - 31
Preload for 1ms ticks therefore = 65504 ?

I'm not looking to generate interupts, this code is already invoked by a pin high interrupt. Don't think I've just posted for the easy answer - I've spent the last 3 hours searching the forum and reading documentation but just can't get my head around it!

James
Good to hear:). Yes, you're partly right. How will your program know when the (background) timer has expired if you don't use a timer interrupt?

To help you understand the timer, have a look at a simple timer demonstration I posted a year ago (post #4): http://www.picaxeforum.co.uk/showthread.php?t=14475

You just need 2 LEDs and their limiting resistors. Then you can experiment. You'll need to recalculate you value for 8MHz of course.
 

cloudy

Member
I was hoping to query the timer value during my goto loop - if the value is less than my preload then the timer must have major ticked and looped round - perhaps that's not a bombproof statement!

Basically all I want to do is check readadc for 200ms, if the adcvalue is a certain one then bail out of the 200ms loop early....

James
 

westaust55

Moderator
Very interesting doc - although I don't often "chase bytes" in programs it should be useful if I need to.

Might be worth picking the bones out of http://www.picaxeforum.co.uk/showthread.php?t=13262 and adding them at the end.

(and running a spell checker on the doc ;))
Thanks Martin,
Texasclodhopper has already done a proofread and proffered some corrections. With some extra words I have written, a new version will be uploaded soon.
 

inglewoodpete

Senior Member
I was hoping to query the timer value during my goto loop - if the value is less than my preload then the timer must have major ticked and looped round - perhaps that's not a bombproof statement!

Basically all I want to do is check readadc for 200ms, if the adcvalue is a certain one then bail out of the 200ms loop early....

James
Yes, you could do that. It may be simpler to just have a loop counter and exit if you get an ADC ready within the right range or when the loopcounter expires.

On the other hand, there is the TOFlag bit, which gets set when the timer expires. So your loop could check for this.
 
Top