DAC on 18M2

geoff07

Senior Member
Yes I did study Nyquist all those years ago but I didn't fancy building the sort of filter that only a few samples would likely need! I'll check out the Mikro s/w, that sounds interesting. I also just started looking into JAL which also promises to be of interest, but I fear spending time on learning curves and complex debugging rather than design. Which is exactly what Picaxe helps you to avoid. No problem with the staircases, after all that is what the filters are for, so long as they can be kept simple.

I do have a couple of programmer boards, never used, but I'll see what they can do.

I do think that a PIC programmed as a function generator could be of quite some interest, being ultra cheap and very simple to use once programmed.

Thanks for your interest!
 

womai

Senior Member
The staircase issue is a big thing if you need variable frequency. To remove the staircase you'd need a low-pass filter with adjustable cutoff frequency and set it somewhere a bit above the currently selected frequency. On the other hand, with sufficient resolution you only need to filter close to the highest frequency of interest (where the level jumps from one sample period to the next are large because there are only few samples per cycle) and thus get away with a fixed-bandwidth filter. Overall the less complex solution.

So far I have a DDS based sine wave generator (using PWM) running on my PIC12F683. The PWM is running at 60 kHz, that allows for 7 bits (128 steps) of PWM duty cycle resolution while running on the internal 8 MHz oscillator. You could go to 8 bits if you use an external 16 or 20 MHz resonator instead but I am trying to keep complexity to a minimum. The DDS sampling is running at 20 kHz so I should easily be able to hit 3+ kHz maximum sine wave output frequency with filtering.

Next step this afternoon is building a suitable anti-aliasing filter. I'll post PIC source code, hex file and filter schematic when I am done with that.

Wolfgang
 

womai

Senior Member
Ok, first, here's the low-pass filter - a 4th order Chebycheff. Generated with Microchip Filterlab. Nominal cutoff is 5 kHz. I used the nearest component values that I could find in my drawer for resistor and capacitor values. 10% deviation won't make a huge difference. If you want better filter behavior then increase the filter order e.g. to 6 or 8 (but the number of components increases accordingly).

Op-amp: Microchip MCP6022

Wolfgang
 

Attachments

MFB

Senior Member
Tracking low-pass filter

For variable output frequencies, the DAC could be followed by a switch capacitor low-pass filter chip. The filters clock rate (and therefore cut-off frequency) could be provided from the PICAXE via a pwm output. This would allow the filter cut-off frequency to automatically track the DAC output frequency.

There is a range of switch capacitor filters available (Maxim, Analog Devices etc) at low cost in 8-pin package, which require very few external components.
 

darb1972

Senior Member
@ Wolfgang - Thanks for the Microchip Filter Lab information. Great little program that will definitely come in handy.

@ Martin - Do you mind me asking what Brand/Model Scope you use? Sorry, this is slightly off subject but I am in the market for a decent scope. Interested to know what you have.

Thanks.
 

MartinM57

Moderator
Tektronix DPO2014 (100Mhz, 4 channel, 1Gs/s) - see http://www2.tek.com/cmswpt/psdetails.lotr?ct=PS&cs=psu&ci=14403&lc=EN

...with the following software options:
DPO2COMP - Computer Serial Triggering and Analysis (RS-232/422/485/UART)
DPO2EMBD - Embedded Serial Triggering and Analysis (I2C, SPI)

Pretty much OTT for most of the low speed digital stuff that I post here (a decent Picoscope would do just the same, if not better in terms of screen display), but in the mixed analogue/digital domain or pure analogue, it's a very effective beast.

Expensive, but if you can get it as an allowed capital expense, offset against profit, then it's a much better proposition.
 
Last edited:

womai

