View previous topic :: View next topic |
Author |
Message |
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
Generate a sine wave using a PIC microcontroller |
Posted: Thu Dec 22, 2016 1:44 pm |
|
|
I am using a PIC18f6722 microcontroller and I want to generate a sine wave at 60Hz. The complier I am using is CCS C. I have the PIC hooked up to a function generator so I can see the sine wave on the oscilloscope. I want to write a code that re-creates the same sine wave on CCS C so I can output it on labVIEW.
I am new to programming microcontrollers so I am not sure where to start. Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 22, 2016 10:34 pm |
|
|
Do you have to use that PIC ? It doesn't have a built-in DAC. If you use
the 18F46K22, it has a DAC with 32 levels. |
|
|
drolleman
Joined: 03 Feb 2011 Posts: 116
|
|
Posted: Thu Dec 22, 2016 11:36 pm |
|
|
in the example directory is ex_sine.c |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Fri Dec 23, 2016 1:44 am |
|
|
and (of course), a lot depends on what you actually want to 'do' with the resulting sinusoid.
For instance, in terms of 'power delivery', you can get a sinusoidal behaviour using a modulated PWM. However to give an actual sinusoidal waveform from this, requires hardware filtering. This is what is commonly used to simulate sinusoids for things like synchronous motors.
Choose a PIC with a DAC, and as PCM_programmer points out you can then get a 'stepped' sinusoid.
Add a better DAC, and the step sizes get smaller.
More data needed before we can really help.... |
|
|
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
|
Posted: Fri Dec 23, 2016 11:34 am |
|
|
PCM programmer wrote: | Do you have to use that PIC ? It doesn't have a built-in DAC. If you use
the 18F46K22, it has a DAC with 32 levels. |
Yeah I have to use that PIC for this :/ |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Dec 23, 2016 11:38 am |
|
|
How much harmonic purity do you need?
How much frequency accuracy?
How much jitter can you stand?
Is this a school assignment " i did it" project
or for a " real thing" ? |
|
|
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
|
Posted: Fri Dec 23, 2016 11:41 am |
|
|
asmboy wrote: | How much harmonic purity do you need?
How much frequency accuracy?
How much jitter can you stand?
Is this a school assignment " i did it" project
or for a " real thing" ? |
Yeah it's a school project. What my professor said is that for now I should just focus on getting a sine wave output on labVIEW and that I can worry about the filters and everything else later. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 23, 2016 11:56 am |
|
|
Quote: | I can worry about the filters and everything else later. |
Considering that the PIC you're using only has i/o ports and 2-level (high
and low) hardware PWM, and can't make an analog waveform, how do
you propose to make analog sine wave without external filtering ?
Here on Roman Black's page, he does it, but he has external filtering:
http://www.romanblack.com/onesec/Sine1kHz.htm
See his schematic. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Dec 23, 2016 2:04 pm |
|
|
Everyone who has replied to this thread - does this stuff "for real"
and the direction, suggestions and questions are all of the sort you need
to answer when doing it "for real".
What we have all been telling you is that
the choice of Pic matters.
And the specifications for the sine wave product matter in the real world.
if working with analog signals and micro controllers is to become your career -
then the things being pointed out and questions posed - will need to be answered for you to make products that work as intended. |
|
|
Arakel
Joined: 06 Aug 2016 Posts: 107 Location: Moscow
|
|
Posted: Sat Dec 24, 2016 11:38 am |
|
|
How much harmonic purity do you need?
How much frequency accuracy?
How much jitter can you stand?
Is this a school assignment " i did it" project
or for a " real thing" ?
I am interested in this for myslef. This is a real thing for me since I want to become an embedded programmer but there are serious wholes in my knowledge.
Can I make the harmonics mostly of one type with a programmable filter like crawling middle?
The frequency accuracy depends on the outisde RC filter meaning probably 1 percent standart, but for medical electronics accurate measurements I do not know how much it should be?
I do not know anything about the jitter. Perhaps it is the same as in computer systems where they use Latch registers and 95% gitter is not acceptable for me. I hope PIC use FLIP-FLOP and not latch.
I use PIC16F690 for now.
If anyone can help me make this simple project I would appreciate it.
1. Generate a signal with a microprocessor
2. Low frequency square impulse filter with a microprocessor.
Tasks needed: generate a signal with the microprocessor and put a filter
2. Descreet Furie transformation with a microprocessor
Thank you in advance! _________________ Yo! I love learning and technology! I just do not have experience so do not be angry if I ask a stupid question about a detail! From so much to remember sometimes I forget the details in order to remember the big problems! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Dec 24, 2016 1:06 pm |
|
|
Taking the issues one at a time.
a PIC PWM generated square wave can have jitter no worse than the scaled jitter of the PIC master oscillator - but if using the internal oscillator it might NOT be low enough for all applications
if you generate a square wave and run it through a butterworth response sallen key 2nd order low pass filter - with the corner frequency set at
F0 of your oscillator frequency - the fundamental freq is -3db down
and you will still have a second harmonic content only -6db below that.
summed harmonics through the 5th leave you with all harmonics down only -4.66db at the filter output - ( pathetically distorted
Higher order multistage filters can help but never get you a quality sine wave.
The way to make clean sine waves ( less than -40db harmonics ie 1% ) is NOT found in PIC hardware
if you want low harmonics ( like .1% or better -60db down) you MUST generate the waveform in a clean way to start.
"the way" is external components and to use the PIC to control them.
here is an example
http://www.ccsinfo.com/forum/viewtopic.php?t=53564&highlight=9833
In a medical instrument that uses this 3 oscillator system to implement a Lock In detector - i share the 8.388608 mhz PIC xtal clock and the 9833's with the
9833 clock in connected to the PIC osc out pin jitter can't be easily measured against the MO...
Last edited by asmboy on Sat Dec 24, 2016 1:18 pm; edited 1 time in total |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Dec 24, 2016 1:17 pm |
|
|
You have a mispelling:
search the forum for :
FOURIER
not furie |
|
|
Arakel
Joined: 06 Aug 2016 Posts: 107 Location: Moscow
|
|
Posted: Sat Dec 24, 2016 1:24 pm |
|
|
I wish I could say I understand all that, but I dont :D. I am not an expert, I do everything by myself.
Lets summarize:
1. To generate a precise SIN wave I need better components, perhaps DSP PIC. An external filter of Sallen Key level 2. Good external oscilator, well packed PCB so I do not have interferances.
2. I can create it by controlling external analog components with a microcontroller but by my opinion that is not good because I can do it easily with no microprocessor and that would be the SIN wave generator block.
I have a question if a SIN wave is recorded in the memory how accurate will it be produced through 8 pins or 16 pins and an external DAC?
How accurate will it be produced through an internal DAC?
I know the PWM is used for generating square impulses for controlling transitors for motors in key mode but I do not know what are the recomendations for generating a SIN wave.
Am I right so far? It will take time to review this. _________________ Yo! I love learning and technology! I just do not have experience so do not be angry if I ask a stupid question about a detail! From so much to remember sometimes I forget the details in order to remember the big problems! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Dec 24, 2016 3:01 pm |
|
|
Quote: | I have a question if a SIN wave is recorded in the memory how accurate will it be produced through 8 pins or 16 pins and an external DAC?
How accurate will it be produced through an internal DAC? |
The detail of a ROM image for sine generation must match the bit resolution of the dac - but remember you only need 90 degrees worth of SIN data .
the bigger problem is that clocking out program memory based CONST data
will have jitter and timebase shift when there are interrupts ...
the ad9833 basically does this at 10 bit resolution to get appx -60db (-56 actually up to 7th harmonic ) while an 8 bit dac is 20 db WORSE and with a worse spectral harmonic power distribution than the 10 bit 9833 as well.
short answer - you are not going to get very clean sine waves with a table inside the PIC either .
I don't see a way to do it. |
|
|
Arakel
Joined: 06 Aug 2016 Posts: 107 Location: Moscow
|
|
Posted: Sat Dec 24, 2016 3:12 pm |
|
|
I know that this information is gold and I am angry that I can not understand it!
Thank you! I do not quite get the decibels, I do not know exactly how much a decibel is because it is an equesion( ratio). I think it should be something like
the higher the decibels the closer do we get to "1" meaning to the idealistic result? I guess minus decibels are from minus to the idalistic result?
So a 16 bit DAC should do the job especially if there is one specialized for SIN wave conversion( I do not know if there is one)? But it still depends on the components accuracy. I know it is done somehow now a days so I have to find how.
Should it not be 180 degrees? _________________ Yo! I love learning and technology! I just do not have experience so do not be angry if I ask a stupid question about a detail! From so much to remember sometimes I forget the details in order to remember the big problems! |
|
|
|