|
本帖最后由 Mcuzone_TKN 于 2020-5-15 09:13 编辑
关键词:Microchip Atmel SAM4E-EK SAM4E16E 芯片 脉宽调制 PWM LED
概述:让脉宽调制控制器输出具有一定周期和占空比的方波,以此来控制LED灯的亮灭
开发板上能启用4个PWM通道,每个通道输出两个互补的方波。启用其中的一个通道,然后让这个通道的两个输出分别控制一个LED灯,但是这两个引脚不在同一通道输出,所以需要第三个引脚辅助。
使用PWM的通道0。PWMH0输出至引脚PA0;PWML0输出至引脚PC0;引脚PD20配置为纯输入引脚;将PC0和PD20两个引脚短接起来,这样就可以使这两个LED灯交替闪烁了。
打开产品光盘SAM4E16E-EK/SAM4E16E-EK中文资料/softpack软件包/Atmel Studio 7,打开02_PWM_LED_EXAMPLE例子。
int main(void)
{
sysclk_init();
board_init();
/ /为调试信息配置控制台UART
configure_console();
//输出例子信息
puts(STRING_HEADER);
//启用PWM外围时钟
pmc_enable_periph_clk(ID_PWM);
//禁用led的PWM通道
pwm_channel_disable(PWM, PIN_PWM_LED0_CHANNEL);
pwm_channel_disable(PWM, PIN_PWM_LED1_CHANNEL);
//将PWM时钟A设置为PWM_FREQUENCY_value(没有使用时钟B)
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);
//初始化LED0的PWM通道
//周期是左对齐的
g_pwm_channel_led.alignment = PWM_ALIGN_LEFT;
//输出波形以低电平开始
g_pwm_channel_led.polarity = PWM_LOW;
//使用PWM时钟A作为时钟源
g_pwm_channel_led.ul_prescaler = PWM_CMR_CPRE_CLKA;
//输出波形的周期值
g_pwm_channel_led.ul_period = PERIOD_VALUE;
//输出波形占空比
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);
//启用通道计数器事件中断
pwm_channel_enable_interrupt(PWM, PIN_PWM_LED0_CHANNEL, 0);
//初始化LED1的PWM通道
//周期是中心对齐的
g_pwm_channel_led.alignment = PWM_ALIGN_CENTER;
//输出波形以高电平开始
g_pwm_channel_led.polarity = PWM_HIGH;
//周期是左对齐的
g_pwm_channel_led.ul_prescaler = PWM_CMR_CPRE_CLKA;
//输出波形的周期值
g_pwm_channel_led.ul_period = PERIOD_VALUE;
//输出波形占空比值
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);
//禁用通道计数器事件中断
pwm_channel_disable_interrupt(PWM, PIN_PWM_LED1_CHANNEL, 0);
//配置中断并启用PWM中断
NVIC_DisableIRQ(PWM_IRQn);
NVIC_ClearPendingIRQ(PWM_IRQn);
NVIC_SetPriority(PWM_IRQn, 0);
NVIC_EnableIRQ(PWM_IRQn);
//为LED启用PWM通道
pwm_channel_enable(PWM, PIN_PWM_LED0_CHANNEL);
pwm_channel_enable(PWM, PIN_PWM_LED1_CHANNEL);
ioport_set_pin_level(LED2_GPIO, IOPORT_PIN_LEVEL_LOW);
ioport_set_pin_level(LED3_GPIO, IOPORT_PIN_LEVEL_LOW);
while (1) {
}
}
|
|