Generating 10kHz sine wave signal by PIC and DAC IC

Thread Starter

PersianEngineer

Joined Aug 15, 2013
19
In need to build a sine wave generator with amplitude 0.1 and frequency 10k max by using PiC micro and a DAC IC. I can generate the wave by a for loop but it's not exactly in the frequency I need. I think it's better to use timer of micro. But the problem is here. The minimum interval of timer without prescale is much bigger that what I need. So the problem is making an exact interval between generating a voltage by DAC using e.g. 10 sample per cycle or 100kHz sample rate or less.
Could you give me any advice that how can I generate exactly the signal with the mentioned frequency by an idea or some code?
 

JohnInTX

Joined Jun 26, 2012
4,787
You did not specify which PIC you want to use or if its a parallell or serial DAC or if you are programming in assembler or C but here are some suggestions.

1) Store pre-computed DAC values corresponding to the sine wave in a table. Don't compute on the fly.
2) Configure TMR2 to interrupt (preferably high priority) at the desired period of the wave * the number of points per wave.
3) Pre-fetch the table value
4) On each interrupt, write the pre-fetched table value to the DAC then fetch the next value. You can determine the end of the table by using a special data value (FFh), count the points etc.
5) Varying the timer interrupt rate varies the frequency.

At 10KHz, the period is 100us. Assume you want 100 points/wave and you get an interrupt rate of 1us. With a 48MHz PIC, the timer 2 settings are:
Prescaler = Postscaler = 1, PR2=12.

At 48MHz, you have 833 Tcyc to fetch/output the next point and wrap the table. More than enough, even in C, usually.

Consider using a PIC with built-in DAC like 16F1784. It only runs at 32MHz max but still should be quick enough. An 18F with built-in DAC would be even better.

Stay away from baseline and midrange for this one. You want a more streamlined architecture and more clock speed. I would go for an 18F myself. With the high priority interrupt and fast-return stack, your interrupt service routine becomes about 3 instructions, not counting wrapping the table pointer.

Have fun.
 

MaxHeadRoom

Joined Jul 18, 2013
28,686
An 18F with built-in DAC would be even better.

.
I searched the whole 18F listing and could not find one when I was looking?, the Product features list heading didn't even show a heading for DAC?
Have you come across one, as I am looking at using an I2c external DAC.
Max.
 

JohnInTX

Joined Jun 26, 2012
4,787
I searched the whole 18F listing and could not find one when I was looking?, the Product features list heading didn't even show a heading for DAC?
Have you come across one, as I am looking at using an I2c external DAC.
It would be better, wouldn't it? I had the datasheet for the 1784 handy figuring that the OP would look to see if something in the 18F would be there as well. Guess you did (and I did too, finally) but there isn't one.

Sorry for the confusion.
 

ScottWang

Joined Aug 23, 2012
7,400
pic → EEPROM → DAC → Op Amp RC Integration circuit.
Preprogramed the datas of the sinewave into the parallel EEPROM.

If you using this way will let you reducing the time spend on uC.
 

JohnInTX

Joined Jun 26, 2012
4,787
The PIC16F1459 has a 5-bit DAC. Be warned, it's an oddball processor, but it's fast.
That's why I was looking at the 16F1784, its 8 bits. But your comment got me to looking at it. The DAC appears to be intended to be used as a programmable voltage reference. No settling time, slew rate or other AC parameters are specified nor is glitch-free operation. Plus it has to be buffered.

Maybe not such a good idea.
 

THE_RB

Joined Feb 11, 2008
5,438
Processors and their PWM modules are so fast now DACs are almost obsolete. Many of the standard PIC 18F series can run at 64MHz at the PWM module granularity, and come standard with 10bit PWM modules.

It's quite trivial to generate a good sinewave by loading 50 table samples into the PWM module (to make a full sine cycle).

Like this one;
http://www.romanblack.com/onesec/Sine1kHz.htm

Or this one;
http://www.romanblack.com/onesec/SineDDS.htm
 

ScottWang

Joined Aug 23, 2012
7,400
Processors and their PWM modules are so fast now DACs are almost obsolete. Many of the standard PIC 18F series can run at 64MHz at the PWM module granularity, and come standard with 10bit PWM modules.

It's quite trivial to generate a good sinewave by loading 50 table samples into the PWM module (to make a full sine cycle).

Like this one;
http://www.romanblack.com/onesec/Sine1kHz.htm

Or this one;
http://www.romanblack.com/onesec/SineDDS.htm
Does any other PICs can be replace the PIC 18F1320?
 

MrChips

Joined Oct 2, 2009
30,806
I would be looking at an STM32 MCU that has built-in DAC and DMA.
With DMA capabilities you can achieve very high sample rates with no dependency on software timing.
 

Danm1

Joined Jul 19, 2010
69
To answer the original post, you say the PIC timer is not enough bits and times out, then in your interrupt, have a variable that really just extends the timer.

So the timer times out, an interrupt is generated, you increment the variable, if it does not reach the target value, then just reset the timer and return from the interrupt. If it does reach the target, then you may still have some more delay to get to your precise time period.

It's not perfect but for a 100KHz signal, it may be good enough. Otherwise, just use a DDS chip.
 

THE_RB

Joined Feb 11, 2008
5,438
Does any other PICs can be replace the PIC 18F1320?
The first link I posted has full C source code, so you can adapt to any other PIC if you like.

The second link has a .HEX file that requires a PIC 18F1320, there is no source code for that one. The 18F1320 is a very popular and common PIC. :)
 

ScottWang

Joined Aug 23, 2012
7,400
The first link I posted has full C source code, so you can adapt to any other PIC if you like.

The second link has a .HEX file that requires a PIC 18F1320, there is no source code for that one. The 18F1320 is a very popular and common PIC. :)
Thanks.
I just searching the PIC 18F1320, and I found it about US$1.67/1pcs from Taiwan seller for 100 pins, and found it on ebay cost US$6.99/1pcs for 18 pins.
 

atferrari

Joined Jan 6, 2004
4,769
I would be looking at an STM32 MCU that has built-in DAC and DMA.

With DMA capabilities you can achieve very high sample rates with no dependency on software timing.
With no experience on that in particular myself, where the timing for DMA comes from then?
 

MrChips

Joined Oct 2, 2009
30,806
There are a few ways of doing this.

The DMA timing can come for the peripheral itself. For example, every time an ADC completes it conversion it can initiate a new conversion.

In the case of a DAC, you may want samples to be output at a fixed controllable rate. For this you use an internal timer module to set the period which can be as short as one clock cycle. At the end of every timer period a DMA request is issued to transfer data from memory to the DAC. You can set up the DMA to access a circular buffer in memory so that the DAC continuously accesses a waveform stored in memory.

All of this is done automatically by the DMA hardware with no software intervention required. The CPU is free to continue normal processing, including updating the waveform look-up table if so desired.
 

ScottWang

Joined Aug 23, 2012
7,400
That Taiwan link you posted didn't work for me, but as a guess I would think the Taiwan price is $1.67 per IC for minimum 100 *pieces* (ie you must buy 100 chips). :)
I would like the 18 pins package, the seller of Taiwan said that the price US$1.67/1pcs is for bid not for sale directly, so the price for each piece should be up more.

Unless there are some cases going on, otherwise I won't buy 100 pcs each times, but my friend will do that kinds of things, maybe he will buy the parts about 500 or 1000 pcs each times, because he got the business running, he even bought the products from Ebay, but I didn't.
 
Top