Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Generate a sine wave using a PIC microcontroller

Status
Not open for further replies.

engineerelectrical

Newbie level 6
Joined
Dec 22, 2016
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
105
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.
 

You need to understand that you will be able to output only a 1+sin(t) because you may not be able to show the negative side.

The most common way is to use the PWM output and convert this to a sine wave by using a simple filter.
 

Are there sample codes I can reference to for this?

- - - Updated - - -

You need to understand that you will be able to output only a 1+sin(t) because you may not be able to show the negative side.

The most common way is to use the PWM output and convert this to a sine wave by using a simple filter.

Are there sample codes I can reference to for this?
 

Hi,

Are there sample codes I can reference to for this?
Countless.
Here in this forum and many more in the internet.

Do a search for "spwm".

Klaus
 

The simplest solution might be a lookup table, it will certainly be faster...

A simple lookup table has several important advantages (apart from being faster):

1. you need to store only half of the points, the remaining points can be generated by simply negating.

2. you can get a decent sine curve with 36 or even 18 points- perhaps a simple filter at the output may be needed but most often the load itself will act as the filter.

3. You can generate phase shifts (90, 120 or 180 phase shifts are most common) simply by shifting the count base.

4. Perhaps you will get more time in the interrupt service routine (keep that simple!)
 

Depending on your requirements, if memory is very limited a very basic sine lookup table only really needs to cove 0 to 90 degrees.

And if you need much finer resolution, you can always interpolate between values stored in lookup.
 


Depending on your hardware, you may need to store the values in the integer format in the range of 0-255 (8 bit; unsigned) and as others have pointed out earlier, you need to store only for the range of 0-90 degrees. If you are using PIC with the built in PWM facility, you need not use more than 16 points (for 0-90 range) in my opinion and perhaps use a low pass filter in the hardware.
 

Hi,

The most simple is to output a square wave and use high order low pass filters to attenuate the overtones.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top