Author Topic: How to generate Sine wave using pic microcontroller  (Read 4736 times)

0 Members and 1 Guest are viewing this topic.

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 1367
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #25 on: April 05, 2019, 01:34:21 am »
I never in the past had issue with making a stable clock coming out of a pic.

There is no issue to get stable clock from PIC (with considering PIC jitter and CLK jitter limitations).

There is a problem to implement good NCO which can produce sine wave with good spectrum purity in realtime on the PIC, especially when NCO frequency cannot divide the clock frequency with integer.

And forget 50hz/60hz, in the 90s, I managed a 8 bit output full DTMF tone generator for dialing on the telephone line on a PIC16C52.  Yes, true sine waves

There is no need for sine wave and no need for DAC/ADC in order to work with DTMF.

I remember in the middle 90's I was implemented DTMF coder and DTMF decoder on the PIC16F84 (1k flash and 68 bytes RAM) which worked well with no needs for DAC or ADC.

It worked with 1 digital pin for input (connected to the comparator) and 1 digital pin for output (connected to speaker) and was able to produce acceptable DTMF signals and make simple FFT for fixed frequencies (used in DTMF) in the realtime.

It was able to detect and produce DTMF codes and even DTMF sequences pretty well with no need for DAC or ADC at all. It used i2c one line LCD to display decoded DTMF sequence and was able to send hardcoded DTMF sequence by pressing key :)

If DTMF code is detected it doesn't means that the signal consists of two pure clean sine waves and nothing more  :)
« Last Edit: April 05, 2019, 04:42:46 am by radiolistener »
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 1367
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #26 on: April 05, 2019, 03:28:07 am »
Numerous existing projects say that it is possible. Seems like 10bits are coming from thin air, I would reduce it to 8bits or so. Also whole "NCO implementation" may be precalculated array of 20 sine values that are clocked out into R2R DAC from ISR running at 1000Hz. That's it.

yes with hardcoded 20 values and 1000 Hz ISR it will works.

P.S. Your scaremongering of DDS DAC nonlinearities (if any) is clear offtopic here in this thread, especially when you say said that effect is visible below -75dB level.

With 10-bit DAC chip it will be about -55 dBc.

Tried to simulate it for hardcoded 20 values and 10 bit R2R DAC with 5% resistors at 1000 Hz sample rate.
I din't take into account clock jitter, just quantization noise and non-linearity due to resistors value.


There is a spectrum difference for square wave and 10-bit DAC versions.
IMD for 10-bit DAC version is about -64 dBc (very good result, I think it will be worse in reality).

I'm not sure what is better. Because this 100 Hz IMD spur from DAC version will be hard to filter with low pass filter.
It is too close to the carrier.  :-//
So, I tend to think that square wave + good LPF will be better :)
But if we don't take into account IMD spurs, in average, DAC version will be better, because it has lower harmonics level, so it needs smaller filter suppression at higher frequency.
« Last Edit: April 05, 2019, 04:08:00 am by radiolistener »
 

Offline soldar

  • Super Contributor
  • ***
  • Posts: 2605
  • Country: es
Re: How to generate Sine wave using pic microcontroller
« Reply #27 on: April 05, 2019, 08:00:58 am »
I have no idea of the requirements of the sine wave or why it has to be generated with a PIC. The power needed, the acceptable distortion and other specs would determine how to best do it.

This is simple enough that, unless there are strong reasons to do it otherwise, I would prefer to do it with traditional electronics rather than a PIC. Simpler and more reliable.

A relatively good approximation to a sine wave can be obtained by using the "rule of sixths". Divide the cycle into 12 parts. During the first part the value increases by 3/6, during the second part it increases by 2/6 and during the third part it increases by 1/6. The rest follows easily. During the fourth part it decreases by 1/6, during the fifth part it decreases by 2/6 and during the sixth part it decreases by 3/6 and we are now back to zero. Now it continues descending, 3/6, 2/6, 1/6, etc.  This simple function gives a reasonably good approximation of a sine function and can be implemented with a capacitor and two current sources or even two resistors
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3322
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #28 on: April 05, 2019, 10:40:24 am »
Tried to simulate it for hardcoded 20 values and 10 bit R2R DAC with 5% resistors at 1000 Hz sample rate.
IMD for 10-bit DAC version is about -64 dBc (very good result, I think it will be worse in reality).

