A to D to A inefficiency

matchbox

Senior Member
Hi everyone,
I would like some advice with respect to, getting the highest speed from a Analog to Digital to Analog code.
e.g. going analog to analog, in the case of a sensor directly driving a PWMOUT function to give high resolution and fast processing time, is ideal in some cases. Although it gives very little control over the process and doesn't seem totally useful.
Is there a faster way to get the control that digital gives?? Without having to write down tens of even hundreds of lines of code to give the resolution that you would get with A to A.
A to D to A obviously slows down the processing time when it has to scan many lines of code to select an output, but it does give very good control interaction over every point in the analog output.

I don't have any program in mind here. I have just found this an issue over the years and wonder if anyone has found ways to create short cuts. So that processing time is still fast and the resolution is also good. I would appreciate and code examples.

Thanks
 

hippy

Technical Support
Staff member
Not sure exactly how you mean but to translate an analogue to analogue mapping you could use a Data Eeprom lookup table, something like ...

Code:
SetFreq M64
PwmOut PWM_PIN, 255, 0
Do
  ReadAdc ADC_PIN, b2
  Read b2, w0
  w0 = w0 * 4
  PwmDuty PWM_PIN, w0
Loop
 

Goeytex

Senior Member
I am totally clueless as to what you are wanting based upon your explanation. Can you possibly word it in a different manner ?
 

matchbox

Senior Member
Ill try to explain it a bit better. The type of analog to analog code you displayed; i have used and it is simple and fast, but i don't find it very useful for command controlled output. It is more of a closed loop control. What i was getting at.... was to be able to use the speed and resolution of A to A code(like you posted) and being able to interject into that code to have digital control over the process. Does that make scense?
 

Buzby

Senior Member
I too am struggling to understand exactly what you want to do.

Is is like this ?.


1 Convert an Analogue signal to Digital.

2 Process the Digital somehow.

3 Convert the Digital to Analogue.

4 Repeat.
 

matchbox

Senior Member
The code that Hippy left gives 0.1% resolution and does so at a very fast rate. Its ideal for perfect closed loop PWM control. But what if i was using a bi-directional hall sensor for example 0-512-1023. For the anolog input. What if i need to change the sensors read direction. Or then stop the process in the middle of its cycle and then restart it somewhere else in that cycle. I know it can be done, but it slows things down and i haven't found a simple way of doing this without slowing the whole output time down.
 

BeanieBots

Moderator
I too am confused about the question.
I'm guessing you want some sort of closed loop control such as a PID controller??
Please let us know what problem needs to be solved.
For example, are you trying to emulate an integrator or a differentiator.
Perhaps you are trying to do real RMS measurements?
Please give a detailed example of what you want solved.
It may well be that op-amps (or a dsPIC) are the only solution to your problem but until we know what the problem is (in detail) we cannot advise.

There are two fundamental restrictions be it digital or analogue.
Bandwidth and resolution.
Resolution is 10-bit on a PICAXE. Infinite (in theory) in analogue but can be expressed as a function of accuracy.
Bandwidth depends on the algorythm being performed in digital.
This can easily drop to sub Hz on a PICAXE because it is interpreted BASIC.
In analogue, it can be GHz but in practice should be limited by good filter design to be less than a decade above what is required by the system loop.

So, analogue will usually give better performance in fast loops but requires a hardware change to make any adjustments.
 

Goeytex

Senior Member
I am not sure that I understand your usage of the term "analog to analog". I would not label the code that hippy posted as "analog to analog". We have an analog signal, a voltage, and we convert that to a number, with the A/D. Then we use that number to to create a PWM signal, which is also digital. How is this analog to analog code?

Perhaps if you provide a real world scenario. What is the sensor? What is the "command control source? What is being controlled? What speed / resolution is needed?
 

BeanieBots

Moderator
Missed your earlier post whilst typing.
Please give an example equation and required bandwidth.
Fast & slow are too vague to give a good response to. (You can make it faster by increasing clock speed).
 

matchbox

Senior Member
I know im sounding a bit arbitrary in my explaination. Its just hard for me to put the type of control i require into words. Its not for anything conventional. But it uses a bi-directional DC with varying flux and potential changes throughout the cycle. Therefore the PWM control has to be very interactive with the Hall sensors and fast aswell.
 

matchbox

Senior Member
Thats right Beanie. Thats what im getting at. Im using a 20M2 at 32Mhz and its still to slow:( What you said is right Goeytex; I should have worded it more accurately.
 
Last edited:

matchbox

Senior Member
My main goal is to get the whole thing running as fast as i can (high micro second range at slowest) and seeing if a picaxe will perform the task. Resolution needs to be at least 1% and noise isn't to much of an issue. But the general accuracy has to remain within 4% of the feedback point.
What i was hoping for was some code snippets from someones previous experience in dealing with a similar type of fast interactive control loop program. I thought, by just seeing something that someone else had written might give me an idea that i had not thought of.
 

Goeytex

Senior Member
What does "high microsecond range" mean ? Need to be more specific.

At 32 MHz, each Picaxe BASIC command will take a minimum of about 70us to execute. Depending upon the command, it could be more or less.
 

premelec

Senior Member
@matchbox... Digital Signal Processing comes to mind - as does analog computing - likely the PICAXEs are too slow for what you want but DSP is not.
 

hippy

Technical Support
Staff member
I know im sounding a bit arbitrary in my explaination. Its just hard for me to put the type of control i require into words. Its not for anything conventional.
Can you point to any existing example similar to what you want to replicate, any research papers, web pages or the like ?
 

matchbox

Senior Member
When i said, High micro second range. That is as slow as i want it to run. Even a readadc command takes an average of 100uS at 32Mhz. So with what i require of this code; its unrealistic to expect it to run any faster than 700uS.
Permelec, an analog signal processor isn't ruled out.
Hippy, i would like to give you an example or paper, but its an idea i have that i have not seen anyone do before. The program is specific to a theory i have and i have got it to work, but needs to be faster to get a greater benifit from the device its running.
 

westaust55

Moderator
The program is specific to a theory i have and i have got it to work, but needs to be faster to get a greater benifit from the device its running.
If you already have something working, albeit slower than desired, then post your code and circuit schematic so folks here can better understand what you are trying to achieve.
 

matchbox

Senior Member
The topic was heading was arbitrary, because i didnt want to go into the operation details of the device. No offence ment by that westaust55:) I was just mostly looking for code samples that might give me an idea in writting my code to work slightly faster.

