sine wave generation with fast pwm mode 2525

8,816

sine wave generation with fast pwm mode 2525
sine wave generation with fast pwm mode 2525

Generate sinusoidal pulse width modulation (SPWM) signals using the ECCP module in a PIC to produce a sine wave output for a DC-AC inverter. There have been inquiries regarding generating similar SPWM signals with other microcontrollers lacking the ECCP module, such as the widely used PIC16F877A. Consequently, an alternative article was composed to illustrate how to generate a sine wave using SPWM with the CCP module of a PIC. This concept can be adapted for any microcontroller equipped with a PWM module. Furthermore, an example is provided for generating a sine wave using SPWM with the Atmel AVR microcontroller, specifically the ATMEGA16. However, this concept is applicable to any AVR with a PWM module. The output sine wave is intended to have a frequency of 50Hz, while a switching frequency of 16kHz for SPWM has been chosen. The process for generating sine wave using sinusoidal pulse width modulation (SPWM) signals utilizing the PWM module, commonly found in most Atmel AVR microcontrollers, is discussed. For reference, previous tutorials focused on Microchip PIC microcontrollers are available for review regarding sine wave generation. The circuit diagram for configuring the MOSFETs and their drivers, along with synchronization signals, is illustrated. The Fast PWM mode is employed, with an oscillator frequency of 16MHz and an SPWM switching frequency of 16kHz. The ICR1 is designated as the TOP value, set to 999, with a prescaler divider (N) of 1. PWM mode 14 is selected, resulting in a switching frequency. Configuration registers TCCR1A and TCCR1B are assigned values of 0x82 and 0x19, respectively, setting the PWM mode and Compare Output mode for OCR1A to non-inverting mode. A sine table is generated with a peak value of 990, ensuring it remains smaller than the TOP value. The sine table includes zeros at the beginning and end to create deadtime, preventing potential short circuits due to cross-conduction between MOSFETs in the same leg. The SPWM generation is managed by a single PWM module, with the MOSFETs activated based on a "Direction" bit and an AND gate hardware configuration. When "Direction" is 0, high side MOSFET A operates for 10ms, during which SPWM signals from the PWM (OC1A) output are directed to low side MOSFET D. Conversely, when "Direction" is 1, high side MOSFET C operates while low side MOSFET B is activated. MOSFETs A and D operate together while B and C remain off, and vice versa. Four pins are designated for the MOSFET drive signals, specifically PORTD bits 0 to 3, although any four pins could be selected. The MOSFETs are turned off before the other two are activated, as indicated in the code block. When Timer/Counter 1 reaches the TOP value, the TOV1 flag is set, and with interrupts enabled, the corresponding ISR is triggered. The table pointer retrieves necessary values from the sine table, which are assigned to OCR1AH and OCR1AL, with the 16-bit result in OCR1A determining the duty cycle.

The implementation of sinusoidal pulse width modulation (SPWM) for sine wave generation in an inverter application involves several critical components and configurations. The choice of microcontroller, in this case, the ATMEGA16, is pivotal due to its integrated PWM capabilities. The oscillator frequency of 16MHz allows for sufficient resolution in generating the SPWM signals, while the selected switching frequency of 16kHz ensures that the output waveform is smooth and suitable for AC applications.

The use of the Fast PWM mode enables efficient control of the duty cycle, which is essential for accurately reproducing the desired sine wave shape. By setting ICR1 as the TOP value and using a prescaler of 1, the PWM frequency is optimized for the application. The configuration of TCCR1A and TCCR1B registers is crucial as it defines the operational mode and the behavior of the output compare function.

The sine table, generated with a peak value of 990, is a vital element in the SPWM generation process. The inclusion of zeros at the beginning and end of the table serves to create necessary deadtime, which is critical in preventing short circuits caused by simultaneous conduction of the high and low side MOSFETs. This deadtime is particularly important in high-frequency switching applications to ensure reliable operation of the inverter.

