How to generate Sine wave using pic microcontroller - Page 1
Forum section: electronics / general

author: khatus
author_rank: Regular Contributor
timestamp: April 04, 2019, 02:35:44 pm
content: Hello guys i want to generate Sine wave using pic 18f2550 microcontroller using timer0 .But how i can implement this??

author: rstofer
author_rank: Super Contributor
timestamp: April 04, 2019, 03:12:49 pm
content: 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-dacPWM is more involved...

author: mikerj
author_rank: Super Contributor
timestamp: April 04, 2019, 03:18:04 pm
content: 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.

author: khatus
author_rank: Regular Contributor
timestamp: April 04, 2019, 03:26:30 pm
content: 50Hz and 10bit adc

author: Mechatrommer
author_rank: Super Contributor
timestamp: April 04, 2019, 03:54:01 pm
content: http://electronics-diy.com/arduino-sinewave-generator.php

author: radiolistener
author_rank: Super Contributor
timestamp: April 04, 2019, 04:21:56 pm
content: Quote from: khatus on April 04, 2019, 03:26:30 pmwhat 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 sineBut 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.

author: MarkF
author_rank: Super Contributor
timestamp: April 04, 2019, 05:43:52 pm
content: 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:#include <xc.h>#include <stdint.h>uint8_t buffer;typedef union {uint8_t b;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 ={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 LOOPwhile (1) {// Get USB packet// Byte 0 Buffer length (command token + data bytes)// Byte 1 Command token// Byte 2..n-1 Data bytes// Byte n ChecksumReceivePacket();switch ( buffer ) {// Sine wave generation token...// MC1408 8-Bit Multiplying Digital-to-Analog Converter// buffer == Time1 MSB// buffer == Time1 LSB// buffer == Time2 MSB// buffer == Time2 LSB// buffer == Time rate// buffer == Phase rate// buffer == Triangle flag// buffer == Voltage referencecase FT_WAVGEN:S_Time1.b=buffer;S_Time1.b=buffer;S_Time2.b=buffer;S_Time2.b=buffer;S_TimeRate=buffer;S_Time.i=S_Time1.i;S_PhaseRate=buffer;S_Triangle=buffer;S_Phase=0;S_Count=0;TRISC=0x00; // set port C as output// Setup voltage ref configurationTRISAbits.TRISA2=1;CVRCON=buffer;// Setup CCP1 configurationCCPR1H=S_Time.b;CCPR1L=S_Time.b;CCP1CON=0x0B; // Compare mode, trigger special event// Setup Timer1 configurationTMR1H=0;TMR1L=0;T1CON=0x05; // 1:1 Prescale | TMR1CS | TMR1ON bits// Enable CCP1 interruptPIR1bits.CCP1IF=0; // CCP1 Interrupt Flag bitPIE1bits.CCP1IE=1; // CCP1 Interrupt Enable bitINTCON=0xC0; // GIE and PEIE interruptsbreak;}}}//============================================================void __interrupt() isr(void){if (PIE1bits.CCP1IE && PIR1bits.CCP1IF) {PIR1bits.CCP1IF=0; // clear CCP1 Interrupt Flagif (S_Triangle) {if (S_Phase < 128)PORTC=S_Phase*2;elsePORTC=(255-S_Phase)*2;} else {PORTC=cosine;}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;CCPR1H=S_Time.b;}S_Count++;}}}

author: radiolistener
author_rank: Super Contributor
timestamp: April 04, 2019, 06:31:17 pm
content: 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.

author: MarkF
author_rank: Super Contributor
timestamp: April 04, 2019, 06:54:25 pm
content: Quote from: radiolistener on April 04, 2019, 06:31:17 pmPeople 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?

author: radiolistener
author_rank: Super Contributor
timestamp: April 04, 2019, 07:18:02 pm
content: Quote from: MarkF on April 04, 2019, 06:54:25 pmI'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.

author: ogden
author_rank: Super Contributor
timestamp: April 04, 2019, 07:30:08 pm
content: Quote from: radiolistener on April 04, 2019, 07:18:02 pmDAC 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?

author: JacobPilsen
author_rank: Regular Contributor
timestamp: April 04, 2019, 07:31:20 pm
content: Tahmid: SPWM on PIC16F

author: apis
author_rank: Super Contributor
timestamp: April 04, 2019, 07:38:41 pm
content: 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 04, 2019, 07:43:29 pm
content: Quote from: ogden on April 04, 2019, 07:30:08 pmit 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 digitalNCOwith 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.

author: ogden
author_rank: Super Contributor
timestamp: April 04, 2019, 09:45:12 pm
content: Quote from: radiolistener on April 04, 2019, 07:43:29 pmYou effectively say that 1-bit DAC is better than those having more bits.

author: radiolistener
author_rank: Super Contributor
timestamp: April 04, 2019, 10:23:55 pm
content: Quote from: ogden on April 04, 2019, 09:45:12 pmIt 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.

author: ogden
author_rank: Super Contributor
timestamp: April 04, 2019, 10:56:47 pm
content: Quote from: radiolistener on April 04, 2019, 10:23:55 pmWorst 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.QuoteDo not shift goalpost into PWM or PDM. Your suggestion was to filter fundamental out of square wave that has same frequency as target sine.

