Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMEGA Pulse Width Modulation (PWM) service

See Quickstart guide for AVR XMEGA PWM service.

This is a service for single slope wave form generation on the XMEGA. It provides functions for enabling, disabling and configuring the TC modules in single slope PWM mode.

The API uses a structure which contain the configuration. This structure must be set up before the PWM can be started.

Dependencies

This driver depends on the following modules:

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the XMEGA Pulse Width Modulation (PWM) service.
 

Data Structures

struct  pwm_config
 PWM configuration. More...
 

Typedefs

typedef void(* pwm_callback_t )(void)
 Interrupt callback type. More...
 

Enumerations

enum  pwm_channel_t {
  PWM_CH_A = 1,
  PWM_CH_B = 2,
  PWM_CH_C = 3,
  PWM_CH_D = 4
}
 PWM compare channel index. More...
 
enum  pwm_clk_sel {
  PWM_CLK_OFF = TC_CLKSEL_OFF_gc,
  PWM_CLK_DIV1 = TC_CLKSEL_DIV1_gc,
  PWM_CLK_DIV2 = TC_CLKSEL_DIV2_gc,
  PWM_CLK_DIV4 = TC_CLKSEL_DIV4_gc,
  PWM_CLK_DIV8 = TC_CLKSEL_DIV8_gc,
  PWM_CLK_DIV64 = TC_CLKSEL_DIV64_gc,
  PWM_CLK_DIV256 = TC_CLKSEL_DIV256_gc,
  PWM_CLK_DIV1024 = TC_CLKSEL_DIV1024_gc
}
 Valid clock source indexes. More...
 
enum  pwm_tc_t {
  PWM_TCC0,
  PWM_TCC1,
  PWM_TCD0,
  PWM_TCD1,
  PWM_TCE0,
  PWM_TCE1,
  PWM_TCF0,
  PWM_TCF1
}
 Valid timer/counters to use. More...
 

Functions

static void pwm_disable (struct pwm_config *config)
 Disable the PWM timer. More...
 
void pwm_init (struct pwm_config *config, enum pwm_tc_t tc, enum pwm_channel_t channel, uint16_t freq_hz)
 Initialize PWM configuration struct and set correct I/O pin to output. More...
 
static void pwm_overflow_int_callback (struct pwm_config *config, pwm_callback_t callback)
 Callback function for timer overflow interrupts. More...
 
static void pwm_set_duty_cycle_percent (struct pwm_config *config, uint8_t duty_cycle_scale)
 Function to set PWM duty cycle. More...
 
void pwm_set_frequency (struct pwm_config *config, uint16_t freq_hz)
 Calculate TC settings from PWM frequency. More...
 
void pwm_start (struct pwm_config *config, uint8_t duty_cycle_scale)
 Start a PWM channel. More...
 
static void pwm_stop (struct pwm_config *config)
 Function that stops the PWM timer. More...
 
static void pwm_timer_reset (struct pwm_config *config)
 Function that resets the PWM timer. More...
 

typedef void(* pwm_callback_t)(void)

Interrupt callback type.

PWM compare channel index.

Enumerator
PWM_CH_A 

Channel A.

PWM output on pin 0

PWM_CH_B 

Channel B.

PWM output on pin 1

PWM_CH_C 

Channel C.

PWM output on pin 2

PWM_CH_D 

Channel D.

PWM output on pin 3

Valid clock source indexes.

Enumerator
PWM_CLK_OFF 
PWM_CLK_DIV1 
PWM_CLK_DIV2 
PWM_CLK_DIV4 
PWM_CLK_DIV8 
PWM_CLK_DIV64 
PWM_CLK_DIV256 
PWM_CLK_DIV1024 
enum pwm_tc_t

Valid timer/counters to use.

Note
Not all timer/counters are available on all devices. Please refer to the datasheet for more information on what timer/counters are available for the device you are using.
Enumerator
PWM_TCC0 

PWM on port C, pin 0, 1, 2 or 3 (depending on channel)

PWM_TCC1 

PWM on port C, pin 4 or 5 (depending on channel)

PWM_TCD0 

PWM on port D, pin 0, 1, 2 or 3 (depending on channel)

PWM_TCD1 

PWM on port D, pin 4 or 5 (depending on channel)

PWM_TCE0 

PWM on port E, pin 0, 1, 2 or 3 (depending on channel)

PWM_TCE1 

PWM on port E, pin 4 or 5 (depending on channel)

PWM_TCF0 

PWM on port F, pin 0, 1, 2 or 3 (depending on channel)

PWM_TCF1 

