ATMEL AVR PWM Overview

Introduction:

The methods described in this section allow the generation of Pulse Width Modulation (PWM) signals. PWM signals enables the microcontroller to control items like the speed of a motor, or the brightness of a LED or lamp.

The methods can also be used to generate the appropriate frequency signal to drive an infrared LED for remote control applications.

GCBASIC support the methods described in this section.

Hardware PWM using a Timer/Counter with a OCRnx module

The AVR devices use a Timer/Counter and OCRnx module that has a variable period register. The Hardware PWM is available through the OCnx pin.

The method uses three parameters to setup the HPWM.

    'HPWM channel, frequency, duty cycle
    HPWM  2, 100, 50          ' <<< the HPWM instruction

Key line: HPWM 2, 100, 50 — starts hardware PWM on channel 2 at a 100 Hz frequency with a 50% duty cycle, using the AVR’s Timer/Counter and OCRnx module rather than a software-timed loop.

Relevant Constants:

A number of constants are used to control settings for PWM hardware module of the microcontroller. To set them, place a line in the main program file that uses #define to assign a value to the particular constant.

 
 
 

See Also:

  • HPWM AVR OCRnx — full reference for the HPWM command and its relevant constants