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

0 Members and 4 Guests are viewing this topic.

Offline khatus

  • Regular Contributor
  • *
  • Posts: 73
  • Country: gl
How to generate Sine wave using pic microcontroller
« on: April 04, 2019, 02:35:44 pm »
Hello guys i want to generate Sine wave using pic 18f2550 microcontroller using timer0 .But how i can implement this??
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 6535
  • Country: us
Re: How to generate Sine wave using pic microcontroller
« Reply #1 on: April 04, 2019, 03:12:49 pm »
You might start with a Google search for: 'generate sine wave with pic' which will produce a large number of hits.  The two predominant themes are 'lookup table' where you cycle through a table in memory outputting bits to a parallel port that looks a lot like a DAC (Digital Analog Converter) when you add some external resistors (R/2R ladder) or you generate it by PWM (Pulse Width Modulation), changing the width of the output pulse many times during a cycle.

This one uses an external DAC controlled by the PIC over the SPI bus:


The easy way is the use a parallel port and an R/2R ladder.

https://www.tek.com/blog/tutorial-digital-analog-conversion-r-2r-dac

PWM is more involved...
 

Online mikerj

  • Super Contributor
  • ***
  • Posts: 2150
  • Country: gb
Re: How to generate Sine wave using pic microcontroller
« Reply #2 on: April 04, 2019, 03:18:04 pm »
What frequency do you want to generate the sine wave at?  What resolution?

You need some way to output an analog value, e.g. a timer capable of PWM (not timer 0!) followed by an external low pass filter, an external DAC (SPI/I2C or parallel according to speed requirements) or you can make a basic low resolution DAC using port pins and resistors.

Technically you could use Timer 0 for PWM, but you would need to be servicing an interrupt on every PWM transition, so this would be quite heavy on CPU time if you need a fast PWM.  Much easier to use Timer 2 and a CCP peripheral to generate it.

 

Offline khatus

  • Regular Contributor
  • *
  • Posts: 73
  • Country: gl
Re: How to generate Sine wave using pic microcontroller
« Reply #3 on: April 04, 2019, 03:26:30 pm »
50Hz and 10bit adc
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9126
  • Country: my
  • reassessing directives...
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #5 on: April 04, 2019, 04:21:56 pm »
50Hz and 10bit adc

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.

For example, you can generate square wave 50 Hz on the PIC pin and then apply Low Pass Filter to cut off all components above 50 Hz. In such way you will get cheap and easy way to get 50 Hz sine :)

But if you're needs to get high purity sine, then it will be hard to implement LPF to cut off unwanted harmonics with required level. But I think PIC controller also will be not good choice for such case.
« Last Edit: April 04, 2019, 05:32:32 pm by radiolistener »
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 1383
  • Country: us
Re: How to generate Sine wave using pic microcontroller
« Reply #6 on: April 04, 2019, 05:43:52 pm »
WARNING: 
   Here is some code that I abandon because it was too slow.
   It is an excerpt and incomplete for a PIC16F877A.
   It uses a MC1408 D/A converter.  Basically a R2R ladder network.

With a 20MHz MCU clock, I seem to remember being able cover the audio frequencies.
It will generate both a sine and triangle waveform.

Edit-  It looks like it also does a frequency sweep and the input is the sine period instead of frequency.

Code: [Select]
#include <xc.h>
 #include <stdint.h>

uint8_t  buffer[80];

typedef union {
   uint8_t  b[2];
   uint16_t i;
} bits_t;

uint8_t  S_Count;
uint8_t  S_Phase;
uint8_t  S_PhaseRate;
uint8_t  S_TimeRate;
uint8_t  S_Triangle;
bits_t S_Time1;
bits_t S_Time2;

