Programming Timer Counter in Phase Correct PWM mode

Phase Correct PWM mode is one of the four mode of operation that can be used with AVR microcontroller. The other modes includes the normal mode, the CTC mode and the Fast PWM mode. These other mode were explained previously:

- Programming Timer Counter in Fast PWM mode

Atmega32A Timer Counter Tutorial Examples

 ATmega32A microcontroller is used here to illustrate how the Phase correct PWM works. There are two modes of Phase Correct PWM- the non-inverted mode and inverted mode. The microcontroller must be a prior configured which mode of Phase Correct PWM to use. Then a count value is loaded into the OCR0 register. The count value depends upon what duty cycle of the output PWM wave is required. The PWM wave will be available at pin OC0 pin which is PORT B pin 3 when using Timer/Counter0. Once OCR0 register is loaded. The timer is started and the timer counts up. When it reaches the OCR0 value match occurs. When the match occurs, in case of non-inverted phase correct PWM, the pin OC0 is set low and the timer continues to count up to top value of 0xFF and then continues to counts down. When counting down, match occurs when it reaches the count value in OCR0 register at which point the the OC0 is set. Then the same process is repeated and this generates non-inverted PWM signal at OC0. The opposite happens when the inverted phase correct PWM is used. During upcounting, when the value of Timer becomes equal to OCR0, the OC0 pin is set high. The timer continues to count up until top value of 0xFF is reached and then continues to count down. During the down counting when the count is equals to OCR0 value, the OC0 is set low. This process is repeated which generates the inverted PWM signal at the OC0 pin.


Programming Timer/Counter0 in Phase Correct PWM mode involves deciding on the frequency of the output phase correct PWM signal and deciding upon the duty cycle of the output PWM wave. These are required to configure the microcontroller. Once the required PWM  frequency and duty cycle is decided the OCR0 can be loaded with proper value, the microcontroller configured in the inverted or non-inverted phase correct PWM mode and proper pre-scalar configured. Then the timer is started which then automatically generates phase correct PWM wave on pin OC0.

 

Programming in Non-Inverted PWM mode

The phase correct PWM wave frequency for Timer0 is calculated using the following equation.

 

\(F_{w}=\frac{Fosc}{510N}\)        ---------------------->(1)


where, Fosc is the crystal oscillator frequency and N is the pre-scalar

For example, with 4MHz oscillator frequency and no-prescalar N=1, the frequency of the PWM wave is 7.84KHz. This can be calculated easily using the Atmega32A Timer/Counter online calculator.

 

The OCR0 value to be loaded in non-inverted mode is determined with the following equation.

 

\(OCR0= \frac{255D}{100}\)             -------------------------->(2)

 

where, D is the duty cycle 0% to 100%.

 

Using again the Atmega32A Timer/Counter online calculator for 35% duty cycle the value of count to be loaded into OCR0 register is 89.


C Program for Timer/Counter0 in non-inverted Phase Correct PWM 

Below is the C program to configure and generate non-inverted Phase Correct PWM using ATmega32A with frequency of 7.84KHz, duty cycle of 35% with Fosc of 4MHz and no-prescalar.

#ifndef F_CPU
#define F_CPU 4000000UL
#endif

#include <avr/io.h>

int main()
 { 
    DDRB |= (1<<PB3);	//set PORT B pin 3 as output
	
	OCR0 = 101;
	TCCR0 |= (1<<WGM00) | (1<<COM01) | (1<<CS00);	//Fast PWM, Non-Inverted, no-Prescalar
	while(1);

   return 0;
 }


Programming in Inverted Phase Correct PWM mode

The frequency of inverted phase correct PWM wave for Timer0 is as same as for non-inverted phase correct PWM which was shown above.

 

\(F_{w}=\frac{Fosc}{510N}\)        ---------------------->(1)


where, Fosc is the crystal oscillator frequency and N is the pre-scalar

Using 4MHz oscillator frequency and with prescalar of 8, N=8, the frequency of the PWM wave generated becoes 980.39Hz. You can use the Atmega32A Timer/Counter online calculator to calculate the frequency.

Now, OCR0 value to be loaded for inverted mode is calculated with the following equation.

 

\(OCR0= 255 - \frac{255D}{100}\)             -------------------------->(2)

 

where, D is the duty cycle 0% to 100%.

 Using Atmega32A Timer/Counter online calculator for duty cycle of 60% the value of count to be loaded into OCR0 register is 153.

 

C Program for Timer/Counter0 in non-inverted Phase Correct PWM 

Below is the C program to configure and generate inverted Phase Correct PWM using ATmega32A with frequency of 980.39Hz, duty cycle of 60%, Fosc =4MHz and pre-scalar of 8.

#ifndef F_CPU
#define F_CPU 4000000UL
#endif

#include <avr/io.h>

int main()
 { 
    DDRB |= (1<<PB3);	//set PORT B pin 3 as output
	
	OCR0 = 101;
	TCCR0 |= (1<<WGM00) | (1<<COM01) | (1<<COM00) | (1<<CS01);	//Fast PWM, Inverted, 1/8 Prescalar
	while(1);

   return 0;
 }

 To write and upload the program code see How to Program ATmega328p using ATMEL Studio & AVR ISP MKII. The phase correct PWM is used in application to control motors. See How to control Stepper Motor using ATmega32 and L293D IC.


Post a Comment

Previous Post Next Post