[国产单片机] 【东软载波ESF0654 PDS开发板活动】高级定时器(AD16C4T0)

[复制链接]
1079|2
 楼主| 一路向北lm 发表于 2019-12-18 20:04 | 显示全部楼层 |阅读模式
支持 1路高级定时器(AD16C4T0)。AD16C4T可被视为复用在 6个通道上的 3相 PWM。
PWM 互补输出的死区时间可配。另外高级定时器兼具通用定时器的所有功能。在调试模式下,高级定时器可被冻结,并禁止PWM 输出。高级定时器可与其他定时器连接,一起配合使用,以达到同步或事件串联的目的。
测试1定时中断控制LED翻转
新建bsp_time0.cbsp_time0.h 具体代码如下:
  1. #include "bsp_timer0.h"
  2. unsigned  char  timeflag;
  3. timer_handle_t  h_tim;
  4. timer_clock_config_t tim_clock;
  5. //定时器初始化函数
  6. void tim0_pin_init()
  7. {       
  8.    /* Initialize AD16C4T0 */
  9.         h_tim.perh = AD16C4T0;
  10.         h_tim.init.period       = arr;
  11.         h_tim.init.prescaler    = psc;
  12.         h_tim.init.clk_div      = TIMER_CLOCK_DIV1;
  13.         h_tim.init.mode         = TIMER_CNT_MODE_UP;
  14.         h_tim.init.re_cnt       = 0;   
  15.         ald_timer_base_init(&h_tim);  
  16.   /* Initialize clock source */
  17.         tim_clock.source = TIMER_SRC_INTER;
  18.         ald_timer_config_clock_source(&h_tim, &tim_clock);
  19.   /* Enable tim0 interrupt */
  20.         ald_mcu_irq_config(AD16C4T0_BRK_UP_TRIG_COM_IRQn, 1, ENABLE);          
  21.         /* Enable UPDATE interrupt */
  22.         ald_timer_interrupt_config(&h_tim, TIMER_IT_UPDATE, ENABLE);
  23.         /* Start input pwm from tim0 channel 1 */
  24.         ald_timer_base_start(&h_tim);
  25. }
  26. //定时器中断处理函数
  27. void AD16C4T0_BRK_UP_TRIG_COM_Handler(void)
  28. {       
  29.         static unsigned int count;
  30.         /* Handle TIMER interrupt */
  31.         ald_timer_irq_handler(&h_tim);
  32.         count ++;
  33.         if(count == 2)
  34.         {
  35.                 count = 0;
  36.           if(timeflag == 0)
  37.                           timeflag =1;
  38.                 else  timeflag =0;
  39.         }
  40.         return;
  41. }

  42. #ifndef _BSP_TIMER0_H
  43. #define _BSP_TIMER0_H
  44. #include "ald_timer.h"
  45. #include "ald_gpio.h"
  46. #define  arr        4999
  47. #define  psc        4799
  48. void tim0_pin_init();
  49. #endif
mian.c 用来测试定时器操作,具体代码如下:
  1. #include "main.h"

  2. extern unsigned  char  timeflag;

  3. int main()
  4. {
  5.         ald_cmu_init();      // Initialize ALD
  6. //初始化系统时钟
  7.         ald_cmu_pll1_config(CMU_PLL1_INPUT_HRC_6, CMU_PLL1_OUTPUT_48M);
  8.         ald_cmu_clock_config(CMU_CLOCK_PLL1,48000000);
  9.         led_module_init();   //LED初始化
  10.         bsp_key_init();      //按键初始化
  11.         adc_module_init();   //ADC模块初始化
  12.     tim0_pin_init();     //定时器初始化
  13.         while (1)
  14.         {
  15.                  if(timeflag == 1)
  16.                  {
  17.                          led_operate(0, LED_ON);
  18.                          led_operate(1, LED_ON);
  19.                  }
  20.                  else
  21.                  {
  22.                    led_operate(0, LED_OFF);
  23.                          led_operate(1, LED_OFF);
  24.                  }
  25.         }
  26. }


 楼主| 一路向北lm 发表于 2019-12-18 20:06 | 显示全部楼层
