Difference between revisions of "PIC32MX: Sinusoidal Analog Output"

From Mech
Jump to navigationJump to search
Line 1: Line 1:
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.
Overview:


How do we obtain analog output? ie PWM (brief description)


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.
How do we get sinusoidal output? ie divide up sinusoid into N points, scale duty cycle accordingly


General discussion about how cutoff frequency, number of points period (N) and the RC time constant matter.


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.
State that the examples here have PWM at 20kHz.


The examples below have a PWM frequency of 20kHz.



__TOC__
__TOC__

Revision as of 18:30, 2 May 2010

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.

This demonstrates what happens when you pick too many points. The output signal has a peak occurring at the 0V area. This output signal used 500 points at 10Hz.
This demonstrates what an ideal analog output should look like. This output was generated using 100 points at 25Hz.
This demonstrates what happens when you select too few points. The output signal shows the capacitor charging. In this case, 10 points at 25Hz are used in generating the analog output.

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.

The sine waves were generated at 25Hz with 100 points. The sine wave labeled 1 was generated with an RC constant of 4.7 x 10^-5, the sine wave labeled 2 was generated using an RC constant 2.2 x 10^-4 and the sine wave labeled 5 was generated using an RC constant 4.7 x 10^-3.

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.