AnsweredHot!PIC32 or PIC18 How to creat a sine wave

Page: 1234 > Showing page 1 of 4
Author
tommylee
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2017/07/11 01:57:47
  • Location: 0
  • Status: offline
2017/07/11 20:17:44 (permalink)
0

PIC32 or PIC18 How to creat a sine wave

hello everyone I have a question
I want to create a sine wave (20kHz,0.5ms)

I do not know what to do
I hope someone can help me
(PS. the picture is I use an arbitrary waveform generator but I want to use PIC to replace it)
post edited by tommylee - 2017/07/19 00:41:31
#1
nigel gaia
Starting Member
  • Total Posts : 55
  • Reward points : 0
  • Joined: 2015/02/19 04:08:17
  • Location: 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 07:03:37 (permalink)
3 (1)
Hi
The 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
#2
qhb
Superb Member
  • Total Posts : 4752
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 07:05:46 (permalink)
4 (2)
The OP has not specified any particular PIC, so how do you know it doesn't have a DAC?
 
#3
PhilipJ
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2010/07/06 06:14:40
  • Location: 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/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...
 
hope this helps
PhilipJ
 
#4
nigel gaia
Starting Member
  • Total Posts : 55
  • Reward points : 0
  • Joined: 2015/02/19 04:08:17
  • Location: 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 07:23:04 (permalink)
0
Point Taken. I have not used a PIC18 with a DAC did not know they even existed but I have not looked at all 
variants of PICs. All PICs that I have used have PWM.
#5
NorthGuy
Super Member
  • Total Posts : 4245
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 07:30:59 (permalink)
4 (1)
PhilipJ
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.



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.
 
#6
rpg7
Super Member
  • Total Posts : 1305
  • Reward points : 0
  • Joined: 2003/11/07 12:47:35
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/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.
#7
bosco
Super Member
  • Total Posts : 2072
  • Reward points : 0
  • Joined: 2004/07/24 10:17:53
  • Location: Idaho
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 08:47:14 (permalink)
3 (1)
There are some 33F that have a builtin DAC that could do this with DMA
 
#8
mbrowning
Super Member
  • Total Posts : 389
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 08:55:26 (permalink)
0
How about output a square wave and use a good filter - maybe using the opamps in the PIC itself?
 

Mark
#9
CinziaG
rome burns :D
  • Total Posts : 2445
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 09:28:37 (permalink)
0
mbrowning
How about output a square wave and use a good filter - maybe using the opamps in the PIC itself?
 




naaaah, he can do better than this Smile
#10
NorthGuy
Super Member
  • Total Posts : 4245
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 09:30:48 (permalink)
0
Of course you need a PIC with a DAC. There are lots of them in every family. PIC16/PIC18 is perfect for the job. PWM is not a good idea. Audio-DAC in dsPIC33 is PWM-based and too slow for the task.
#11
PStechPaul
Super Member
  • Total Posts : 1160
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: online
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 17:32:45 (permalink) ☼ Best Answerby tommylee 2017/07/16 07:32:10
3 (1)
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.

 
#12
mrpackethead
Super Member
  • Total Posts : 460
  • Reward points : 0
  • Joined: 2007/04/01 23:33:39
  • Location: 0
  • Status: online
Re: PIC32 or PIC18 How to creat a sine wave 2017/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.
 
#13
tommylee
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2017/07/11 01:57:47
  • Location: 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 20:35:42 (permalink)
0
Thanks everyone Smile: Smile
have any example CODE for this I can refer to it
because I want to test by demo board App025
thank you too much
#14
qhb
Superb Member
  • Total Posts : 4752
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 21:02:16 (permalink)
3 (1)
Why didn't you mention what PIC you have?
After a bit of Googling, I think an APP025 is an ancient demo board with a PIC18F4520 on it.
Is that what you have?
 
#15
tommylee
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2017/07/11 01:57:47
  • Location: 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/12 21:12:55 (permalink)
0
Yes the demo board App025  with a PIC18F4520 on it
I just want to do test  to verify that it is feasible
I also have PIC32 
#16
NorthGuy
Super Member
  • Total Posts : 4245
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/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.
#17
tommylee
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2017/07/11 01:57:47
  • Location: 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/16 07:48:18 (permalink)
0
Hello [class="goog-text-highlight"]PStechPaul
Can 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

Thank you very much
#18
CinziaG
rome burns :D
  • Total Posts : 2445
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/16 08:40:25 (permalink)
0
I'd say it is the "old way" for generating high frequencies waves, way before microcontrollers came:
connect a bunch of 7490 to an EEprom and alike, and let it sweep through all of the available data.
 
In present days I'd do that just with a PIC and its (or an external one) DAC Smile
#19
dan1138
Super Member
  • Total Posts : 2490
  • Reward points : 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/16 17:03:26 (permalink)
3 (1)
The Original Poster: tommylee has what seems to be a development board that was available from Microchip in Taiwan called the APP025.

At this link you can find a sample project and schematic.

I found references to the APP025 Demo Board on several educational web sites.

Perhaps many university students were required to purchase these demo boards as part of a course on embedded code development.

In any case there are a wide variety of 40-pin DIP package Microchip devices that are pin compatible with this demo board.

I do not know how many have a DAC function block, though the R-2R ladder network can be used on an PIC with an output port of 8-bits.

See application note AN655 for a good description of using a PWM and DAC for Sine wave generation.

The notion of using a PWM or R-2R ladder DAC is irrelevant because the APP025 has an SPI 12-bit DAC (MCP4921) as part of the on board resources.
#20
Page: 1234 > Showing page 1 of 4
Jump to:
© 2017 APG vNext Commercial Version 4.5