24.8.4.1.2 Code
Add to the main application source file, before any functions:
- SAM D21 Xplained Pro.
#define PWM_MODULE EXT1_PWM_MODULE
#define PWM_OUT_PIN EXT1_PWM_0_PIN
#define PWM_OUT_MUX EXT1_PWM_0_MUX
- SAM D20 Xplained Pro.
#define PWM_MODULE EXT1_PWM_MODULE
#define PWM_OUT_PIN EXT1_PWM_0_PIN
#define PWM_OUT_MUX EXT1_PWM_0_MUX
- SAM R21 Xplained Pro.
#define PWM_MODULE EXT1_PWM_MODULE
#define PWM_OUT_PIN EXT1_PWM_0_PIN
#define PWM_OUT_MUX EXT1_PWM_0_MUX
- SAM D11 Xplained Pro.
#define PWM_MODULE EXT1_PWM_MODULE
#define PWM_OUT_PIN EXT1_PWM_0_PIN
#define PWM_OUT_MUX EXT1_PWM_0_MUX
- SAM L21 Xplained Pro.
#define PWM_MODULE EXT2_PWM_MODULE
#define PWM_OUT_PIN EXT2_PWM_0_PIN
#define PWM_OUT_MUX EXT2_PWM_0_MUX
- SAM L22 Xplained Pro.
#define PWM_MODULE EXT3_PWM_MODULE
#define PWM_OUT_PIN EXT3_PWM_0_PIN
#define PWM_OUT_MUX EXT3_PWM_0_MUX
- SAM DA1 Xplained Pro.
#define PWM_MODULE EXT1_PWM_MODULE
#define PWM_OUT_PIN EXT1_PWM_0_PIN
#define PWM_OUT_MUX EXT1_PWM_0_MUX
- SAM C21 Xplained Pro.
Add to the main application source file, outside of any functions:#define PWM_MODULE EXT1_PWM_MODULE
#define PWM_OUT_PIN EXT1_PWM_0_PIN
#define PWM_OUT_MUX EXT1_PWM_0_MUX
Copy-paste the following callback function code to your user application:struct
tc_module tc_instance;
Copy-paste the following setup code to your user application:void
tc_callback_to_change_duty_cycle(
struct
tc_module *
const
module_inst)
{
static
uint16_t i = 0;
i += 128;
tc_set_compare_value(module_inst, TC_COMPARE_CAPTURE_CHANNEL_0, i + 1);
}
Add to user application initialization (typically the start of main()):void
configure_tc(
void
)
{
struct
tc_config config_tc;
tc_get_config_defaults(&config_tc);
config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM;
config_tc.counter_16_bit.compare_capture_channel[0] = 0xFFFF;
config_tc.pwm_channel[0].enabled =
true
;
config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN;
config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX;
tc_init(&tc_instance, PWM_MODULE, &config_tc);
tc_enable(&tc_instance);
}
void
configure_tc_callbacks(
void
)
{
tc_register_callback(
&tc_instance,
tc_callback_to_change_duty_cycle,
TC_CALLBACK_CC_CHANNEL0);
tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
}
configure_tc();
configure_tc_callbacks();