Nice & well done! Thank you for investing your time. I would simulate using 2% considering that 1% resistors are used. Anyway.. it shows that I am right:

According to your simulation we have roughly -60dB 2nd harmonic @100Hz for DAC and -10dB 3rd harmonic @150 Hz for square wave. We do not need low pass filter for fixed freq! Compliments to AD Filter Wizard, I quickly made bandpass "needle" filter (attach). It attenuates 100Hz by roughly 57dB, but 150Hz - by around 67dB. Difference just around 10dB!!! If we put it all together, then after filtering DAC strongest harmonic will be at -60-57 = - 117 dB level, but strongest harmonic of square wave filtered with *same* filter will be -10-67 = -77dB. DDS won filtered square wave by huge 40dB SNR margin.
« Last Edit: April 05, 2019, 10:55:43 am by ogden »
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 4682
  • Country: ca
Re: How to generate Sine wave using pic microcontroller
« Reply #29 on: April 05, 2019, 08:08:18 pm »
There is no need for sine wave and no need for DAC/ADC in order to work with DTMF.
My competition who programmed their DTMF generator pic firmware this way using Microchip's reference code sounded like shit.  The scope waveform also looked like shit, no matter how extensive the design of the output filter.  8 resistors with a 10nf cap just costs less and outperforms an opamp with complex notch filter, on a grand scale.  Even 4 bits, ie 4 resistors outperformed the 1 bit solution.  (Yes, bell did properly interpreter the 1 bit generated signal, but, hearing that shit out of the earpiece of the phone was unacceptable compared to my solution which sounded exactly like a true dedicated DTMF generator IC.)
« Last Edit: April 05, 2019, 08:13:14 pm by BrianHG »
__________
BrianHG.
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 1367
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #30 on: April 05, 2019, 08:48:51 pm »
after filtering DAC strongest harmonic will be at -60-57 = - 117 dB level, but strongest harmonic of square wave filtered with *same* filter will be -10-67 = -77dB. DDS won filtered square wave by huge 40dB SNR margin.

yes, with using the same filter, DAC DDS has lower harmonics level in comparison with square wave.
But DAC version has more spurs, which is missing in square wave version. Their level is small, but they exsist.
You can reduce it's level with better filter, but you cannot eliminate it at all :)

So, what is better is depends on topic starter requirements.
If he needs more clean spectrum (with rare spurs), then square wave + good low pass filter is better.
If spurs is not an issue, and the main goal is just to reduce harmonics level, then DAC + low pass filter will be better.

Also, square wave version is more easy to implement, it requires just one pin of the controller, and is not critical to processor performance. It can run even with CLK = 400 Hz. It doesn't needs a DAC. It doesn't needs a lot of wires between processor and DAC. It is cheap and easy :)

Instead of spend money on the DAC, you can spend it to improve filter and get the same harmonics performance, but with better spur performance and lower CPU load, lower power consumption.
« Last Edit: April 05, 2019, 09:13:33 pm by radiolistener »
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 1367
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #31 on: April 05, 2019, 09:18:25 pm »
this way using Microchip's reference code sounded like shit.  The scope waveform also looked like shit

I didn't used any Microchip reference code for my DTMF coder/decoder.
I was not interested in the beauty of the waveform on oscilloscope.
And I was not interested to get beautiful and pleasant sound, because I was not going to listen to it on Hi-Fi audiosystem.
My goal was to send and receive DTMF sequences through phone line with minimum hardware. And it worked well.


Even 4 bits, ie 4 resistors outperformed the 1 bit solution.

Of course, there is a difference between 10-bit DAC, 4-bit DAC and 1-bit DAC.
In some cases, 1-bit DAC is better than 10-bit DAC, in other cases 10-bit DAC is better than 1-bit DAC.

