Re: PIC32 or PIC18 How to creat a sine wave2017/07/12 07:03:37
(permalink)

3 (1)

HiThe pic does not have a D to A so you will have to use a pulse width modulated output PWM and a low pass filter. Both devices have PWM. To generate the number used in the PWM you can use floating point math lib sinf( Index) where a for loop can update the index or a look up table of the resolution you wish to have. I believe sinf() is in radians there are 2pi radians in 360 degrees

Re: PIC32 or PIC18 How to creat a sine wave2017/07/12 07:07:47
(permalink)

3 (1)

Quite an open-ended question...First you need a D/A converter, what resolution do you want? signal to noise ratio?Do you want variable frequency sine-wave or just fixed 20kHz tone burst?

To get a 20kHz sine wave you need to provide samples to the DAC quite a lot faster than that, say 50x faster if you want low noise so you are talking about an update rate of maybe 1MHz, this is fast even for a PIC32 to manage but it is possible if you scan through a table of pre-calculated samples.

Of course you could use the standard CD rate of 44.1 kHz but then you would need a very good "brick wall" filter at 21kHz, and the ability to switch the burst on and off at the zero crossing points would be much more difficult

If you want to get useful help you really need to provide a much clearer idea of what you are trying to design or people will just pass by...

Re: PIC32 or PIC18 How to creat a sine wave2017/07/12 07:30:59
(permalink)

4 (1)

PhilipJTo get a 20kHz sine wave you need to provide samples to the DAC quite a lot faster than that, say 50x faster if you want low noise so you are talking about an update rate of maybe 1MHz, this is fast even for a PIC32 to manage but it is possible if you scan through a table of pre-calculated samples.

If you don't need to do anything else during this 0.5ms, then any PIC with DAC will do. You can do 1MS/s on PIC16 (50 points per sine period) without much work, more if you try hard. You can easily do 2Ms/s (100 points per period) on PIC18.

Re: PIC32 or PIC18 How to creat a sine wave2017/07/12 07:59:25
(permalink)

3 (1)

Note that PWM produces significant distortion due to phase modulation of the signal. You may be better off with a 6 bit R-2R ladder network. Else add a decent DAC. Audio codecs will give very good performance as they normally have good post conversion filters included. The TI TLV320AIC23B supports up to 96kHz sample rate, so 20kHz is well within reach. Use a DDS sin generator.

You could program an EPROM (or SRAM) with 8-bit data corresponding to the sine wave, and the output could connect to a parallel DAC. The address lines could be a port on the PIC, or from a binary counter that can be clocked as needed for the frequency. With 180 data points each would correspond to 2 degrees, and to get 20 kHz would require a clock rate of 180 * 20k = 3.6 MHz.

The PIC16F1777/8/9 has a 10 bit DAC. It should be possible to store 180 8-bit data points in flash memory and write only to the DACxREFH register to use just a single instruction cycle (plus cycles to increment a counter at the desired rate).

It may not be necessary to use so many data points if a good bandpass filter is used, but it may affect the waveform when it starts and stops.

Re: PIC32 or PIC18 How to creat a sine wave2017/07/12 19:00:45
(permalink)

4 (1)

if all you need is a Sine wave at at a given frequency its a simple exercise. Create a square wave.. ( easy, just bit bang a stream of 1010101.... ) and then place a 12db filter after it.. there will be not a lot of the higher order fundamentals left ).. you could use a pretty elementary pic for this..

Of course this might not fill all the requirments but we are just guessing.

Re: PIC32 or PIC18 How to creat a sine wave2017/07/12 21:19:37
(permalink)

3 (1)

PIC18F4520, aside of being old, doesn't have a DAC. You need a PIC with DAC. Many PIC16F1* have 5-bit DACs. If this is not enough, there are many with 8-bit DAC, such as PIC16F1704, or 10-bit DAC such as PIC16F1764.

Re: PIC32 or PIC18 How to creat a sine wave2017/07/16 07:48:18
(permalink)

0

Hello [class="goog-text-highlight"]PStechPaulCan you explain the depth of your answer?I want to know how to practice this method ,Such as how the program architecture I want to designMy conditional type can arbitrarily produce a set of 20 khz frequency sine waves