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.
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!)
You can have look at the basic working theory @ http://www.ocfreaks.com/sine-wave-generator-using-pwm-lpc2148-microcontroller-tutorial/ Its implemented for LPC2148 but the underlying working is the same. You need to generate or use a hardcoded sine lookup table which feeds values to your PWM block and at the PWM output use a filter which acts a DAC converting your varying PWM output to sinewave output.
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.