const uint8_t cosine[256] =
{
   254, 254, 254, 254, 254, 254, 253, 253, 252, 251, 251, 250, 249, 248, 247, 246,
   245, 244, 242, 241, 239, 238, 236, 235, 233, 231, 229, 228, 226, 224, 221, 219,
   217, 215, 213, 210, 208, 205, 203, 200, 198, 195, 193, 190, 187, 184, 182, 179,
   176, 173, 170, 167, 164, 161, 158, 155, 152, 149, 146, 143, 139, 136, 133, 130,
   127, 124, 121, 118, 115, 111, 108, 105, 102,  99,  96,  93,  90,  87,  84,  81,
    78,  75,  72,  70,  67,  64,  61,  59,  56,  54,  51,  49,  46,  44,  41,  39,
    37,  35,  33,  30,  28,  26,  25,  23,  21,  19,  18,  16,  15,  13,  12,  10,
     9,   8,   7,   6,   5,   4,   3,   3,   2,   1,   1,   0,   0,   0,   0,   0,
     0,   0,   0,   0,   0,   0,   1,   1,   2,   3,   3,   4,   5,   6,   7,   8,
     9,  10,  12,  13,  15,  16,  18,  19,  21,  23,  25,  26,  28,  30,  33,  35,
    37,  39,  41,  44,  46,  49,  51,  54,  56,  59,  61,  64,  67,  70,  72,  75,
    78,  81,  84,  87,  90,  93,  96,  99, 102, 105, 108, 111, 115, 118, 121, 124,
   127, 130, 133, 136, 139, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173,
   176, 179, 182, 184, 187, 190, 193, 195, 198, 200, 203, 205, 208, 210, 213, 215,
   217, 219, 221, 224, 226, 228, 229, 231, 233, 235, 236, 238, 239, 241, 242, 244,
   245, 246, 247, 248, 249, 250, 251, 251, 252, 253, 253, 254, 254, 254, 254, 254,
};

//============================================================
void main(void)
{

   // MAIN LOOP
   while (1) {
      // Get USB packet
      //    Byte 0         Buffer length (command token + data bytes)
      //    Byte 1         Command token
      //    Byte 2..n-1    Data bytes
      //    Byte n         Checksum
      ReceivePacket();
      switch ( buffer[1] ) {
         // Sine wave generation token...
         // MC1408 8-Bit Multiplying Digital-to-Analog Converter
         //    buffer[2] == Time1 MSB
         //    buffer[3] == Time1 LSB
         //    buffer[4] == Time2 MSB
         //    buffer[5] == Time2 LSB
         //    buffer[6] == Time rate
         //    buffer[7] == Phase rate
         //    buffer[8] == Triangle flag
         //    buffer[9] == Voltage reference
         case FT_WAVGEN:
            S_Time1.b[1]=buffer[2];
            S_Time1.b[0]=buffer[3];
            S_Time2.b[1]=buffer[4];
            S_Time2.b[0]=buffer[5];
            S_TimeRate=buffer[6];
            S_Time.i=S_Time1.i;
            S_PhaseRate=buffer[7];
            S_Triangle=buffer[8];
            S_Phase=0;
            S_Count=0;
            TRISC=0x00;                   // set port C as output
            // Setup voltage ref configuration
            TRISAbits.TRISA2=1;
            CVRCON=buffer[9];
            // Setup CCP1 configuration
            CCPR1H=S_Time.b[1];
            CCPR1L=S_Time.b[0];
            CCP1CON=0x0B;                 // Compare mode, trigger special event
            // Setup Timer1 configuration
            TMR1H=0;
            TMR1L=0;
            T1CON=0x05;                   // 1:1 Prescale | TMR1CS | TMR1ON bits
            // Enable CCP1 interrupt
            PIR1bits.CCP1IF=0;            // CCP1 Interrupt Flag bit
            PIE1bits.CCP1IE=1;            // CCP1 Interrupt Enable bit
            INTCON=0xC0;                  // GIE and PEIE interrupts
            break;
      }
   }
}