What is better is depends on your requirements  ;)
« Last Edit: April 05, 2019, 09:30:57 pm by radiolistener »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3322
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #32 on: April 05, 2019, 09:41:26 pm »
So, what is better is depends on topic starter requirements.
If he needs more clean spectrum (with rare spurs), then square wave + good low pass filter is better.

Rare spurs? What's that? :-// With your help I just demonstrated that SNR of filtered DDS is better than filtered square wave, yet (again) you say opposite. Anyway I agree that for many applications filtered square wave may be more than enough.

BTW just noticed that simulated DAC spectrum does not have sampling 1KHz spur, but two symmetric spurs around 1KHz instead. Some kind of unwanted FM-modulation artefact? Could you please check your simulation and if possible - make 8-bit DAC with 1% R2R ladder?

Quote
In some cases, 1-bit DAC is better than 10-bit DAC, in other cases 10-bit DAC is better than 1-bit DAC.

We shall not introduce 1-bit oversampling DAC confusion such as Direct Stream Digital because it is way too different story. Here we talk about filtering fundamental frequency out of square wave, right?
« Last Edit: April 05, 2019, 09:45:40 pm by ogden »
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2352
  • Country: nz
Re: How to generate Sine wave using pic microcontroller
« Reply #33 on: April 05, 2019, 09:59:57 pm »
I hope that the 50Hz needle filter with a 2Hz pass band  can be made with the spread of 1% tolerance parts... 
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3322
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #34 on: April 05, 2019, 10:54:50 pm »
I hope that the 50Hz needle filter with a 2Hz pass band  can be made with the spread of 1% tolerance parts...

Good question. Even with 10Hz bandpass, 10% caps and 5% resistors (attach), slope between 100Hz and 150Hz does not differ much compared to "needle filter", ~15dB versus 10dB. Still huge win for filtered DDS, 35 dB.
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 1367
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #35 on: April 05, 2019, 10:57:58 pm »
Rare spurs? What's that? :-//

Since 1-bit DAC has just 2 possible output level, it's output doesn't have non-linear distortion products.
N-bit DAC output has such non-linear distortion products, since it has some non-linearity due to resistors value spread.
This is why 1-bit DAC spectrum is more clean than N-bit DAC (if we don't take into account harmonics level).

BTW just noticed that simulated DAC spectrum does not have sampling 1KHz spur, but two symmetric spurs around 1KHz instead. Some kind of unwanted FM-modulation artefact? Could you please check your simulation and if possible - make 8-bit DAC with 1% R2R ladder?

I applied just quantization and resistors value spread effects. It doesn't include all possible DAC issues.
The result is sampled with 1 MHz and processed with FFT at RBW = 1 Hz with float32 precision.

Here we talk about filtering fundamental frequency out of square wave, right?

Yes, exactly.
The square wave is a sine wave representation on 1-bit DAC output.
So, technically we are compare 1-bit DAC with 10-bit DAC, but we're compare it for fixed sine wave :)

And yes, we can propose topicstarter with a third way to produce sine wave   :D
He can use 1-bit DAC with oversampling and sigma-delta modulator in order to produce arbitrary waveform.
So, he will not be limited with just a sine wave and will be able to produce any kind of waveform with no need for DAC.

But I think it will be overkill, because topic starter needs just a fixed sine wave, so there is no need for oversampling.
It can be done with a simple fixed square wave and apply low-pass filter with a proper cut-off.

slope between 100Hz and 150Hz does not differ much...Still huge win for filtered DDS, 35 dB.

It may be win or lose. It depends on requirements and selected filter. Harmonics level may not be an issue at all. But more non-linear distortions products may be an issue.
« Last Edit: April 05, 2019, 11:15:36 pm by radiolistener »
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 1367
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #36 on: April 05, 2019, 11:30:42 pm »
By the way, 50 Hz looks very like the frequency for DC-AC inverter.
In such case the square wave on the single pin output with a simple low-pass filter will be more than enough.

10-bit DAC will be huge overkill for such task.
Even PIC controller here is a huge overkill.
Because it can be easy done with a simple RC or LC oscillator :popcorn:
« Last Edit: April 06, 2019, 01:27:52 am by radiolistener »
 

Online Zero999

  • Super Contributor
  • ***
  • Posts: 14686
  • Country: gb
  • 0999
Re: How to generate Sine wave using pic microcontroller
« Reply #37 on: April 06, 2019, 06:17:37 pm »
By the way, 50 Hz looks very like the frequency for DC-AC inverter.
In such case the square wave on the single pin output with a simple low-pass filter will be more than enough.

10-bit DAC will be huge overkill for such task.
Even PIC controller here is a huge overkill.
Because it can be easy done with a simple RC or LC oscillator :popcorn:
The main advantages of using an MCU would be accuracy and cost.
 

Offline alex.martinez

  • Contributor
  • Posts: 44
  • Country: es
  • Happiness is fixing electronics by hitting them.
Re: How to generate Sine wave using pic microcontroller
« Reply #38 on: April 06, 2019, 11:58:03 pm »

what level of sine purity do you needs?

Roughly speaking, square wave generated with 2 levels pin is also can be considered as a sine wave.
The difference with usual sine wave is just a level of harmonics and other components.
In the real life these components are always present. The important thing is it's level.


The harmonic level in a square wave is so through the roof that it can be considered anything but a sine wave, speakin roughly or not. What you mean is that any signal can be interpreted as a collection of complex exponentials as shown by our friend Fourier...
 

Offline soldar

  • Super Contributor
  • ***
  • Posts: 2605
  • Country: es
Re: How to generate Sine wave using pic microcontroller
« Reply #39 on: April 07, 2019, 10:11:43 am »
The harmonic level in a square wave is so through the roof that it can be considered anything but a sine wave, speakin roughly or not.

I dislike the "modified sinewave" name used for a rectangular wave. It is just misleading and wrong. It is like saying your wife is a "modified cow" and it should carry the same punishment. :)

