0% found this document useful (0 votes)
65 views3 pages

PWM Generation with PIC16F877A Guide

The document discusses the implementation of Pulse Width Modulation (PWM) using the 16F877A microcontroller, detailing how to generate a PWM carrier and control its duty cycle through specific registers. It includes examples of using PWM for Digital to Analog Conversion (DAC) and provides code development instructions for initializing PWM and running a simple program. Additionally, it outlines tasks for blinking an LED, implementing a DAC filter, and generating a fixed frequency sinewave.

Uploaded by

walid Qaja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views3 pages

PWM Generation with PIC16F877A Guide

The document discusses the implementation of Pulse Width Modulation (PWM) using the 16F877A microcontroller, detailing how to generate a PWM carrier and control its duty cycle through specific registers. It includes examples of using PWM for Digital to Analog Conversion (DAC) and provides code development instructions for initializing PWM and running a simple program. Additionally, it outlines tasks for blinking an LED, implementing a DAC filter, and generating a fixed frequency sinewave.

Uploaded by

walid Qaja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

P.

Klimo Jan 2012

LabC4 Pulse Width Modulation


Background

Pulse-width modulation (PWM) uses a rectangular pulse wave whose pulse width is
modulated resulting in the variation of the average value of the waveform.

The PWM carrier (see figure) has a time-base (period) and the time that the output
stays high (duty cycle).

We shall use the 16F877A to generate a PWM carrier making use of the PWM mode
of the Capture and Compare module (CCM) . The CCM1 module (the 16F877A has
two CCMs) can be configured to provide a PWM output on the pin RC2. In this mode
it generates up to a 10-bit resolution PWM carrier, the period of which is determined
by the value in PR2, the Timer2 module’s period register. The value of the PWM
period is given by

T = [(PR2)+1] x 4 x Tosc x (Timer2 Pre-scale value) (1)

The PWM Duty Cycle is specified by writing to the CCPR1L (Capture/Compare/PWM


Low) register and to the CCP1CON (Capture/Compare/PWM Control) register. The
CCP1R1L contains the eight most significant bits and the bits #5 and #4 contain the
two least significant bits. This 10 bit value is represented by

CCPR1L:CCP1CON,5:4>

The PWM Duty Cycle is given by

Duty Cycle = CCPR1L:CCP1CON,5:4> x Tosc x (Timer2 pre-scale value) (2)

The PWM generation proceeds as follows:

Although the registers CCPR1L and CCP1CON,<5:4> can be written to at all times,
the new Duty Cycle value will not take effect until the start of the next period. A new
period starts with Timer2 counter being reset to zero. This forces the output pin of
the CCM1 high and latches the value of the Duty Cycle. When the Timer 2 16-bit
counter reaches the value stored in the PR2 register the cycle ends. The Timer2
counter is automatically cleared and if the output pin is still high it will be re-set.

1
P. Klimo Jan 2012

Example: Digital to Analogue Conversion (DAC).

The average of the PWM carrier represents an analogue voltage of the binary
modulation signal. Thus in order to implement a DAC, the PWM waveform needs to
be passed through a low pass filter. This removes the components of the high
frequency carrier leaving only the low frequency modulation signal.

Consider an example where we wish to implement DAC on an 8-bit signal that has a
bandwidth from 0Hz to 1000Hz. According to the Nyquist we need to select filter’s
stop band at twice the highest signal component i.e. 2kHz. A first order RC Low Pass
filter will provide a 6dB/octave roll off. Now choose a PWM carrier frequency of 30
kHz. The RC filter will suppress the high frequency components by (30000/2000)x 6
dB = 90 dB which is a factor of about 16.

From formula (1) if the pre-scale value is 1

PR2 = Fosc/(4FPWM ) -1 = 66

but choose 64 as the nearest power of 2. Given the 8 bit signal value S we can apply
a duty cycle:

Duty Cycle = S/4

This means that only the six most significant bits of S are taken into account. Thus
the values of the two bits in the CCP1CON,<5:4> are zero and

CCPR1L = ¼ S

Code Development.

We now proceed to develop the code. The PWM mode initialization is done in the
function PWM_init:

void PWM_init(void){
TRISC &= ~0b00000100; // RC2 is CCM1 output
PR2 = 64; // Set the PWM period as 32.5 uS (30.8KHz)
T2CON = 0b000000100; // Enable Timer2 with Pre-scale=1, (no post-scale)
CCP1CON =0b000001100; // Turn the PWM mode on
}

We can run the PWM module as shown in this code extract.

volatile unsigned char value = 192; // Gives 75% Duty Cycle

void main(void){
PWM_init();
CCPR1L = value >>2; // Divide by 4 to set the Duty Cycle
while(1){;}
}

2
P. Klimo Jan 2012

TASK1: Blink LED


Write a program that blinks a LED connected to the pin RC2 at a frequency close to
1 Hz with a Duty Cycle of 25%.

TASK2: Simple DAC Filter.

Using a 1kHz PWM carrier, implement a DAC on a 10 bit binary value stored as a
variable in the Data Memory. Then design a suitable RC low pass filter to extract the
average value of the PWM carrier.

• Write a test program enabling you to change the binary value whilst the
program is running.

• Connect the RC2 pin to an oscilloscope and observe the waveform obtained.
Estimate the amplitude of the “ripple” superimposed on the DC level.