//============================================================
void __interrupt() isr(void)
{
   if (PIE1bits.CCP1IE && PIR1bits.CCP1IF) {
      PIR1bits.CCP1IF=0;            // clear CCP1 Interrupt Flag

      if (S_Triangle) {
         if (S_Phase < 128)
            PORTC=S_Phase*2;
         else
            PORTC=(255-S_Phase)*2;
      } else {
         PORTC=cosine[S_Phase];
      }
      S_Phase+=S_PhaseRate;

      if (S_TimeRate > 0) {
         if (S_Count == 0) {
            if (S_Time2.i > S_Time1.i) {
               S_Time.i+=S_TimeRate;
               if (S_Time.i > S_Time2.i) S_Time.i=S_Time1.i;
            }
            if (S_Time2.i < S_Time1.i) {
               S_Time.i-=S_TimeRate;
               if (S_Time.i < S_Time2.i) S_Time.i=S_Time1.i;
            }
            CCPR1L=S_Time.b[0];
            CCPR1H=S_Time.b[1];
         }
         S_Count++;
      }
   }
}
« Last Edit: April 04, 2019, 06:49:32 pm by MarkF »
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #7 on: April 04, 2019, 06:31:17 pm »
the topic starter needs sine wave, I think DAC will be overkill here.
It can be done with one pin square wave generator + low pass filter.
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 1383
  • Country: us
Re: How to generate Sine wave using pic microcontroller
« Reply #8 on: April 04, 2019, 06:54:25 pm »
the topic starter needs sine wave, I think DAC will be overkill here.
It can be done with one pin square wave generator + low pass filter.

People just love to filter PWM waveforms.
Are you going to be *lazy* and filter a square wave into something approximating a sine or are you going to generate the real thing?
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #9 on: April 04, 2019, 07:18:02 pm »
Are you going to be *lazy* and filter a square wave into something approximating a sine or are you going to generate the real thing?

I'm talking NOT about pwm. I'm talking about square wave.
If you're need a real and pure sine wave, then square wave + good low pass filter is better way.
It will get you real and clean sine with no distortions.

On the other hand, with DAC you will get just a sine appoximation with a lot of spurs and distortions.

Also, if you will use DAC it will be more complicated to produce clean sine, especially if you're use PIC for that.

In any way, you will need to use low pass filter even for DAC output.
« Last Edit: April 04, 2019, 07:29:15 pm by radiolistener »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 2667
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #10 on: April 04, 2019, 07:30:08 pm »
On the other hand, with DAC you will get just a sine appoximation with a lot of spurs and distortions.

DAC w/o reconstruction filter is just fatal error, improper use of DAC in this (AC sine) case. Obviously filtered DAC DDS will be much cleaner than filtered (using same filter) square wave. Why we even have this discussion?  :-//
 

Offline JacobPilsen

  • Regular Contributor
  • *
  • Posts: 112
  • Country: cz
 

Offline apis

  • Super Contributor
  • ***
  • Posts: 1668
  • Country: se
  • Hobbyist
Re: How to generate Sine wave using pic microcontroller
« Reply #12 on: April 04, 2019, 07:38:41 pm »
It's because a (perfect) square wave can be thought of as a sum of sine waves with frequencies that are odd integer multiples of the square wave frequency. I.e. a 1 khz square wave will consist of a 1 khz sine wave + a 3 khz sine wave + a 5 khz sine wave + ...
So if you generate a 1 khz square wave and pass it through a sufficiently good low pass filter that blocks at least 3 khz and upp you will get a nice 1 khz sine wave out.

But the same thing, sort of, happens when you use a DAC/PWM + filter. DAC is "better" but more expensive and complicated.

What is best will depend on the application as usual, but it's a good idea to try the simplest solution first imo.
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #13 on: April 04, 2019, 07:43:29 pm »
will be much cleaner than filtered (using same filter) square wave

it will be true if you will use bad low pass filter and put pure and properly calculated sine on the DAC with very low INL with precise clock and delays.
But anyway, the DAC will give you non-linearity. And you will not be able to fix it with filter.
So, if you're need really clean sine, DAC will be not a good choice.
It is more easy to get clean sine wave from clean square wave.

You can put some table calculated sine, but it will require a lot of memory and may be not stable clocked and will have some jitter and non-linearity due to DAC INL and calculation precision level.
PIC is not a good choice to implement digital NCO with a clean output. It's better to use FPGA or DSP for such purposes.

On the other hand, it is very easy to get clean and stable square wave at fixed frequency on the PIC.
It doesn't needs memory, just a proper timing, which will be pretty easy on PIC.
With a proper low pass filter it will produce very clean and stable sine wave.
And this sine wave will be jitter free (limited to PIC&CLK jitter) and non-linear distortion free.
With DAC it will be much harder to eliminate jitter and non-linearity issues, especially if your'e sit on the PIC.

The filter complexity will depends on how much clean sine wave topic starter needs.

The DAC will be good choice if you will need some more complex signal than sine wave.
« Last Edit: April 04, 2019, 09:06:19 pm by radiolistener »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 2667
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #14 on: April 04, 2019, 09:45:12 pm »
But anyway, the DAC will give you non-linearity. And you will not be able to fix it with filter.
So, if you're need really clean sine, DAC will be not a good choice.
It is more easy to get clean sine wave from clean square wave.

 :palm:

You effectively say that 1-bit DAC is better than those having more bits.
« Last Edit: April 04, 2019, 10:08:56 pm by ogden »
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #15 on: April 04, 2019, 10:23:55 pm »
You effectively say that 1-bit DAC is better than those having more bits.

It depends on what you're mean "better". From non-linearity point of view - yes. In theory 1-bit DAC is better than N-bit DAC, because 1-bit DAC has better linearity.

But in the real world there are a lot of limitations, and of course - frequency limitation. 1-bit DAC requires very high frequency for the clock. But at high frequency there is higher requirement for phase noises of sampling clock oscillator, because high phase noise also leads to non-linearity.

So, in the real world, if you want to be on the cutting edge of technology, it is better to balance between high speed and bit resolution. Because you cannot get extra high level of purity just by using something one - oversampling or bit resolution. In order to get best results, you're needs to use both in balanced combination.

But this is out of scope for PIC microcontroller. If you want to get clean sine on the PIC, the most easy way is to produce clean square wave and then let it through low pass filter to cut-off high frequency harmonics.

With square wave you will get very simple and easy PIC code and clean square wave on the PIC pin. So, the signal will not have unwanted frequency components due to non-linear distortions. The output will consist just a 50 Hz sine and it's harmonics, nothing more. So, all what you need is just to cut-off unwanted high frequency harmonics with a low-pass filter. More clean sine just requires more better low pass filter.  :)

With N-bits DAC you will get non-linearity issues, which will leads to parasitic frequency components (spurs) which will be hard to filter, because these spurs will be above and below the carrier and may be very close to the carrier frequency. Also it will require to implement NCO (digital oscillator) with low jitter and high spectral purity and it will be too hard for the PIC microcontroller.

« Last Edit: April 04, 2019, 11:05:34 pm by radiolistener »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 2667
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #16 on: April 04, 2019, 10:56:47 pm »
You effectively say that 1-bit DAC is better than those having more bits.

It depends on what you're mean "better". From non-linearity point of view - yes. In theory 1-bit DAC is better than N-bit DAC, because 1-bit DAC has better linearity.

Worst possible, awfully nonlinear DAC will approximate sine better than square wave. Reconstruction filter obviously will filter amplitude nonlinearities out. This is how DDS work and what you suggest that with same filter filtered square wave will produce cleaner sine than filtered DAC output, is simply not true.

Quote
But in the real world there are a lot of limitations, and of course - frequency limitation. 1-bit DAC requires very high frequency for the clock.

Do not shift goalpost into PWM or PDM. Your suggestion was to filter fundamental out of square wave that has same frequency as target sine.
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 1383
  • Country: us
Re: How to generate Sine wave using pic microcontroller
« Reply #17 on: April 04, 2019, 11:09:11 pm »
will be much cleaner than filtered (using same filter) square wave

it will be true if you will use bad low pass filter and put pure and properly calculated sine on the DAC with very low INL with precise clock and delays.
But anyway, the DAC will give you non-linearity. And you will not be able to fix it with filter.
So, if you're need really clean sine, DAC will be not a good choice.
It is more easy to get clean sine wave from clean square wave.