A square wave is not a "modified sinewave" in any way, shape or form.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 1367
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #40 on: April 07, 2019, 10:36:52 am »
A square wave is not a "modified sinewave" in any way, shape or form.

yeah, square wave is not modified sine wave. It is original sine wave, but it is mixed with other higher frequency sine waves :)
 

Offline soldar

  • Super Contributor
  • ***
  • Posts: 2605
  • Country: es
Re: How to generate Sine wave using pic microcontroller
« Reply #41 on: April 07, 2019, 11:08:01 am »
"Modified sine wave" is marketing fluff. Anything is a "modified sine wave". Brahm's Symphonies are "modified sine waves".

That's like saying my pee is "modified beer". It sounds much better and is technically true but I think it is misleading.

If I said "This place smells like modified beer" how many people would really understand what I mean?

It is the same with "modified sine wave". The only purpose of the expression is to hide the truth. Someone is told "you need to get a sine wave converter, not a square wave but a sine wave". They go shopping and they find one labelled "square wave" and another labelled "modified sine wave".  Which one do they choose?

- Your honor, I lovingly gave my wife some modified cheese.

- Yeah, you modified it by adding rat poison.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Online Zero999

  • Super Contributor
  • ***
  • Posts: 14686
  • Country: gb
  • 0999
Re: How to generate Sine wave using pic microcontroller
« Reply #42 on: April 07, 2019, 02:20:30 pm »
It's possible to make a modified sine wave with an MCU I/O port.

Connect it to a potential divider, with two equal value resistors. When the I/O port is set to an input, it will be at half the voltage, yet it can be set high and low. This can be low passed filtered to give a real sine wave.
 

Offline soldar

  • Super Contributor
  • ***
  • Posts: 2605
  • Country: es
Re: How to generate Sine wave using pic microcontroller
« Reply #43 on: April 07, 2019, 02:41:44 pm »
It's possible to make a modified sine wave with an MCU I/O port.

Connect it to a potential divider, with two equal value resistors. When the I/O port is set to an input, it will be at half the voltage, yet it can be set high and low. This can be low passed filtered to give a real sine wave.
Don't forget to block the DC and to use a *really* good filter. After that I feel I might as well design and build an oscillator.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf