ES-PDS32 ES32F654评测————第九步:测试高级控制定时器(AD16C4T)

支持 1路高级定时器(AD16C4T0)。AD16C4T可被视为复用在 6个通道上的 3相 PWM。
PWM 互补输出的死区时间可配。另外高级定时器兼具通用定时器的所有功能。在调试模式下,高级定时器可被冻结,并禁止 PWM 输出。高级定时器可与其他定时器连接,一起配合使用,以达到同步或事件串联的目的。

测试1: 定时中断控制LED翻转

新建bsp_time0.c和bsp_time0.h 具体代码如下:

#include "bsp_timer0.h"
unsigned  char  timeflag;
timer_handle_t  h_tim;
timer_clock_config_t tim_clock;
//定时器初始化函数
void tim0_pin_init()
{	
   /* Initialize AD16C4T0 */
	h_tim.perh = AD16C4T0;
	h_tim.init.period       = arr;
	h_tim.init.prescaler    = psc;
	h_tim.init.clk_div      = TIMER_CLOCK_DIV1;
	h_tim.init.mode         = TIMER_CNT_MODE_UP;
	h_tim.init.re_cnt       = 0;   
	ald_timer_base_init(&h_tim);  
  /* Initialize clock source */
	tim_clock.source = TIMER_SRC_INTER;
	ald_timer_config_clock_source(&h_tim, &tim_clock);
  /* Enable tim0 interrupt */
	ald_mcu_irq_config(AD16C4T0_BRK_UP_TRIG_COM_IRQn, 1, ENABLE);	  
	/* Enable UPDATE interrupt */
	ald_timer_interrupt_config(&h_tim, TIMER_IT_UPDATE, ENABLE);
	/* Start input pwm from tim0 channel 1 */
	ald_timer_base_start(&h_tim);
}
//定时器中断处理函数
void AD16C4T0_BRK_UP_TRIG_COM_Handler(void)
{	
	static unsigned int count;
	/* Handle TIMER interrupt */
	ald_timer_irq_handler(&h_tim);
	count ++;
	if(count == 2)
	{
		count = 0;
	  if(timeflag == 0)
			  timeflag =1;
		else  timeflag =0;
	} 
	return;
}

#ifndef _BSP_TIMER0_H
#define _BSP_TIMER0_H
#include "ald_timer.h"
#include "ald_gpio.h"
#define  arr        4999
#define  psc        4799
void tim0_pin_init();
#endif

mian.c 用来测试定时器操作,具体代码如下:

#include "main.h"

extern unsigned  char  timeflag;

int main()
{
	ald_cmu_init();      // Initialize ALD 
//初始化系统时钟
	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();     //定时器初始化
	while (1) 
	{
		 if(timeflag == 1)
		 {
			 led_operate(0, LED_ON);
			 led_operate(1, LED_ON);
		 }
		 else
		 {
		   led_operate(0, LED_OFF);
			 led_operate(1, LED_OFF);
		 }
	}
}

测试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) 
	{ 
	}
}

ES-PDS32 ES32F654评测————第九步:测试高级控制定时器(AD16C4T)_第1张图片

测试结果

ES-PDS32 ES32F654评测————第九步:测试高级控制定时器(AD16C4T)_第2张图片

ES-PDS32 ES32F654评测————第九步:测试高级控制定时器(AD16C4T)_第3张图片

你可能感兴趣的:(评测)