You can put some table calculated sine, but it will require a lot of memory and may be not stable clocked and will have some jitter and non-linearity due to DAC INL and calculation precision level.
PIC is not a good choice to implement digital NCO with a clean output. It's better to use FPGA or DSP for such purposes.

On the other hand, it is very easy to get clean and stable square wave at fixed frequency on the PIC.
It doesn't needs memory, just a proper timing, which will be pretty easy on PIC.

With a proper low pass filter it will produce very clean and stable sine wave.
And this sine wave will be jitter free (limited to PIC&CLK jitter) and non-linear distortion free.
With DAC it will be much harder to eliminate jitter and non-linearity issues, especially if your'e sit on the PIC.

The filter complexity will depends on how much clean sine wave topic starter needs.

The DAC will be good choice if you will need some more complex signal than sine wave.

Make up your mind!

You can't use a DAC because the PIC CAN NOT generate a stable clock.
But you can use a filtered square wave because the PIC CAN EASILY generate the proper timing.

You are talking in circles.  I'm getting off this merry-go-round.   :scared:
 
The following users thanked this post: BrianHG

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #18 on: April 04, 2019, 11:13:47 pm »
Worst possible, awfully nonlinear DAC will approximate sine better than square wave. Reconstruction filter obviously will filter amplitude nonlinearities out.

No, you're wrong. Reconstruction filter is nothing else than low pass filter, it just removes high frequency components.
It cannot remove non-linear distortion products from the signal.

The problem that I talking about is non-linearity of DAC, because you cannot provide linear voltage steps for all bits in N-bits DAC. Any N-bit DAC has IMD limit.

Non-linearities leads to a new frequency components in the signal, they are all mixed with harmonics and produce a lot of distortion products. It is almost impossible to remove all of them with analog filter.

This is why DAC-based DDS chips has so dirty output spectrum in comparison with 1-bit square wave output PLL chips.
For example AD9851 has much more dirty output spectrum than Si5351.

Yes, Si5351 has high level harmonics, but you can easily remove it with simple low pass filter.
With AD9851 you don't have such option, because it's spurs everywhere and very close to the carrier.

« Last Edit: April 05, 2019, 12:06:02 am by radiolistener »
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #19 on: April 04, 2019, 11:20:28 pm »

You can't use a DAC because the PIC CAN NOT generate a stable clock.
But you can use a filtered square wave because the PIC CAN EASILY generate the proper timing.

You're don't understand what I'm talking about.
Just try to calculate sine function with maximum possible precise at realtime on the PIC and you will understand why it is hard for the PIC. This is not so easy task even for high speed FPGA with a lot of memory.

PIC can produce square wave, because you can easily calculate opcode tacts and make the simple loop to change pin state with fixed interval.

You will not be able to do that for precise sine function calculation. If you will use table, your sine purity will be limited with table size and microcontroller performance. Also it will be crazy to keep fixed intervals between samples.
« Last Edit: April 04, 2019, 11:25:33 pm by radiolistener »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 2667
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #20 on: April 05, 2019, 12:10:09 am »
This is why DAC-based DDS chips has so dirty output spectrum in comparison with 1-bit square wave output PLL chips.
For example AD9851 has much more dirty output spectrum than Si5351.

I don't know what you are talking about. Perhaps you see phase noise spectrum "smear" which are quantization (time linearity) errors and think that it is caused by amplitude nonlinearity? AD9851 have 6x frequency multiplier inside which *does* have phase noise - that's why there are spectrum shoulders. AD9850 clocked from clean 125MHz have clean output spectrum with -59dB 2nd harmonic which obviously can be filtered-away using LP filter (attach).

I will not even talk about PLL spurs of Si5351  :rant:
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #21 on: April 05, 2019, 12:16:55 am »
what you suggest that with same filter filtered square wave will produce cleaner sine than filtered DAC output

yes, this is true.
If you produce clean square wave and apply low pass filter, you will get much cleaner sine wave, than DDS can produce.