The mechanism for directing the PWM signals to the appropriate MOSFETs through the "Direction" bit and the use of AND gates illustrates a clever hardware solution to manage the output stages of the inverter. This design allows for flexibility in controlling the output phase, facilitating the generation of a balanced AC waveform.

Overall, this approach to generating sine wave outputs using SPWM with an AVR microcontroller exemplifies the versatility of PWM technology in power electronics applications, providing a foundation for further exploration and adaptation in various microcontroller environments.Generate sinusoidal pulse width modulation (SPWM) signals using the ECCP module in a PIC for generating a sine wave output for use in DC-AC inverter. I have had requests from people asking how to generate the same SPWM signals with other microcontrollers that don`t have the ECCP module, such as the super popular PIC16F877A.

And so I had written another article where I showed how to generate sine wave using SPWM with the CCP module of a PIC. This concept, as I had mentioned in that tutorial, can be extended to use for any microcontroller that has a PWM module. And so, I`ve decided to demonstrate how to generate sine wave using SPWM with an Atmel AVR microcontroller.

The microcontroller I`ve chosen is the ATMEGA16. However, the concept can be used on any AVR that has a PWM module. The output sine wave is to have a frequency of 50Hz. I have chosen to use a switching frequency of 16kHz for SPWM. So, here I talk about how to generate sine wave using sinusoidal pulse width modulation (SPWM) signals using the PWM module as can be commonly found on most Atmel AVR microcontrollers. If you`re curious regarding my previous tutorials that revolved around the Microchip PIC microcontrollers, you should go through the other articles related to generating SPWM to get an idea of what I`m talking about regarding sine wave generation: Below (Fig.

2) is the circuit diagram for the configuration of the MOSFETs and the drivers - and the synchronization with the signals generated from Fig. 1 above. Here, I`ve used the Fast PWM mode. I chose to use an oscillator frequency (for ATMEGA16) of 16MHz and an SPWM switching frequency of 16kHz.

For this, I selected ICR1 as the TOP and assigned 999 to ICR1. I used a prescaler divider (N) = 1. I used PWM mode 14. So that gives a switching frequency of: I`ve assigned 0x82 to TCCR1A and 0x19 to TCCR1B. This sets PWM mode to mode 14. This also sets Compare Output mode for OCR1A to non-inverting mode: Clear OCR1A on compare match, clear at BOTTOM. I have selected the sine table such that the peak value is smaller than TOP. To generate the sine table, I used my software Smart Sine  with a peak value of 990. Also notice how there`s a zero at the beginning and ending of the sine table. These act to create a deadtime  so that there isn`t a short circuit due to cross-conduction between the MOSFETs in the same leg being driven.

I did this by generating a sine table with 31 values and a peak of 990 and then adding a zero at the end. See Fig. 5 below for demonstration of the deadtime. For my software "Smart Sine", visit this page: The SPWM generation is done by the single PWM module and which MOSFETs to send the signals to is set by the "Direction" bit and the hardware trick employing the AND gates.

When "Direction" is equal to 0, the high side MOSFET A is kept on for 10ms during which time the SPWM signals on PWM (OC1A) output (PORTD5) are sent to low side MOSFET D by sending a "1" to PORTD3, which, with the help of the AND gate "diverts" the OC1A signal to the low side MOSFET D (see Fig. 1 above). The same thing is achieved when "Direction" is equal to 1, just with high side MOSFET C and low side MOSFET B.

When MOSFETs A and D are operated, MOSFETs B and C are kept off and vice versa. For MOSFET drive, we need 4 pins for the 4 drive signals and I`ve chosen PORTD bits 0 to 3. I`ve arbitrarily chosen these four pins but you can choose any four pins really. The MOSFETs are first turned off before the other two are turned on, as can be seen in the code block: When Timer/Counter 1 reaches TOP, TOV1 flag is set and since interrupt is enabled, the ISR in the corresponding interrupt vector is served. There table pointer is made use of to retrieve the required values off the sine table and assign them to OCR1AH and OCR1AL the 16-bit resultant in OCR1A sets the duty cycle.

The sine table stores the duty cycle in such a way that when th 🔗 External reference