PWM on port F, pin 4 or 5 (depending on channel)

static void pwm_disable ( struct pwm_config config)
inlinestatic

Disable the PWM timer.

This function disables the peripheral clock for the timer and shut down module when unused in order to save power.

Parameters
*configPointer to the PWM configuration struct

References pwm_stop(), pwm_config::tc, and tc_disable().

void pwm_init ( struct pwm_config config,
enum pwm_tc_t  tc,
enum pwm_channel_t  channel,
uint16_t  freq_hz 
)

Initialize PWM configuration struct and set correct I/O pin to output.

Parameters
configPointer to PWM configuration struct.
tcTC to use for this PWM.
channelCC channel to use for this PWM.
freq_hzFrequency to use for this PWM.

References Assert, pwm_config::cc_mask, pwm_config::channel, pwm_config::clk_sel, pwm_config::period, PWM_CH_A, PWM_CH_B, PWM_CH_C, PWM_CH_D, PWM_CLK_OFF, pwm_set_frequency(), PWM_TCC0, PWM_TCC1, PWM_TCD0, PWM_TCD1, PWM_TCE0, PWM_TCE1, PWM_TCF0, PWM_TCF1, pwm_config::tc, TC_CCAEN, TC_CCBEN, TC_CCCEN, TC_CCDEN, tc_enable(), tc_set_wgm(), TC_WG_SS, tc_write_clock_source(), and UNUSED.

Referenced by main().

static void pwm_overflow_int_callback ( struct pwm_config config,
pwm_callback_t  callback 
)
inlinestatic

Callback function for timer overflow interrupts.

This function enables T/C overflow interrupts (low level interrupts) and defines the callback function for the overflow ISR interrupt routine.

Parameters
*configPointer to the PWM configuration struct
callbackCallback function

References pwm_config::tc, TC_INT_LVL_LO, tc_set_overflow_interrupt_callback(), and tc_set_overflow_interrupt_level().

Referenced by main().

static void pwm_set_duty_cycle_percent ( struct pwm_config config,
uint8_t  duty_cycle_scale 
)
inlinestatic

Function to set PWM duty cycle.

The duty cycle can be set on a scale between 0-100%. This value will be used to update the CCx register for the selected PWM channel.

Parameters
*configPointer to the PWM configuration struct
duty_cycle_scaleDuty cycle as a value between 0 and 100.

References Assert, pwm_config::channel, pwm_config::period, pwm_config::tc, and tc_write_cc_buffer().

Referenced by pwm_callback_1(), pwm_callback_2(), and pwm_start().

void pwm_set_frequency ( struct pwm_config config,
uint16_t  freq_hz 
)

Calculate TC settings from PWM frequency.

This function will find the correct TC settings (clock prescaler and period) which will give the wanted PWM frequency.

Note
Since we want to be able to run the PWM at all duty-cycles ranging from 0-100%, we require a period of at least 100 to achieve this. Thus, the highest possible PWM frequency is CPU frequency / 100.
Parameters
configPointer to PWM configuration.
freq_hzWanted PWM frequency in Hz.

References Assert, pwm_config::clk_sel, pwm_config::period, PWM_CLK_DIV1, PWM_CLK_DIV1024, PWM_CLK_DIV2, PWM_CLK_DIV256, PWM_CLK_DIV4, PWM_CLK_DIV64, PWM_CLK_DIV8, PWM_CLK_OFF, and sysclk_get_cpu_hz().

Referenced by pwm_init().

void pwm_start ( struct pwm_config config,
uint8_t  duty_cycle_scale 
)

Start a PWM channel.

This function enables a channel with a given duty cycle.

Parameters
*configPointer to the PWM configuration struct
duty_cycle_scaleDuty cycle as a value between 0 and 100.

References pwm_config::cc_mask, pwm_config::clk_sel, pwm_config::period, pwm_set_duty_cycle_percent(), pwm_config::tc, tc_enable_cc_channels(), tc_write_clock_source(), and tc_write_period().

Referenced by main().

static void pwm_stop ( struct pwm_config config)
inlinestatic

Function that stops the PWM timer.

The PWM timer is stopped by writing the prescaler register to "clock off"

Parameters
*configPointer to the PWM configuration struct

References pwm_config::tc, and tc_write_clock_source().

Referenced by pwm_disable().

static void pwm_timer_reset ( struct pwm_config config)
inlinestatic

Function that resets the PWM timer.

This function reset the CNT register for the selected timer used for PWM

Parameters
*configPointer to the PWM configuration struct

References pwm_config::tc, and tc_write_count().