The problem here is that you cannot build low-pass filter which can be programmaticaly adjusted for wide range frequencies. This is why DDS are used. If you don't use proper low pass filter, DDS will produce smaller harmonics level, but higher non-linear distortions. It can be accepted if you're need cheap generator which can be tuned in wide frequency range.

But the topic starter asked for fixed frequency sine generator. It can be easy done with square wave + low pass filter. There is no need to change sine frequency, so there is no need to change low pass filter. :)
 

Online radiolistener

  • Frequent Contributor
  • **
  • Posts: 917
  • Country: ua
Re: How to generate Sine wave using pic microcontroller
« Reply #22 on: April 05, 2019, 12:39:23 am »
I'm pretty sure that the square wave + low pass filter will be much more easy and cheap solution to get clean sine wave for fixed frequency than 10 bit DAC + NCO implemented on the PIC (if it even will be possible to implement it on the PIC).
« Last Edit: April 05, 2019, 01:01:30 am by radiolistener »
 

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 3108
  • Country: ca
Re: How to generate Sine wave using pic microcontroller
« Reply #23 on: April 05, 2019, 12:40:28 am »
will be much cleaner than filtered (using same filter) square wave

it will be true if you will use bad low pass filter and put pure and properly calculated sine on the DAC with very low INL with precise clock and delays.
But anyway, the DAC will give you non-linearity. And you will not be able to fix it with filter.
So, if you're need really clean sine, DAC will be not a good choice.
It is more easy to get clean sine wave from clean square wave.

You can put some table calculated sine, but it will require a lot of memory and may be not stable clocked and will have some jitter and non-linearity due to DAC INL and calculation precision level.
PIC is not a good choice to implement digital NCO with a clean output. It's better to use FPGA or DSP for such purposes.

On the other hand, it is very easy to get clean and stable square wave at fixed frequency on the PIC.
It doesn't needs memory, just a proper timing, which will be pretty easy on PIC.

With a proper low pass filter it will produce very clean and stable sine wave.
And this sine wave will be jitter free (limited to PIC&CLK jitter) and non-linear distortion free.
With DAC it will be much harder to eliminate jitter and non-linearity issues, especially if your'e sit on the PIC.

The filter complexity will depends on how much clean sine wave topic starter needs.

The DAC will be good choice if you will need some more complex signal than sine wave.

Make up your mind!

You can't use a DAC because the PIC CAN NOT generate a stable clock.
But you can use a filtered square wave because the PIC CAN EASILY generate the proper timing.

You are talking in circles.  I'm getting off this merry-go-round.   :scared:

Thank-you Mark.
I never in the past had issue with making a stable clock coming out of a pic.
I never had in the past issue with interfacing a PIC to a DAC, or even a simple 8 resistor DAC on an 8 bit port.
I never had issue in the past with my 60Hz inverter's pic generated sine wave where I could software modulate the reference amplitude and also deliberately slightly amplify the waveform and squish the peaks to deal with a weak battery source to deliver a peak constant voltage.
I never had issue in the past with using a simple 1 resistor, 1 capacitor filter on the output of this DAC to make a step-less smooth sine wave output.
I never in the past had issue with software tuning this output to sync with a reference source signal, with a software adjustable tuning band range.

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, not 1 bit pwms of the telephone number dialing on a PIC smaller than a PIC16C54 which was well within Bell standards and operated with with the cheapest NTSC TV color crystal at 3.57945Mhz.  Perfectly smooth round waveforms with a simple RC filter on the 8 resistor DAC.

BG.
« Last Edit: April 05, 2019, 12:47:14 am by BrianHG »
__________
BrianHG.
 
The following users thanked this post: ogden

Offline ogden

  • Super Contributor
  • ***
  • Posts: 2667
  • Country: lv
Re: How to generate Sine wave using pic microcontroller
« Reply #24 on: April 05, 2019, 01:27:00 am »
I'm pretty sure that the square wave + low pass filter will be much more easy and cheap solution to get clean sine wave for fixed frequency

If you get filter for free then yes - easy and cheap.

Quote
than 10 bit DAC + NCO implemented on the PIC (if it even will be possible to implement it on the PIC).

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.

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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf