DAC on 18M2

MartinM57

Moderator
Time to start a thread on the next new command :D

Manual 2 says that the DAC output must be buffered for reliable use (and shows a voltage follower opamp circuit that's bound to prompt a few questions - what's the cap for, what value is it, what's an op-amp, what op-amp should I use, why isn't my op-amp output going right up to 5v etc etc).

So if I'm using DAC as an inter-PICAXE signalling system (32 levels down one wire to the other PICAXE's ADC pin and a READADC10 in that PICAXE - I wouldn't call it a comms link, but I see the simplicity attractive for simple signalling) is buffering needed?

I suspect not, but guidance would be good.

Hippy - like the task examples you put in the other thread, got any killer apps where DAC will make life easy?
 

hippy

Technical Support
Staff member
The example circuit is I believe the same as Microchip suggest which also doesn't give any suggested values for C nor op-amp type. I imagine almost any op-amp will do from the 741 up, but it will likely depend on application. The capacitor I would guess is there to compensate for the fact that the analogue from the DAC jumps to discrete levels when it changes voltages, to limit harmonics and smooth the voltage. The best C value may well depend on the frequency of changes made to DAC.

As for applications ... anything which requires an analogue output where 32 steps of analogue will do. One application is in bit-banging sine waves or other waveform functions. The PICAXE won't likely be able to generate extremely high frequency waves but may well be usable for some applications.

Cheap humidity sensors require an AC sine wave for operation so perhaps this will help in such cases.

With suitable routing plus sample and holds and level shifting it should be possible to create a lighting desk which can interface to industry standard analogue dimmers, albeit with only 32 brightness levels but that may still be better than on and off.

With suitable buffering you can control fan and motor speeds for those which do not work with PWM but require voltage control.

If anyone is planning to experiment with neural networks using PICAXE then it may be useful giving a direct 'fuzzy output' rather than digital. Multiple outputs can be diode mixed or summed using op-amps.

A particular use I can see is in a PICAXE capacitor meter. This can work using a fixed R and measuring the time for C to rise and fall. Ideally the R should be chosen to suit the capacitor ( pf, nF or uF ) but, alternatively, the voltage to R can be altered.
 

MartinM57

Moderator
Nice ideas - thanks.

Any view on my question - needing a buffer for (solely) inter-PICAXE signalling?

The example circuit is I believe the same as Microchip suggest which also doesn't give any suggested values for C nor op-amp type. I imagine almost any op-amp will do from the 741 up, but it will likely depend on application.
Having that example circuit in a datasheet is fine (where a reasonable amount of knowledge is assumed), but for (the major?) part of the typical PICAXE target market (students, some teachers, newbie hobbyists etc) it's maybe a step too far in its simplicity. I do appreciate the problem that if you say nothing - why can't I get variable brightness from my LED connected to the DAC output? etc

Also, the schematic assumes you realise it's a perfect op-amp being indicated - infinite gain, rail to rail voltage output swing etc. I guess there's going to be a need for a FAQ on:
- where do I get a -V supply for the 741 I've had in my drawer since 1976
- I've measured 0v/5v at the DAC pin but my opamp does >0v/<5v. Why?
- etc
 

Dippy

Moderator
"Will a LMC662 CMOS Op-amp be OK?"
- I think this the point Martin is trying to make (??).

I've seen many a Data Sheet where components are unidentified - in fact even yesterday when looking at a Triac driver application.
I'm never quite sure what the author's motive/assumptions are.
All I can say is try and do some calcs or good guesses and have a go - it may be quicker than posting and waiting.
 

Technical

Technical Support
Staff member
The DAC can be considered as just a long ladder of 32 resistors in series. The top of the ladder can be V+ or a voltage from the internal refereces such as 2.048V.

The DAClevel command simply connects the output to a particular 'joint' in this ladder. So no current is really available as it would have to flow through all the resistors, so you can't light an LED. It's also not ideal for connection to another PICAXE ADC as the DAC output impedance doesn't match the ideal ADC input impedance.

We actually considered giving a 'real' example op-amp but did not want to start another 'my op-amp is better than yours' thread. But maybe we will reconsider this for the next version!
 
Last edited:

hippy

Technical Support
Staff member
Microchip sate, "Due to the limited current drive capability, a buffer must be used on the DAC voltage reference output for external connections to DACOUT" and that is also reiterated in Manual 2.

Like other cases, people may choose to operate out of specification but that's entirely on their own head with reliable, correct, non-damaging operation not being guaranteed.
 

MartinM57

Moderator
"Will a LMC662 CMOS Op-amp be OK?"
- I think this the point Martin is trying to make (??)
Exactly. Most people these days want a ready made guaranteed solution...
- use this opamp
- do these tricks with Vref+ and Vref- (didn't think you could use both with a 18M2 but haven't checked)
- note that you will see X, Y and Z happening...
 

Dippy

Moderator
"'...my op-amp is better than yours' thread...."
- haha, I know what you mean but wouldn't worry about it.

Just give an example that works, I'm sure hundreds will. If others find something 'better' then hurrah! and if others choose something unsuitable and it doesn't work then , well, what can you say except "ours works" :)
 

BeanieBots

Moderator
Welcome to the wonderfull world of analogue!

If you are interfacing to/from analogue devices, you will (of course) know the required spec and hence what op-amp (if any is required) won't you???
Why is this any different to "should I use a word or byte for my calculation"?

It is very common practice for a functional analogue diagram to simply show an ideal op-amp. How close it can get it's outputs to power rail is not an issue because it is (and fairly so) assumed that the op-amp is provided with suitable power rails for it not to be an issue. (eg +/- 15v).

If the 'user' chooses to digress from the ideal by using single rail, then surely they have already made a concious decision to do that and therefore also accept the consequences of such a descision.

Besides, we are talking about a 5-bit resolution. I doubt the characteristics of ANY op-amp are likely to have a significant effect at that level (again assuming 'correct' implementation).

Generating a negative rail is simple.
1. Diode pump from PWMout to give -5v.
2. Two stage Cockcroft-Walton multiplier (double diode pump) will give about -9v when driven from PWMout but only at about 5mA to keep within single OP current limits.
3. Dedicated -ve rail chip such 7660.

Op-amp and analogue electronics are no more mysterious than microprocessors. It's just a different discepline.
If you find yourself needing an analogue output, then the need will dictate what you ahve to do.
If you just want to play, then do just that and discover all the pitfalls and gotchas for yourself.

If you can cope with the limitations of 8-bit maths, you can cope with the limits of a less than ideal op-amp.
 
Last edited:

hippy

Technical Support
Staff member
For testing or experiencing DACSETUP and DACLEVEL operation it should be possible to place the probes of a DMM or scope on the DAC pin and not use an op-amp buffer. Both should have high impedance inputs and draw very little current.

One novel application I've thought of is using DAC to interface to logic at lower voltage levels. Signalling would have to be by controlling DACLEVEL and if you do put out too much voltage, set the pin Output High you may 'disappear' what's being controlled. A digital input usually has a high impedance so may also not need an op-amp.

One thing to note is that "Vdacout=(N/32)*Vdacref", and with 32 steps, the maximum output is "(31/32)*Vdacref".
 
Last edited:

MartinM57

Moderator
...that's nearly twice the price (Farnell UK) of an 18M2 :D

I'm gonna wait for the section in Manual 3 about interfacing DAC to the outside world - in the simple and cheap stylee of PICAXE...but how long will I have to wait?
 

womai

Senior Member
To keep things simple I would recommend using a rail-to-rail op-amp (one that both accepts inputs from 0V to VCC and can also output signals from 0V to 5V). That's as close to an ideal op-amp as you can get. That way you don't need a negative supply or a second positive supply. The venerable 741 is a bad choice in that respect.

Suitable types would e.g. be Microchip MCP6022 or MCP6024, or Maxim MAX492 or AMX494. Both available as free samples.

The capacitor on the DAC output is simply there to remove unwanted higher-frequency noise. 0.1uF is a good choice to start out with.

Wolfgang
 

MartinM57

Moderator
I like the look of Microchip MCP6022. Just need:
- a reference circuit in Manual 3 for buffering the DAC (not that it's very hard to work out)
- it to be on the techsupplies inventory (for those that don't use the likes of Farnell etc)
...and the jobs a good'un.

Right - I'm off to find something else new on the 18M2...
 
Last edited:

MartinM57

Moderator
Just thought I'd have a quick play...just with scope connected to DAC pin (didn't dare connect it to anything else :D)

Code:
#picaxe18m2
#no_data
setfreq m32
low c.2              ; make the DAC pin an output
dacsetup %10100000   ; external DAC, supply voltage
do
    for b1 = 0 to 31
    daclevel b1 ; set DAClevel
    next b1
loop
A ~121Hz sawtooth of 32 levels, each lasting about 240uS - last one slightly longer as the code goes around the do...loop

 

hippy

Technical Support
Staff member
Excellent. If you've still got the scope attached you could try getting the speed up by relying on DACLEVEL dropping the top 3 msb -

#picaxe18m2
#no_data
setfreq m32
low c.2 ; make the DAC pin an output
dacsetup %10100000 ; external DAC, supply voltage
do
daclevel b0
b0 = b0 + 1
loop

Haven't tried it, but the fastest might be -

do
daclevel bptr
b0 = @bptrinc
loop
 

MartinM57

Moderator
Code:
do
    daclevel b0
    b0 = b0 + 1
loop
...129Hz sawtooth...and a clever way of manipulating the lower 5 bits

Code:
do
    daclevel bptr
    b0 = @bptrinc
loop
..132Hz sawtooth...and a challenge to understand the code :D
 

hippy

Technical Support
Staff member
The trick is in "b0=@bptrinc", effectively it's "bptr = bptr + 1" but by using "@bptrinc" you get a free increment without needing the firmware to read the "+" or "1" tokens. To use @bptrinc it has to be used as part of a command so may as well use it to fetch some data which is then ignored.
 

hippy

Technical Support
Staff member
Start0:
Suspend 1
... Initialisation ... of DACSETUP etc
Resume 1
Do
DacLevel b0
b0 = b0 + 1
Loop

Start1:
Do
DacLevel b1
b1 = b1 - 1
Loop

:)
 
Last edited:

MartinM57

Moderator
missing a DO at the beginning of start1:

...and I guess you know what it looks like on the scope.

Just need to find a use for it ;)
 

hippy

Technical Support
Staff member
Yes, missing a DO, but hadn't looked at it on a scope. A good excuse to get to understand how the scope works as it's still a bit of a mystery.

I was happy to see it was close to how I imagined it would be. Add a "b1=0" after "Start1:" and move "b1=b1-1" to before DACLEVEL and it was exactly how I'd expected. Going up periods 'same width' as going down periods.

Rather shows the trickiness of trying to get the order of statements and timing right when working with multi-tasking. Not something which would need to be commonly done.

I don't think I'll be in the front running of finding a use for it :)
 

MartinM57

Moderator
Code:
#picaxe18m2
#no_data

start0:
suspend 1
low c.2 ; make the DAC pin an output
dacsetup %10100000 ; external DAC, supply voltage
resume 1

do
    daclevel b0
    b0 = b0 + 1
loop

start1:
b1 = 0
do
    b1 = b1 - 1
    DacLevel b1
loop
 

hippy

Technical Support
Staff member
Is it my eyes or an artefact of the posted image that, for the top left of the diamond, the individual steps look to be sloping down to the right ?

I'll hesitate to say "rubbish scope you've got there" it's much nicer that what I get to see :)
 

MartinM57

Moderator
Using the "Wave Inspector" as it's called, to zoom into a captured waveform (up to 25,000x - only needed 25x here though), they're pretty flat.



The previous snapshot is pretty close to what it looks like on screen - there are artefacts for various reasons:
- it's about a 7.5" diagonal screen but not a huge amount of pixels
- it's only 8 bit vertical resolution (the spec item that is the most serious limitation I think, but there's always ways around it - like shown above)
- saving to a USB stick in .png format
- ..and then photo-editing it to a .jpg and posting it

EDIT:
- also it's an open circuit measurement - a few K load might tidy it up a bit
- the rise time of the edges is just a bit less than 3uS
 
Last edited:

hippy

Technical Support
Staff member
That's pretty impressive analogue response from the DAC, though I'll admit don't really know what I was expecting. It also highlights another benefit of digital scopes, the ability to actually see the verticals rather than just the top and bottoms as on analogue. Plus the ability to take a screen shot for posting. Thanks for all the images and the time taken to experiment.
 

westaust55

Moderator
Microchip state, "Due to the limited current drive capability, a buffer must be used on the DAC voltage reference output for external connections to DACOUT" and that is also reiterated in Manual 2.
Is there a Microchip type datasheet available for the 18M2 akin to the 300+ pages for pre "M2" chips (eg 08M = PIC12F683)? :confused:
I see that Manual 1 page 15 just refers to the markings as "PICAXE 18M2".
 

Dippy

Moderator
Martin asked a similar question recently.
Technical replied:
"No it is not a 16F1826, the 1827 is nearer. It is a custom part made for Revolution but it is based upon the same family as the 1826/27 and so the 1826 or 1827 registers etc will be the same, so you can indeed use the 1826 or 1827 datasheets for what you want to do."

So, until a pukka Data Sheet comes along I would assume that is the nearest thing. And people who do this seriously would appreciate a proper Data Sheet I'm sure. (Hands together and beg/pray :) )
 

westaust55

Moderator
The example circuit is I believe the same as Microchip suggest which also doesn't give any suggested values for C nor op-amp type. I imagine almost any op-amp will do from the 741 up, but it will likely depend on application.
Noting that the internal reference voltage can be as high as 4.096 V
should we be recommending say an AD822 which is Single Supply and Rail to Rail output capable.

I have see references to the CA3140 having rail to rail output on this forum, but my own tests in the past and the Intersil website suggest otherwise:
http://www.intersil.com/products/deviceinfo.asp?pn=CA3140

Rail-to-Rail Input . . . . . . N
Rail-to-Rail Output . . . . . N
 

westaust55

Moderator
Martin asked a similar question recently.
Technical replied:
"No it is not a 16F1826, the 1827 is nearer. It is a custom part made for Revolution but it is based upon the same family as the 1826/27 and so the 1826 or 1827 registers etc will be the same, so you can indeed use the 1826 or 1827 datasheets for what you want to do."

So, until a pukka Data Sheet comes along I would assume that is the nearest thing. And people who do this seriously would appreciate a proper Data Sheet I'm sure. (Hands together and beg/pray :) )
Thanks for the feedback Dippy
 

hippy

Technical Support
Staff member
There's unlikely to be a full "PICAXE-18M2" datasheet produced by Microchip or Rev-Ed.

As Technical has said, the PICAXE-18M2 is from the same family as 1826/27 so functionality and registers etc which apply to the 1826/27 will apply to the PICAXE-18M2. Anyone can take a Microchip 1826/27 data sheet and globally substitute "1826/27" with "PICAXE-18M2" in their copy if it makes them feel more comfortable.
 

Dippy

Moderator
I fully appreciate the "no more confusing PIC numbers for students to decipher!". Makes perfect sense, but many of the more experienced or adventurous old Lags like to have a bit of a Poke around.