• Connect a DC voltmeter to the RC2 pin and plot the measured voltage values
against a range of binary values between 0 and 1024. Calculate the accuracy
in terms of standard and maximum deviation. Explain your findings.

TASK3: Fixed Frequency Sinewave Generation.

Use the PWM module to generate a 100 Hz sinewave with 8 bit resolution with 20
values per period. Use a suitable Low Pass filter to remove high frequency
components.

Hint: Utilize Timer0 or Timer1 to generate interrupts at fixed time increments.

Common questions

Powered by AI

In PWM for DAC applications, accuracy and precision are impacted by the PWM carrier frequency and the filter design. A higher carrier frequency, such as 30kHz, improves the DAC's ability to isolate the desired signal from high-frequency components because it allows for a more effective roll-off with a low pass filter . The design of the filter, typically an RC filter, must ensure that the cutoff frequency is suitable for the signal bandwidth while achieving sufficient attenuation of unwanted components, which is directly related to the system's accuracy. Any ripple or deviation from the expected analog signal can result in errors . Thus, precision in filter design and carrier frequency selection is critical to minimizing deviations and ensuring the DAC output accurately represents the digital input .

High accuracy in PWM-based DAC systems is challenged by digital noise and frequency selection . Digital noise, often introduced by high-frequency PWM components, needs careful filtering through optimized low-pass filter design to prevent inaccuracies in output. Selecting a high carrier frequency mitigates this issue by moving these noise components outside the passband, making them easier to filter . Variability in component properties, such as filter components, can also affect accuracy, requiring precise and often calibrated component selection . Balancing these elements maximizes DAC accuracy by ensuring a clean and stable analog representation of the digital input .

To generate a fixed frequency sine wave using the 16F877A microcontroller, the PWM module must be programmed to output a waveform that changes duty cycle in a sine pattern, often stored as a lookup table accessed periodically. Using Timer0 or Timer1, interrupts can ensure changes occur at fixed intervals. The PWM signal's rapid flucuations in duty cycle approximate a sine wave's shape over time . A low-pass filter is crucial after the PWM output to remove high-frequency components inherent in the square-like signal, allowing the pure sine wave to emerge, which can represent an 8-bit resolution sine wave using approximately 20 values per period .

PWM simulates digital-to-analog conversion by averaging the pulse width of a modulated waveform to represent an analog voltage, corresponding to the binary modulation signal . A low pass filter plays a crucial role by removing high frequency components of the PWM carrier, which is necessary to isolate the low frequency modulation signal. This filtering step is essential to smooth out the digital signal into an analog form .

The registers CCPR1L and CCP1CON,<5:4> jointly determine the PWM duty cycle by representing a 10-bit value . CCPR1L holds the eight most significant bits, while CCP1CON,<5:4> carries the two least significant bits. This combined value dictates the duration for which the PWM signal remains high during a cycle. In a DAC implementation, these registers directly affect the output voltage as the duty cycle modulates the average voltage output. A higher duty cycle increases the output voltage, emulating a signal that corresponds to the digital input .

When setting up a PWM carrier frequency for a DAC on an 8-bit signal, a key consideration is ensuring the filter’s stop band is at least twice the highest frequency component of the signal (according to the Nyquist theorem). For a signal with a 0-1000Hz range, a stop band at 2000Hz is required . Additionally, a sufficiently high PWM frequency, such as 30kHz, should be chosen to ensure the RC filter provides significant attenuation (e.g., 90 dB) of the high frequency components to effectively simulate the analog output .

To configure a PWM signal using the 16F877A microcontroller’s Capture and Compare module, the PWM period is set by writing the PR2 register, which determines the period through the equation T = [(PR2)+1] x 4 x Tosc x (Timer2 Pre-scale value). The duty cycle is configured by writing a 10-bit value across the registers CCPR1L and CCP1CON,<5:4>. The eight most significant bits are in CCPR1L and the least significant bits in CCP1CON,<5:4>. This value dictates how long the PWM signal stays high . The new duty cycle takes effect at the start of a new PWM period when Timer2 resets .

Using a 10-bit PWM resolution with an 8-bit input signal in a DAC system improves resolution and fidelity by allowing finer adjustments in the PWM duty cycle representation. Although the input is 8-bit, the additional resolution in the PWM process permits a more granular control of the voltage output, which translates to a smoother approximation of the analog signal. This improves the fidelity of the analog signal, as minor variations become more accurately reflected than would be possible with an 8-bit PWM alone . The extra resolution space overcomes potential losses from quantization errors in the DAC process .

The pre-scale value in the PWM period setting is crucial as it determines the division factor applied to the oscillator frequency before it's used in calculating the PWM period . By adjusting the Timer2 pre-scale value, finer control over the period is achieved, allowing the PWM frequency to be modified to better match application-specific needs, such as matching a signal bandwidth or enhancing DAC precision by selecting an appropriate PR2 register value for optimal frequency and period .

Programming an LED to blink using PWM involves setting up the PWM module to toggle the LED at a frequency close to 1 Hz and a duty cycle of 25% . The PWM period must be adjusted to achieve the desired 1 Hz frequency by configuring the Timer2 and PR2 appropriately. A lower duty cycle of 25% means the LED will be on for only one quarter of the cycle time, which is achieved by writing the correct values in CCPR1L and CCP1CON,<5:4> to set the duty cycle . Accurate timing critical in achieving the precise blinking effect involves ensuring that Timer2 settings and the main loop's control flow maintain consistent period lengths across cycles .

You might also like