author: MarkF
author_rank: Super Contributor
timestamp: April 04, 2019, 11:09:11 pm
content: Quote from: radiolistener on April 04, 2019, 07:43:29 pmMake 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 04, 2019, 11:13:47 pm
content: Quote from: ogden on April 04, 2019, 10:56:47 pmNo, 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 04, 2019, 11:20:28 pm
content: Quote from: MarkF on April 04, 2019, 11:09:11 pmYou'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.

author: ogden
author_rank: Super Contributor
timestamp: April 05, 2019, 12:10:09 am
content: Quote from: radiolistener on April 04, 2019, 11:13:47 pmI 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

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 12:16:55 am
content: Quote from: ogden on April 04, 2019, 10:56:47 pmyes, 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 12:39:23 am
content: 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).

author: BrianHG
author_rank: Super Contributor
timestamp: April 05, 2019, 12:40:28 am
content: Quote from: MarkF on April 04, 2019, 11:09:11 pmThank-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.

author: ogden
author_rank: Super Contributor
timestamp: April 05, 2019, 01:27:00 am
content: Quote from: radiolistener on April 05, 2019, 12:39:23 amIf you get filter for free then yes - easy and cheap.QuoteNumerous 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 yousaysaid that effect is visible below -75dB level.

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 01:34:21 am
content: Quote from: BrianHG on April 05, 2019, 12:40:28 amThere 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 wavewith good spectrum purityin realtime on the PIC, especially when NCO frequency cannot divide the clock frequency with integer.Quote from: BrianHG on April 05, 2019, 12:40:28 amThere 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 keyIf DTMF code is detected it doesn't means that the signal consists of two pure clean sine waves and nothing more

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 03:28:07 am
content: Quote from: ogden on April 05, 2019, 01:27:00 amyes with hardcoded 20 values and 1000 Hz ISR it will works.Quote from: ogden on April 05, 2019, 01:27:00 amWith 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 betterBut 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.

author: soldar
author_rank: Super Contributor
timestamp: April 05, 2019, 08:00:58 am
content: 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

author: ogden
author_rank: Super Contributor
timestamp: April 05, 2019, 10:40:24 am
content: Quote from: radiolistener on April 05, 2019, 03:28:07 amNice & 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 toAD 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.

author: BrianHG
author_rank: Super Contributor
timestamp: April 05, 2019, 08:08:18 pm
content: Quote from: radiolistener on April 05, 2019, 01:34:21 amMy 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.)

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 08:48:51 pm
content: Quote from: ogden on April 05, 2019, 10:40:24 amyes, with usingthe 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 allSo, 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 easyInstead 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 09:18:25 pm
content: Quote from: BrianHG on April 05, 2019, 08:08:18 pmI 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.Quote from: BrianHG on April 05, 2019, 08:08:18 pmOf 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

author: ogden
author_rank: Super Contributor
timestamp: April 05, 2019, 09:41:26 pm
content: Quote from: radiolistener on April 05, 2019, 08:48:51 pmRare 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?QuoteWe shall not introduce 1-bit oversampling DAC confusion such asDirect Stream Digitalbecause it is way too different story. Here we talk about filtering fundamental frequency out of square wave, right?

author: hamster_nz
author_rank: Super Contributor
timestamp: April 05, 2019, 09:59:57 pm
content: I hope that the 50Hz needle filter with a 2Hz pass band can be made with the spread of 1% tolerance parts...

author: ogden
author_rank: Super Contributor
timestamp: April 05, 2019, 10:54:50 pm
content: Quote from: hamster_nz on April 05, 2019, 09:59:57 pmGood 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 10:57:58 pm
content: Quote from: ogden on April 05, 2019, 09:41:26 pmSince 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).Quote from: ogden on April 05, 2019, 09:41:26 pmI 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.Quote from: ogden on April 05, 2019, 09:41:26 pmYes, 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 waveAnd yes, we can propose topicstarter with a third way to produce sine waveHe 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.Quote from: ogden on April 05, 2019, 10:54:50 pmIt 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 05, 2019, 11:30:42 pm
content: 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

author: Zero999
author_rank: Super Contributor
timestamp: April 06, 2019, 06:17:37 pm
content: Quote from: radiolistener on April 05, 2019, 11:30:42 pmThe main advantages of using an MCU would be accuracy and cost.

author: alex.martinez
author_rank: Contributor
timestamp: April 06, 2019, 11:58:03 pm
content: Quote from: radiolistener on April 04, 2019, 04:21:56 pmThe 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...

author: soldar
author_rank: Super Contributor
timestamp: April 07, 2019, 10:11:43 am
content: Quote from: alex.martinez on April 06, 2019, 11:58:03 pmI 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.

author: radiolistener
author_rank: Super Contributor
timestamp: April 07, 2019, 10:36:52 am
content: Quote from: soldar on April 07, 2019, 10:11:43 amyeah, square wave is not modified sine wave. It is original sine wave, but it is mixed with other higher frequency sine waves

author: soldar
author_rank: Super Contributor
timestamp: April 07, 2019, 11:08:01 am
content: "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.

author: Zero999
author_rank: Super Contributor
timestamp: April 07, 2019, 02:20:30 pm
content: 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.

author: soldar
author_rank: Super Contributor
timestamp: April 07, 2019, 02:41:44 pm
content: Quote from: Zero999 on April 07, 2019, 02:20:30 pmDon'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.