Senior Member
As promised, here is my proof-of-concept project for the PIC12F683. Development platform is MikroC Pro (http://www.mikroe.com).

It modulates the duty cycle of a PWM signal to generate a pulse-width encoded sine wave. To actually see the sine wave signal you need to low-pass filter the output. For low output frequencies (up to a couple 100 Hz) this can be as simple as a R-C filter (resistor leading into capacitor) with a time constant R*C about 20x lower than the highest output frequency. But for best results use the active multipole filter I posted above, or similar. Then you can get up to ~5 kHz sine waves. All tested in practise (development board & output filter on breadboard) so it's guaranteed to work.

There is a tradeoff between achievable duty cycle resolution and the PWM frequency. The higher the resolution the smoother the output. The higher the PWM frequency the higher the achievable output frequency. So it's a tradeoff. The program as it uses ~120 kHz PWM which limits resolution to 6 bits. It also has commented-out sections for 7 and 8 bit resolution (with correspondingly lower max. frequency).

Right now the output frequency is hardcoded (variable assignment close to the top). Add a serial receive or maybe read in a DAC value to make it user adjustable. Right now the calculation (all integer) limits it to 1 Hz frequency steps, but the DDS algorithm would really allow for MUCH fine steps if needed (down to ~5 uHz) - 1 Hz step still corresponds to a pretty large phase_incr value.

The zip file (change the file extension of the attachment from pdf to zip!) contains the complete project (project file, source code, and a HEX file that you can simply burn onto you PIC and that will generate a 500 Hz sine wave).

Wolfgang
 

Attachments

Last edited:

womai

Senior Member
Oops. User error. I was only taking 125k samples per capture. Changing that to 1.25M per capture and zooming in 10,000x gives a risetime of ~480nS. That's more like it...
Just measured a full-sized step down (code 31 down to 0) and measured settling time to less than 1 LSB is <250ns.

A full-sized step up in contrast showed almost 1us before settled to less than 1 LSB.

Anyway, both numbers are orders of magnitude faster that the Picaxe command execution speed so settling time shouldn't be a concern.

Wolfgang
 

geoff07

Senior Member
Wolfgang, that is a fantastic piece of work and I will certainly try it out. Currently having trouble with my burner board which does not seem to like 8-pin chips, but will solve that shortly I hope. What do you use with the Mikroe system to program chips?

Meanwhile I have discovered just how many different development environments there are for bare chip programming. And how much simpler it is to stick with Picaxe where they are feasible!

However, Mikroe seems to work under Wine under Ubuntu which is a good start. My XP systems are starting to creak and I have decided not to spend £100 on Windows 7. Ubuntu 10.04 seems to be completely stable and very fast. My wife who is a medical doctor and completely intolerant of technology has happily used Ubuntu for her email etc for several years now.

I was also fascinated by the switched-capacitor idea (especially discovering the concept that a capacitor plus a couple of switches equals a resistor). I shall have to get hold of some devices to try.
 

womai

Senior Member
I user MikroE's EasyPIC 5 development board. Very good value for money. (version 6 is out by now).

Yes, the Picaxe is in its own league for simplicity yet it's very powerful and flexible as long as you don't need raw execution speed (most of the time, that's not an issue). Picaxe got me started with microcontrollers so I'll be eternally thankful for its existence.

DDS as well as pulse-width encoded data can be fascinating (at least for a while :) - especially when looking with the scope at the scrambled, messy signal at the filter input and then compare it to the clean, nice, stable sine wave at the output. Add to that the switched capacitor filter principle and you learned a huge amount of quite advanced electronics in just one relatively small project. That alone is worth the effort.
 

geoff07

Senior Member
Wolfgang, I see the hex file and lots of other generated files but no C or ASM source except for led_blinking.

I'm now setting up an MPLAB programmer (I went for PICkit2 as the hardware as mine was too old to support the current chips). I have loaded the hex and see the pwm signal on pin 5 but still need to add some filtering.
 

womai

Senior Member
led_blinking is the correct C file etc. Don't get misled by the name. I just modified one of the sample programs (always easier to start with something that already works) and did not bother chaning any of the file names or the project name.

Wolfgang
 

geoff07

Senior Member
Cunning! I should have looked at the code. Too many things going on.

Given that I'm now on the HTECH C compiler with MPLAB, what does the built_in.h include do, so I can determine the HTECH/MPLAB equivalent? Also I suspect that the registers have different names, or else I'm still way down the learning curve.
 

womai

Senior Member
built_in.h only has a few definitions to allow byte-wise access to int's and long's. That way you don't have to worry if variable are stored as big endian or little endian.

For 32 bit words:

lo() = bits 7:0 (i.e. LSB)
high() = bits 15:8
higher() = bits 23:16
highest() = bits 31:24 (i.e. MSB)

The register names correspond 1:1 to the names in the PIC datasheet. One thing that could need some translation is the way MikroC provides access to bits within a byte or word; this is either done by overlaying a union containing bit fields (defined in an automatically included, predefined file), or by bitwise access as variable.Fbit. Not sure how the Hitech C compiler solves that. Another place where each compiler tends to be different is how you define the interrupt service routine. In MikroC for PIC16 and PIC18 this is done simply be declaring a function named "interrupt" without parameters and without return valune, i.e.

void interrupt ()
{
....
}

Yes, moving away from the Picaxe does add complexity... but in return you gain speed and flexibility.

Wolfgang
 

darb1972

Senior Member
Tektronix DPO2014 (100Mhz, 4 channel, 1Gs/s) - see http://www2.tek.com/cmswpt/psdetails.lotr?ct=PS&cs=psu&ci=14403&lc=EN

...with the following software options:
DPO2COMP - Computer Serial Triggering and Analysis (RS-232/422/485/UART)
DPO2EMBD - Embedded Serial Triggering and Analysis (I2C, SPI)

Pretty much OTT for most of the low speed digital stuff that I post here (a decent Picoscope would do just the same, if not better in terms of screen display), but in the mixed analogue/digital domain or pure analogue, it's a very effective beast.

Expensive, but if you can get it as an allowed capital expense, offset against profit, then it's a much better proposition.

Thanks Martin.
 
Top