Unless the base PIC is identified (or Rev Ed produce a fairly detailed Data Sheet c&p'd from PIC ds) how can people do this?

What are the hardware differences between 16F1827 and 18M2 ?
 

Technical

Technical Support
Staff member
We are not deliberately being evasive, but custom parts involve lots of hard work, non disclosure agreements and documentation. We are sure you understand that there are reasons we say that a PICAXE-18M2 is a custom part and 'based around the PICxxx family'.

However if you want to poke you will actually need pokesfr for the M2 parts, and as already stated you can get all the register names and positions from the 16F1826/1827 datasheet as they are the same family. pokesfr in part2 of the manual explains how you use the %ppp aaaaa addressing system where pp is page 0-7 and aaaaa is address 0-31 of that page.
 
Last edited:

westaust55

Moderator
Time to start a thread on the next new command :D

Manual 2 says that the DAC output must be buffered for reliable use (and shows a voltage follower opamp circuit that's bound to prompt a few questions - what's the cap for, what value is it, what's an op-amp, what op-amp should I use, why isn't my op-amp output going right up to 5v etc etc).
getting back to part of the original question,

From a TI app note that I have on Single Supply Op-Amp circuits, the capacitor on the unity follower/buffer circuit is given as 0.1uF and stated reason is:
"Capacitor C1 forms a low-pass filter to eliminate conducted noise on the voltage rail".
 

geoff07

Senior Member
Do I read from this thread that the highest audio freq I could generate this way would be of the order of 130Hz, with a 32MHz clock and using 32 levels for a sine wave?

I was rather looking for a cheap/easy way to make sine waves up to say 3kHz. I see that the old function generator chips (e.g. 038) are all obsolete and not available.

If anyone has any idea of a suitable current function generator chip that is easy to control by Picaxe and capable of being soldered by hand (dip ideally) then I would appreciate the info.
 

womai

Senior Member
With suitable filtering on the ouput you will need much less samples per period and could generate sine waves with much higher output frequencies. Microchip has a free program called "Filterlab" on their website where you enter filter type and a few other parameter and it spits out the complete circuit schematic for an active (op-amp based) filter. Chebychev, ~5th - 7th order, with a cutoff at ~1.2-1.5 times your highest frequency would be a good choice. If you produce DAC samples with a rate of at least 3x your highest frequency you will then see a nice sine wave on the output.

To generate arbitrary sine wave frequencies with very high resolution you should use a DDS (direct digital synthesis) alogorithm (Analog Devices has a good, extensive application note about that subject).

With these two tools (DDS and filtering) I believe 3 kHz could be achievable even with a Picaxe.

The whole thing would be almost trivial with a "bare" PIC microcontroller. If I find time I could probably come up with a program for the PIC12f683 (which the Picaxe 08M is based on) in an hour or two. Would talk to the Picaxe through a serial connection and would need a simple R-C filter on its output. Let me know if that sounds interesting.

Wolfgang
 

geoff07

Senior Member
I was expecting to do some filtering, but was expecting to need at least 8 samples per cycle so even then it would be low. The other problem is that I want the picaxe to do a lot of other things as well! The idea was to try to substitute software for hardware and keep the component count down in a simple audio wiring test unit. I want to generate test signals, measuring low incoming signals,control amplification and potentiometers, and use a moving coil meter as a level meter (controlled by pwm). All in a small box.

I did play with pwm but that is far too slow, at least via Basic.

The idea of a separate bare PIC is interesting. I would try it but the lure of Picaxe Basic has been so strong I haven't set up an IDE for bare chips, though I probably have the skills to do it. I do this for fun not profit so I don't want to pay for expensive IDEs and compilers but if you can advise an economic setup that works I could have a go.

How would you use the 12f683 to generate the signal? PWM?

Alternatively if you have a couple of spare hours .. but really I should do it, though a very kind offer. I foresee a future Picaxe function 'generate sine' for the new chips. Then the heavy lifting would be at clock speed but the control in Basic. Ideal.
 

womai

Senior Member
Nyquist's sampling theorem is applicable not only to signal measurement (e.g. oscilloscope) but also to generation. So in theory at least 2 samples per period is enough to define a sine wave - but you'd have to wait infinitely long for the waveform to settle. So in practice a ratio of about 2.5 - 3 samples per period is the best one can achieve. Of course if you want to generate some other waveform (e.g. suqare waves) they contain higher harmonics and Nyquist applies to the highest harmonic of interest, not the fundamental (which means you'd need many more samples per fundamental period).

Yes, I was thinking of using PWM to generate the output level. If I find time I may just try. Do you have a PIC programmer available to burn HEX files? (these programmers don't cost much). As for programming environments, personally I use Mikroelektronica'a MikroC, they have a free version available that can produce programs with up to 2K words of compiled code (which is as much as the 12F683 can hold anyway). They also have Basic and Pascal compilers.

I'll also see if the 18M can be beat high enough to generate 3 kHz sines directly Of course its internal DAC doesn't have much resolution so the waveforms will look like staircases at lower frequencies. PWM on a 12F683 could potentially produce up to 8 or 10 bits equivalent resolution.

Wolfgang
 
Top