PIC32MX: Sinusoidal Analog Output
The PIC32 is actually incapable of directly creating an analog output. Instead, the PIC32 can perform pulse width modulation (PWM) in combination with an external RC circuit to create the analog output. Essentially, PWM varies the time that the output pin is on (output is 3.3V) and off (output is 0V) to create the duty cycle which can simulate voltages between 0V and 3.3V. For example, if the duty cycle puts out 3.3V for 50% of the time and 0V for the other 50%, the effective output voltage averages out to be 1.65V. Thus, to create the sinusoidal output, the sine wave must be divided into a number of points so that the PIC32 will know what duty cycle to output at what time. In essence, this method is similar to connecting the dots.
The three most important parameters in creating the sinusoidal analog output are the cutoff frequency, the number of points per period, and the RC time constant. The combination of the cutoff frequency and the points per period helps tell the PIC when to change the duty cycle. The RC constant of an external RC circuit is important in creating smooth and accurate curves and in improving the quality of the output signal.
The code for the analog output will first generate a lookup table. This table contains values for the specific duty cycles and will generate the number of duty cycles based on the number of points specified by the user. After generating the table, the code goes through a loop which tells the board to output the appropriate PWM corresponding to the duty cycle in the lookup table at each time step. By looping through the duty cycles at the same time intervals, the output voltage is varied to match that of a sine wave. The external RC circuit acts as a buffer to store these voltages to smooth the final output voltage to yield the sine wave. Without this external RC circuit, the output would only be a series of pulses matching the duty cycle at a given time.
The examples below have a PWM frequency of 20kHz.
Cutoff Frequency
How to choose cut-off frequency -- what is the equation?
Show picture if you choose your frequency to high and if you choose your frequency to low.
Say something about how the amplitude decreases with high frequency
Number of Points Per Period
Selecting the number of points to use to generate the sine wave is important because choosing too few will result in a very rough sine wave. When the number of points is too low, you can actually see multiple RC charging graphs between each point. However, if we choose too many points, a peak appears at what should be 0V on the sine wave. To prevent this peek from occurring, one could insert an “if” statement in the lookup table generation loop that replaces all of the occurring 0’s into 1’s. Inclusion of the if statement would result in no true 0V output for that particular output. The ideal number of points for 25Hz is around 200 points.
RC Time Constant
The RC time constant plays a big role in creating the sine wave. By increasing the RC constant, the quality of the output signal is increased, where "quality" refers to the width of the output signals. Despite having such advantages, a larger RC constant will result in a phase lag which causes a delay between the output of the PIC and the output from the whole circuit.
Sample Code
Include your sample code (this may need cleaned up a bit).
To make a box around the code, put a 'space' in front of each line.