测试2:利用定时器产生4路PWM 新建bsp_pwm.c和bsp_pwm.h 具体代码如下: #include "bsp_pwm.h"
timer_handle_t pwm_tim;
timer_clock_config_t pwm_tim_clock;
timer_oc_init_t tim_ocinit;
void pwm_init()
{
gpio_init_t GPIO_InitStructure;
/* Initialize tim0 ch1 pin */
GPIO_InitStructure.mode = GPIO_MODE_OUTPUT;
GPIO_InitStructure.odos = GPIO_PUSH_PULL;
GPIO_InitStructure.pupd = GPIO_PUSH_UP;
GPIO_InitStructure.odrv = GPIO_OUT_DRIVE_NORMAL;
GPIO_InitStructure.flt = GPIO_FILTER_DISABLE;
GPIO_InitStructure.type = GPIO_TYPE_TTL;
GPIO_InitStructure.func = GPIO_FUNC_2;
ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL1_PIN, &GPIO_InitStructure);
ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL2_PIN, &GPIO_InitStructure);
ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL3_PIN, &GPIO_InitStructure);
ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL4_PIN, &GPIO_InitStructure);
pwm_tim.perh = AD16C4T0;
pwm_tim.init.prescaler = pwm_psc;
pwm_tim.init.period = pwm_arr;
pwm_tim.init.mode = TIMER_CNT_MODE_UP;
pwm_tim.init.clk_div = TIMER_CLOCK_DIV1;
pwm_tim.init.re_cnt = 0;
ald_timer_pwm_init(&pwm_tim);
/* Initialize clock source */
pwm_tim_clock.source = TIMER_SRC_INTER;
ald_timer_config_clock_source(&pwm_tim, &pwm_tim_clock);
/* Common configuration for all channels */
tim_ocinit.oc_mode = TIMER_OC_MODE_PWM1;
tim_ocinit.oc_polarity = TIMER_OC_POLARITY_HIGH;
tim_ocinit.oc_fast_en = DISABLE;
tim_ocinit.ocn_polarity = TIMER_OCN_POLARITY_HIGH;
tim_ocinit.ocn_idle = TIMER_OCN_IDLE_RESET;
tim_ocinit.oc_idle = TIMER_OC_IDLE_RESET;
/* Set the pulse value for channel 1 */
tim_ocinit.pulse = PULSE1_VALUE;
ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_1);
/* Set the pulse value for channel 2 */
tim_ocinit.pulse = PULSE2_VALUE;
ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_2);
/* Set the pulse value for channel 3 */
tim_ocinit.pulse = PULSE3_VALUE;
ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_3);
/* Set the pulse value for channel 4 */
tim_ocinit.pulse = PULSE4_VALUE;
ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_4);
/* Start input pwm from tim0 channel 1 */
ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_1);
/* Start input pwm from tim0 channel 2 */
ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_2);
/* Start input pwm from tim0 channel 3 */
ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_3);
/* Start input pwm from tim0 channel 4 */
ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_4);
}
#ifndef __BSP_PWM_H__
#define __BSP_PWM_H__
#include "ald_timer.h"
#include "ald_gpio.h"
#define pwm_psc 0
#define pwm_arr (uint32_t)(700 - 1) /* Period Value */
#define PULSE1_VALUE (uint32_t)(pwm_arr/2) /* Capture Compare 1 Value */
#define PULSE2_VALUE (uint32_t)(pwm_arr*37.5/100) /* Capture Compare 2 Value */
#define PULSE3_VALUE (uint32_t)(pwm_arr/4) /* Capture Compare 3 Value */
#define PULSE4_VALUE (uint32_t)(pwm_arr*12.5/100) /* Capture Compare 4 Value */
#define TIMER0_CHANNEL_PORT GPIOA
#define TIMER0_CHANNEL1_PIN GPIO_PIN_8
#define TIMER0_CHANNEL2_PIN GPIO_PIN_9
#define TIMER0_CHANNEL3_PIN GPIO_PIN_10
#define TIMER0_CHANNEL4_PIN GPIO_PIN_11
void pwm_init(void);
#endif
mian.c 用来测试定时器0产生4路pwm,引脚分别对应PA8、 PA9、PA10、 PA11具体代码如下: #include "main.h"
int main()
{
//初始化ALD
ald_cmu_init();
//初始化系统时钟
ald_cmu_pll1_config(CMU_PLL1_INPUT_HRC_6, CMU_PLL1_OUTPUT_48M);
ald_cmu_clock_config(CMU_CLOCK_PLL1,48000000);
led_module_init(); //LED初始化
bsp_key_init(); //按键初始化
adc_module_init(); //ADC模块初始化
//tim0_pin_init(); //定时器初始化
pwm_init(); //pwm初始化
while (1)
{
}
}
|