# pwm_example.c File Reference

## Detailed Description

PWM example driver for AVR32.

This file provides an example for the PWM on AVR32 devices.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All AVR32 devices with a PWM module can be used.
- AppNote:

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file pwm_example.c.

`#include <avr32/io.h>`
`#include "pwm.h"`
`#include "gpio.h"`
`#include "board.h"`

Go to the source code of this file.

## Defines
#define | EXAMPLE_PWM_CHANNEL_ID 0
#define | EXAMPLE_PWM_FUNCTION AVR32_PWM_0_FUNCTION
#define | EXAMPLE_PWM_PIN AVR32_PWM_0_PIN
## Functions
int | main ()
| Main function. Execution starts here.

## Define Documentation

#define EXAMPLE_PWM_CHANNEL_ID 0

Definition at line 87 of file pwm_example.c.

Referenced by main().

#define EXAMPLE_PWM_FUNCTION AVR32_PWM_0_FUNCTION

Definition at line 86 of file pwm_example.c.

Referenced by main().

#define EXAMPLE_PWM_PIN AVR32_PWM_0_PIN

Definition at line 85 of file pwm_example.c.

Referenced by main().

## Function Documentation

int main | ( |  | ) |

Main function. Execution starts here.

Returns:
  0 on success

Definition at line 99 of file pwm_example.c.

References pwm_opt_t::diva, pwm_opt_t::divb, EXAMPLE_PWM_CHANNEL_ID, EXAMPLE_PWM_FUNCTION, EXAMPLE_PWM_PIN, pwm_opt_t::prea, pwm_opt_t::preb, pwm_channel_init(), pwm_init(), PWM_MODE_LEFT_ALIGNED, PWM_POLARITY_LOW, pwm_start_channels(), pwm_stop_channels(), and PWM_UPDATE_DUTY.

    00100 {
    00101   pwm_opt_t pwm_opt;                // PWM option config.
    00102   avr32_pwm_channel_t pwm_channel;  // One channel config.
    00103   // The channel number and instance is used in several functions.
    00104   // It's defined as local variable for ease-of-use.
    00105   unsigned int channel_id;
    00106
    00107   channel_id = EXAMPLE_PWM_CHANNEL_ID;
    00108   gpio_enable_module_pin(EXAMPLE_PWM_PIN, EXAMPLE_PWM_FUNCTION);
    00109
    00110   // PWM controller configuration.
    00111   pwm_opt.diva = AVR32_PWM_DIVA_CLK_OFF;
    00112   pwm_opt.divb = AVR32_PWM_DIVB_CLK_OFF;
    00113   pwm_opt.prea = AVR32_PWM_PREA_MCK;
    00114   pwm_opt.preb = AVR32_PWM_PREB_MCK;
    00115
    00116   pwm_init(&pwm_opt);
    00117
    00118   pwm_channel.CMR.calg = PWM_MODE_LEFT_ALIGNED;       // Channel mode.
    00119   pwm_channel.CMR.cpol = PWM_POLARITY_LOW;            // Channel polarity.
    00120   pwm_channel.CMR.cpd = PWM_UPDATE_DUTY;              // Not used the first time.
    00121   pwm_channel.CMR.cpre = AVR32_PWM_CPRE_MCK_DIV_1024;  // Channel prescaler.
    00122   pwm_channel.cdty = 250;   // Channel duty cycle, should be < CPRD.
    00123   pwm_channel.cprd = 1000;  // Channel period.
    00124   pwm_channel.cupd = 0;   // Channel update is not used here.
    00125   // With these settings, the output waveform period will be :
    00126   // (20000000/1024)/1000 == 19.5Hz == (MCK/prescaler)/period, with MCK == 20 MHz,
    00127   // prescaler == 1024, period == 1000.
    00128
    00129   pwm_channel_init(channel_id, &pwm_channel); // Set channel configuration to channel 0.
    00130
    00131   pwm_start_channels(1 << channel_id);  // Start channel 0.
    00132
    00133   while(1);
    00134
    00135   // Stop channel output.
    00136   pwm_stop_channels(1 << channel_id); // Stop channel 0.
    00137
    00138   return 0;
    00139 }