Hi there..
I have a simple question related to an Atmel Software studio (ASF)
This question fades two LEDs throught varying the duty cycle of a PWM signal
I want to get a constant duty cycle which can be modified by user no fading or so
I think this can be modified in the " PWM_HANDLER" function but im not sure
Can anyone please help me to modifiy this few lines of code ?
Thanks alot in advance
* This example can be used on any SAM3/4 boards. The 2 required leds need to
* be connected to PWM output pins, else consider probing the PWM output pins
* with an oscilloscope.
*
* \par Usage
* /*
* -# Initialize system clock and pins setting on board
* -# Initialize PWM clock
* -# Configure PIN_PWM_LED0_CHANNEL
* -# Configure PIN_PWM_LED1_CHANNEL
* -# Enable interrupt of counter event and PIN_PWM_LED0_CHANNEL
* & PIN_PWM_LED1_CHANNEL
* -# Change duty cycle in ISR
*
*/
#include "asf.h"
#include "conf_board.h"
#include "conf_clock.h"
/** PWM frequency in Hz */
#define PWM_FREQUENCY 1000
/** Period value of PWM output waveform */
#define PERIOD_VALUE 100
/** Initial duty cycle value */
#define INIT_DUTY_VALUE 0
/** PWM channel instance for LEDs */
pwm_channel_t g_pwm_channel_led;
/**
* \brief Interrupt handler for the PWM controller.
*/
void PWM_Handler(void)
{
static uint32_t ul_count = 0; /* PWM counter value */
static uint32_t ul_duty = INIT_DUTY_VALUE; /* PWM duty cycle rate */
static uint8_t fade_in = 1; /* LED fade in flag */
uint32_t events = pwm_channel_get_interrupt_status(PWM);
/* Interrupt on PIN_PWM_LED0_CHANNEL */
if ((events & (1 << PIN_PWM_LED0_CHANNEL)) ==
(1 << PIN_PWM_LED0_CHANNEL)) {
ul_count++;
/* Fade in/out */
if (ul_count == (PWM_FREQUENCY / (PERIOD_VALUE - INIT_DUTY_VALUE))) {
/* Fade in */
if (fade_in) {
ul_duty++;
if (ul_duty == PERIOD_VALUE) {
fade_in = 0;
}
} else {
/* Fade out */
ul_duty--;
if (ul_duty == INIT_DUTY_VALUE) {
fade_in = 1;
}
}
/* Set new duty cycle */
ul_count = 0;
g_pwm_channel_led.channel = PIN_PWM_LED0_CHANNEL;
pwm_channel_update_duty(PWM, &g_pwm_channel_led, ul_duty);
g_pwm_channel_led.channel = PIN_PWM_LED1_CHANNEL;
pwm_channel_update_duty(PWM, &g_pwm_channel_led, ul_duty);
}
}
}
int main(void)
{
/* Initialize the SAM system */
sysclk_init();
board_init();
/* Enable PWM peripheral clock */
pmc_enable_periph_clk(ID_PWM);
/* Disable PWM channels for LEDs */
pwm_channel_disable(PWM, PIN_PWM_LED0_CHANNEL);
pwm_channel_disable(PWM, PIN_PWM_LED1_CHANNEL);
/* Set PWM clock A as PWM_FREQUENCY*PERIOD_VALUE (clock B is not used) */
pwm_clock_t clock_setting = {
.ul_clka = PWM_FREQUENCY * PERIOD_VALUE,
.ul_clkb = 0,
.ul_mck = sysclk_get_cpu_hz()
};
pwm_init(PWM, &clock_setting);
/* Initialize PWM channel for LED0 */
/* Period is left-aligned */
g_pwm_channel_led.alignment = PWM_ALIGN_LEFT;
/* Output waveform starts at a low level */
g_pwm_channel_led.polarity = PWM_LOW;
/* Use PWM clock A as source clock */
g_pwm_channel_led.ul_prescaler = PWM_CMR_CPRE_CLKA;
/* Period value of output waveform */
g_pwm_channel_led.ul_period = PERIOD_VALUE;
/* Duty cycle value of output waveform */
g_pwm_channel_led.ul_duty = INIT_DUTY_VALUE;
g_pwm_channel_led.channel = PIN_PWM_LED0_CHANNEL;
pwm_channel_init(PWM, &g_pwm_channel_led);
/* Enable channel counter event interrupt */
pwm_channel_enable_interrupt(PWM, PIN_PWM_LED0_CHANNEL, 0);
/* Initialize PWM channel for LED1 */
/* Period is center-aligned */
g_pwm_channel_led.alignment = PWM_ALIGN_CENTER;
/* Output waveform starts at a high level */
g_pwm_channel_led.polarity = PWM_HIGH;
/* Use PWM clock A as source clock */
g_pwm_channel_led.ul_prescaler = PWM_CMR_CPRE_CLKA;
/* Period value of output waveform */
g_pwm_channel_led.ul_period = PERIOD_VALUE;
/* Duty cycle value of output waveform */
g_pwm_channel_led.ul_duty = INIT_DUTY_VALUE;
g_pwm_channel_led.channel = PIN_PWM_LED1_CHANNEL;
pwm_channel_init(PWM, &g_pwm_channel_led);
/* Disable channel counter event interrupt */
pwm_channel_disable_interrupt(PWM, PIN_PWM_LED1_CHANNEL, 0);
/* Configure interrupt and enable PWM interrupt */
NVIC_DisableIRQ(PWM_IRQn);
NVIC_ClearPendingIRQ(PWM_IRQn);
NVIC_SetPriority(PWM_IRQn, 0);
NVIC_EnableIRQ(PWM_IRQn);
/* Enable PWM channels for LEDs */
pwm_channel_enable(PWM, PIN_PWM_LED0_CHANNEL);
pwm_channel_enable(PWM, PIN_PWM_LED1_CHANNEL);
/* Infinite loop */
while (1) {
}
}