Thanks guys
 

westaust55

Moderator
While the code posted by hippy at Post 2 was in a loop, what is to stop you taking the content of that loop and using as a subroutine.

Then you can call at whatever point you need throughout the program to read the incoming ADC and adjust the PWM for outgoing analog level. The PWM and hence analog output is maintained at the last level until the subroutine is called to refresh/update the output based upon.

Keep in mind that even at 32 MHz, a GOSUB and RETURN command pair will take of the order of 200 usec.

If you want the updates to be on some pre determined intervals rather than specific points in the main PICAXE program consider using a timer to generate an interrupt at the required intervals (may need a change to an X2 part ?) and undertake the READADC, required scaling and PWM output inside the interrupt routine.
 

matchbox

Senior Member
Thanks Westaust55, As you suggested; i am using a call sample to read the ADC at points. And from that advance or retreat the PWM as it requires. It does sound like using an X2 part may be the go:)
 

AllyCat

Senior Member
Hi,

.... even at 32 MHz, a GOSUB and RETURN command pair will take of the order of 200 usec.
Actually, I believe it's almost 400 us with an M2, so best avoided if speed is really critical. And presumably much the same for an interrupt service? :(

Also, sometime I must measure the timing of a PWMDUTY command (as might be used for D-A conversion). I was surprised that it consumes more than 30 bytes of Program Codespace, which suggests that it's a PE-generated macro, so might be rather slow?

Cheers, Alan.
 

geoff07

Senior Member
Slightly surreal thread! But curiously, I think a PWM signal is analogue, not digital, albeit with lots of harmonics. After all, the variable is the pulse width, and the width is is a varying analogue quantity.
 

inglewoodpete

Senior Member
Slightly surreal thread! But curiously, I think a PWM signal is analogue, not digital, albeit with lots of harmonics. After all, the variable is the pulse width, and the width is is a varying analogue quantity.
PWM is certainly digital. It is usually used/interpreted as analogue by interfacing it to a low-pass filter. An RC network is one example of a low-pass filter. A motor is another example (of a low-pass filter).
 

geoff07

Senior Member
Hmm. But ideally a filter only passes some of what you put in it. If analogue is coming out, then it must be going in. All the filter does is modify the spectrum.
 

BeanieBots

Moderator
PWM is certainly digital. It is usually used/interpreted as analogue by interfacing it to a low-pass filter. An RC network is one example of a low-pass filter. A motor is another example (of a low-pass filter).
Agreed. Also, the duty is set by a digital (bit resolution) number, so is quantised in a digital manner.
It's digital in every respect except it's amplitude which we could debate might be considered analogue.
 

BeanieBots

Moderator
Hmm. But ideally a filter only passes some of what you put in it. If analogue is coming out, then it must be going in. All the filter does is modify the spectrum.
So, how many states can the PWM output pin be in?
How many states can a light switch be in? By your analogy, because I turn mine off in the morning and on at night I've converted to analogue!

Just for fun, what is the output of a digital filter?
Is a rolling average of a digital value analogue?

Hint: analogue (in theory) has an infinite number of states. ie continuously variable.
But then, what about electrons? Everything is made of atoms which are quantised so everything is digital.
Enjoy!

PS. After filtering, you can still only get 1024 different states from a PWM output, so even then it can't be analogue.
 

matchbox

Senior Member
So i suppose i should have worded it..... analog in to digital then processed to another digital form and back to anolog again (A to D to D to A)
e.g. Comes in as a varying voltage then is converted to a digital number then processed for output selection to another digital number, then converted back to a varying voltage.
Its the digital to digital data selection processing that slows the whole process down.

The way i was trying to get it across was...... 'analog in to digital and back to analog out' (A to D to A)
e.g. Comes in as a varying voltage then converted to a digital number and then converted back to a varying voltage.

There are two ways of righting an analog in to analog out program. The simple loop direct digital number conversion, as Hippy posted. Or writing out 1023 'IF' commands for the processor to scan through to select from. The later is accurate, but very slow.

I think Beanie said it well. Its about quantization. At what level of resolution does digital become analog. If we had MCU's that had millions of processing cores, like the human brain. Everything would be "viewed" as analog.
 
Last edited:

BeanieBots

Moderator
If we had MCU's that had millions of processing cores, like the human brain. Everything would be "viewed" as analog.
Great, now we can chat about my favourite topic, neural networks!
A few dozen 08M2's should do a reasonable job with a little help from a few op-amps.
 
Top