How can I change the amplitude of a pwm signal ?
hi there
im able change duty cycle , frequency of a 3 pwm signals. still need change amplitude of each pwm signal can tell me how make it ?
im able change duty cycle , frequency of a 3 pwm signals. still need change amplitude of each pwm signal can tell me how make it ?
code: [select]
#include "asf.h"
#include "conf_board.h"
#include "conf_clock.h"
/** pwm frequency in hz */
#define pwm_frequency 800
/** period value of pwm output waveform */
#define period_value 100
/** initial duty cycle value */
#define init_duty_value 50
/* pin definition third pwm channel @ arduino due board = pwm pin 07 */
#define pin_pwm_3dpwm_gpio pio_pc23_idx
#define pin_pwm_3dpwm_flags (pio_periph_b | pio_default)
#define pin_pwm_3dpwm_channel pwm_channel_6
/** pwm channel instance */
pwm_channel_t g_pwm_channel_led;
/**
* \brief interrupt handler 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 = init_duty_value;
if (ul_duty == period_value) {
fade_in = 0;
}
} else {
/* fade out */
ul_duty = init_duty_value;
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);
g_pwm_channel_led.channel = pin_pwm_3dpwm_channel;
pwm_channel_update_duty(pwm, &g_pwm_channel_led, ul_duty);
}
}
}
int main(void)
{
/* initialize sam system */
sysclk_init();
board_init();
gpio_configure_pin(pin_pwm_3dpwm_gpio, pin_pwm_3dpwm_flags); // pwm pin 3
/* enable pwm peripheral clock */
pmc_enable_periph_clk(id_pwm);
/* disable pwm channels leds */
pwm_channel_disable(pwm, pin_pwm_led0_channel);
pwm_channel_disable(pwm, pin_pwm_led1_channel);
pwm_channel_disable(pwm, pin_pwm_3dpwm_channel);// 3rd pwm output
/* set pwm clock pwm_frequency*period_value (clock b 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 led0 */
/* period left-aligned */
g_pwm_channel_led.alignment = pwm_align_left;
/* output waveform starts @ low level */
g_pwm_channel_led.polarity = pwm_low;
/* use pwm clock 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 led1 */
/* period center-aligned */
g_pwm_channel_led.alignment = pwm_align_left;
/* output waveform starts @ high level */
g_pwm_channel_led.polarity = pwm_low;
/* use pwm clock 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);
/* initialize pwm channel led2 */
/* period center-aligned */
g_pwm_channel_led.alignment = pwm_align_left;
/* output waveform starts @ high level */
g_pwm_channel_led.polarity = pwm_low;
/* use pwm clock 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_3dpwm_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 , enable pwm interrupt */
nvic_disableirq(pwm_irqn);
nvic_clearpendingirq(pwm_irqn);
nvic_setpriority(pwm_irqn, 0);
nvic_enableirq(pwm_irqn);
/* enable pwm channels leds */
pwm_channel_enable(pwm, pin_pwm_led0_channel);
pwm_channel_enable(pwm, pin_pwm_led1_channel);
pwm_channel_enable(pwm, pin_pwm_3dpwm_channel);
/* infinite loop */
while (1) {
}
}
need external components if want besides 0-5v output.
see page 16 of datasheet
http://www.digikey.com/product-detail/en/ad5450yujz-reel7/ad5450yujz-reel7ct-nd/2063129
vref multiplied value shifted dac.
higher resolutions available, here 12-bit examples
http://www.digikey.com/product-search/en?fv=2640004%2c3cc0053&k=multiplying+dac&mnonly=0&newproducts=0&columnsort=1000011&page=1&stock=0&pbfree=0&rohs=0&quantity=&ptm=0&fid=0&pagesize=25
see page 16 of datasheet
http://www.digikey.com/product-detail/en/ad5450yujz-reel7/ad5450yujz-reel7ct-nd/2063129
vref multiplied value shifted dac.
higher resolutions available, here 12-bit examples
http://www.digikey.com/product-search/en?fv=2640004%2c3cc0053&k=multiplying+dac&mnonly=0&newproducts=0&columnsort=1000011&page=1&stock=0&pbfree=0&rohs=0&quantity=&ptm=0&fid=0&pagesize=25
Arduino Forum > Products > Arduino Due (Moderator: fabioc84) > How can I change the amplitude of a pwm signal ?
arduino
Comments
Post a Comment