测试2利用定时器产生4PWM
新建bsp_pwm.cbsp_pwm.h 具体代码如下:
  1. #include "bsp_pwm.h"
  2. timer_handle_t pwm_tim;
  3. timer_clock_config_t pwm_tim_clock;
  4. timer_oc_init_t tim_ocinit;

  5. void pwm_init()
  6. {
  7.   gpio_init_t GPIO_InitStructure;
  8.         /* Initialize tim0 ch1 pin */
  9.         GPIO_InitStructure.mode = GPIO_MODE_OUTPUT;
  10.         GPIO_InitStructure.odos = GPIO_PUSH_PULL;
  11.         GPIO_InitStructure.pupd = GPIO_PUSH_UP;
  12.         GPIO_InitStructure.odrv = GPIO_OUT_DRIVE_NORMAL;
  13.         GPIO_InitStructure.flt  = GPIO_FILTER_DISABLE;
  14.         GPIO_InitStructure.type = GPIO_TYPE_TTL;
  15.         GPIO_InitStructure.func = GPIO_FUNC_2;  
  16.         ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL1_PIN, &GPIO_InitStructure);
  17.    ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL2_PIN, &GPIO_InitStructure);
  18.    ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL3_PIN, &GPIO_InitStructure);
  19.    ald_gpio_init(TIMER0_CHANNEL_PORT, TIMER0_CHANNEL4_PIN, &GPIO_InitStructure);

  20.         pwm_tim.perh              = AD16C4T0;
  21.         pwm_tim.init.prescaler    = pwm_psc;
  22.         pwm_tim.init.period       = pwm_arr;
  23.         pwm_tim.init.mode         = TIMER_CNT_MODE_UP;
  24.         pwm_tim.init.clk_div      = TIMER_CLOCK_DIV1;
  25.         pwm_tim.init.re_cnt       = 0;
  26.         ald_timer_pwm_init(&pwm_tim);
  27.        
  28.         /* Initialize clock source */
  29.         pwm_tim_clock.source = TIMER_SRC_INTER;
  30.         ald_timer_config_clock_source(&pwm_tim, &pwm_tim_clock);

  31. /* Common configuration for all channels */
  32. tim_ocinit.oc_mode      = TIMER_OC_MODE_PWM1;
  33.   tim_ocinit.oc_polarity  = TIMER_OC_POLARITY_HIGH;
  34.   tim_ocinit.oc_fast_en   = DISABLE;
  35.   tim_ocinit.ocn_polarity = TIMER_OCN_POLARITY_HIGH;
  36.   tim_ocinit.ocn_idle     = TIMER_OCN_IDLE_RESET;
  37.   tim_ocinit.oc_idle      = TIMER_OC_IDLE_RESET;
  38.    
  39.   /* Set the pulse value for channel 1 */
  40.   tim_ocinit.pulse = PULSE1_VALUE;
  41.         ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_1);   
  42.   /* Set the pulse value for channel 2 */
  43.   tim_ocinit.pulse = PULSE2_VALUE;
  44.         ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_2);   
  45.   /* Set the pulse value for channel 3 */
  46.   tim_ocinit.pulse = PULSE3_VALUE;
  47.         ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_3);  
  48.   /* Set the pulse value for channel 4 */
  49.   tim_ocinit.pulse = PULSE4_VALUE;
  50.         ald_timer_oc_config_channel(&pwm_tim, &tim_ocinit, TIMER_CHANNEL_4);
  51.         /* Start input pwm from tim0 channel 1 */
  52.         ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_1);  
  53.   /* Start input pwm from tim0 channel 2 */
  54.         ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_2);
  55.   /* Start input pwm from tim0 channel 3 */
  56.         ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_3);  
  57.   /* Start input pwm from tim0 channel 4 */
  58.         ald_timer_oc_start(&pwm_tim, TIMER_CHANNEL_4);
  59. }


  60. #ifndef __BSP_PWM_H__
  61. #define __BSP_PWM_H__
  62. #include "ald_timer.h"
  63. #include "ald_gpio.h"
  64. #define  pwm_psc             0  
  65. #define  pwm_arr            (uint32_t)(700 - 1)  /* Period Value  */
  66. #define  PULSE1_VALUE       (uint32_t)(pwm_arr/2)        /* Capture Compare 1 Value  */
  67. #define  PULSE2_VALUE       (uint32_t)(pwm_arr*37.5/100) /* Capture Compare 2 Value  */
  68. #define  PULSE3_VALUE       (uint32_t)(pwm_arr/4)        /* Capture Compare 3 Value  */
  69. #define  PULSE4_VALUE       (uint32_t)(pwm_arr*12.5/100) /* Capture Compare 4 Value  */

  70. #define TIMER0_CHANNEL_PORT GPIOA
  71. #define TIMER0_CHANNEL1_PIN GPIO_PIN_8
  72. #define TIMER0_CHANNEL2_PIN GPIO_PIN_9
  73. #define TIMER0_CHANNEL3_PIN GPIO_PIN_10
  74. #define TIMER0_CHANNEL4_PIN GPIO_PIN_11
  75. void pwm_init(void);
  76. #endif
mian.c 用来测试定时器0产生4pwm,引脚分别对应PA8 PA9PA10 PA11具体代码如下:
  1. #include "main.h"
  2. int main()
  3. {
  4.         //初始化ALD
  5.         ald_cmu_init();     
  6.         //初始化系统时钟
  7.         ald_cmu_pll1_config(CMU_PLL1_INPUT_HRC_6, CMU_PLL1_OUTPUT_48M);
  8.         ald_cmu_clock_config(CMU_CLOCK_PLL1,48000000);
  9.         led_module_init();   //LED初始化
  10.         bsp_key_init();      //按键初始化
  11.         adc_module_init();   //ADC模块初始化
  12.   //tim0_pin_init();   //定时器初始化

  13.         pwm_init();          //pwm初始化
  14.         while (1)
  15.         {
  16.         }
  17. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| 一路向北lm 发表于 2019-12-18 20:07 | 显示全部楼层
测试结果:

连接对应引脚

输出的pwm波形


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部