- Main Page
- Related Pages
- Data Structures
- Files

# pwm_example.c

Go to the documentation of this file.

    00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00078 #include <avr32/io.h>
    00079 #include "pwm.h"
    00080 #include "gpio.h"
    00081 #include "board.h"
    00082
    00083
    00084 #if BOARD == STK1000
    00085 #  define EXAMPLE_PWM_PIN             AVR32_PWM_0_PIN
    00086 #  define EXAMPLE_PWM_FUNCTION        AVR32_PWM_0_FUNCTION
    00087 #  define EXAMPLE_PWM_CHANNEL_ID      0
    00088 #elif BOARD == NGW100
    00089 #  define EXAMPLE_PWM_PIN             AVR32_PWM_0_PIN
    00090 #  define EXAMPLE_PWM_FUNCTION        AVR32_PWM_0_FUNCTION
    00091 #  define EXAMPLE_PWM_CHANNEL_ID      0
    00092 #endif
    00093
    00094
    00099 int main()
    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 }