Friday, February 12, 2021

ATMega32 Timer/Counter1 in Fast PWM Mode

Introduction

Timer/Counter1 module of ATMega32 is configurable to operate in fast Pulse Width Modulation (PWM) mode. There are two PWM blocks inside this module - PWM A and PWM B. Hence there are two corresponding output pins for these two blocks - OC1A and OC1B.

PWM output pulse has two mode, non-inverting and inverting mode. Its set and clear output pulse is the comparison between TCNT1 and OC1X register (OC1A or OC1B).

ATMega32 Timer/Counter1 in Fast PWM Mode
Fast PWM Mode - Timing Diagram

To make ATMega32 operates in this mode, user must set WGM13:0 of  Timer/Counter1 Control Register (TCCR1x) to 5, 6, 7, 14, or 15. Its frequency is calculated as below.

ATMega32 Timer/Counter1 in Fast PWM Mode
Fast PWM Mode Frequency

Its resolution is between 8 and 10-bit. Where,

ATMega32 Timer/Counter1 in Fast PWM Mode
Fast PWM Resolution

Programming

We need to configure some registers for fast PWM mode.
  • Timer/Counter1 Control Register A (TCCR1A)
ATMega32 Timer/Counter1 in Fast PWM Mode
TCCR1A

This register contains some settings for fast PWM mode. Compare Output Mode bits used for selecting between inverting and non-inverting mode of fast PWM mode.

ATMega32 Timer/Counter1 in Fast PWM Mode
Compare Output Mode Bits of TCCR1A

Waveform Generation Mode Bits must be set to any value as listed to make this module operates in fast PWM mode.

ATMega32 Timer/Counter1 in Fast PWM Mode
Waveform Generation Mode Bits

  • Timer/Counter1 Control Register B (TCCR1B)
This register contains prescaler selection for fast PWM mode. 
ATMega32 Timer/Counter1 in Fast PWM Mode
TCCR1B

Clock Select Bits (CS12:10) allow user to set its prescaler effecting output frequency of fast PWM.

Timer/Counter1 Control Register B (TCCR1B)
Clock Selection Bits
  • Output Compare Register A 
This is a pair of 8-bit registers - OCR1AH and OCR1AL. This register is a compare variable of TCNT1 that toggle output waveform of PWM signal.
ATMega32 Timer/Counter1 in Fast PWM Mode
Output Compare Register A
Now we make a simple example of using fast PWM. PWM A generates its output waveform on OC1A pin with a frequency of 490Hz, and 50% duty cycle. Output signal is non-inverting mode. Its resolution is 8-bit.

ATMega32 Timer/Counter1 in Fast PWM Mode
Schematic Diagram

Microcontroller clock is 16MHz from external crystal oscillator.

Source Code:

/*
 * timer_1_fast_pwm_8_bit_1.c
 *
 * Created: 2/10/2021 3:41:04 PM
 * Author : admin
 */ 

#include <avr/io.h>

int main(void)
{
/*OC1A PD5 Output*/
DDRD|=(1<<5);
/*Non-inverting mode, fast PWM 8-bit*/
TCCR1A=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM12)|(1<<WGM10);
/*1:64 Prescaler*/
TCCR1B=(1<<CS11)|(1<<CS10);
/*Select 50% duty cycle*/
OCR1AH=0;
OCR1AL=127;
    while (1) 
    {
    }
}

Click here to download archive. 

Now let use both of this PWM output of Timer/Counter1 module. Either OC1A and OC1B output signal are inverting with different duty cycle, but its frequency is unique. 

ATMega32 Timer/Counter1 in Fast PWM Mode
Simulation screen shot

Source code:

/*
 * timer_1_fast_pwm_8_bit_2.c
 *
 * Created: 3/3/2021 8:38:00 PM
 * Author : admin
 */ 

#include <avr/io.h>


int main(void)
{
    /*OC1A and OC1B Output*/
DDRD=(1<<5)|(1<<4);
/*inverting fast PWM 8-bit*/
TCCR1A=(1<<COM1A1)|(1<<COM1B1)|(1<<COM1A0)|(1<<WGM12)|(1<<WGM10);
/*1::64 Pre-Scaler*/
TCCR1B=(1<<CS11)|(1<<CS10);
/*OC1A duty cycle*/
OCR1AH=0;
OCR1AL=127;
/*OC1B duty cycle*/
OCR1BH=0;
OCR1BL=75;
    while (1) 
    {
    }
}

Click here to download this example.



No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)