学习目标

  1. 加强掌握PWM开发流程
  2. 理解定时器与通道的关系
  3. 掌握多通道配置策略
  4. 掌握定时器查询方式
  5. 掌握代码抽取优化策略

    学习内容

    需求

    210.png209.png
    208.png
    点亮8个灯,采用pwm的方式。
定时器 通道 引脚 AF LED序号
T0 ch0 PA8 AF1 LED2
T1 ch0 PA5 AF1 LED7
T2 ch0 PC6 AF2 LED8
ch1 PC7 AF2 LED6
ch2 PC8 AF2 LED4
T7 ch0_on PA7 AF3 LED1
ch1_on PB14 AF3 LED5
ch2_on PB15 AF3 LED3

实现LED4``LED6``LED8``LED1``LED3``LED5呼吸灯效果

通用定时器多通道

点亮T2定时器下的多个通道的灯。

开发流程

  1. 添加Timer依赖
  2. 初始化PWM,包含多通道配置
  3. PWM占空比控制

    多通道配置

    ```c timer_oc_parameter_struct tops;

// ch0 timer_channel_output_struct_para_init(&tops); tops.outputstate = TIMER_CCX_ENABLE; timer_channel_output_config(timerx, TIMER_CH_0, &tops);

// ch1 timer_channel_output_struct_para_init(&tops); tops.outputstate = TIMER_CCX_ENABLE; timer_channel_output_config(timerx, TIMER_CH_1, &tops);

// ch2 timer_channel_output_struct_para_init(&tops); tops.outputstate = TIMER_CCX_ENABLE; timer_channel_output_config(timerx, TIMER_CH_1, &tops);

  1. - TIMER_CH_0 表示通道0
  2. - TIMER_CH_1 表示通道1
  3. - TIMER_CH_2 表示通道2
  4. ```c
  5. // ch0
  6. timer_channel_output_mode_config(timerx, TIMER_CH_0, TIMER_OC_MODE_PWM0);
  7. // ch1
  8. timer_channel_output_mode_config(timerx, TIMER_CH_1, TIMER_OC_MODE_PWM0);
  9. // ch2
  10. timer_channel_output_mode_config(timerx, TIMER_CH_2, TIMER_OC_MODE_PWM0);
  • TIMER_OC_MODE_PWM0: 高电平有效
  • TIMER_OC_MODE_PWM1:低电平有效

    占空比更新

    1. // ch0
    2. timer_channel_output_pulse_value_config(timerx, TIMER_CH_0, 0);
    3. // ch1
    4. timer_channel_output_pulse_value_config(timerx, TIMER_CH_1, 0);
    5. // ch2
    6. timer_channel_output_pulse_value_config(timerx, TIMER_CH_2, 0);

    关心的内容

    1. static void PWM_config() {
    2. uint32_t timerx = TIMER2;
    3. uint32_t timerx_rcu = RCU_TIMER2;
    4. uint32_t timerx_psc = RCU_TIMER_PSC_MUL4;
    5. uint32_t timerx_prescaler = PRESCALER; // 分频计数
    6. uint32_t timerx_period = PERIOD; // 周期计数
    7. uint32_t timerx_ch0_port = GPIOC;
    8. uint32_t timerx_ch0_port_rcu = RCU_GPIOC;
    9. uint32_t timerx_ch0_pin = GPIO_PIN_6;
    10. uint32_t timerx_ch0_af = GPIO_AF_2;
    11. uint32_t timerx_ch1_port = GPIOC;
    12. uint32_t timerx_ch1_port_rcu = RCU_GPIOC;
    13. uint32_t timerx_ch1_pin = GPIO_PIN_7;
    14. uint32_t timerx_ch1_af = GPIO_AF_2;
    15. uint32_t timerx_ch2_port = GPIOC;
    16. uint32_t timerx_ch2_port_rcu = RCU_GPIOC;
    17. uint32_t timerx_ch2_pin = GPIO_PIN_8;
    18. uint32_t timerx_ch2_af = GPIO_AF_2;
    19. /*************** GPIO config **************/
    20. //// ch0
    21. // 配置时钟
    22. rcu_periph_clock_enable(timerx_ch0_port_rcu);
    23. // 配置GPIO模式
    24. gpio_mode_set(timerx_ch0_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch0_pin);
    25. // 配置GPIO输出
    26. gpio_output_options_set(timerx_ch0_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch0_pin);
    27. // 配置复用功能
    28. gpio_af_set(timerx_ch0_port, timerx_ch0_af, timerx_ch0_pin);
    29. //// ch1
    30. // 配置时钟
    31. rcu_periph_clock_enable(timerx_ch1_port_rcu);
    32. // 配置GPIO模式
    33. gpio_mode_set(timerx_ch1_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch1_pin);
    34. // 配置GPIO输出
    35. gpio_output_options_set(timerx_ch1_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch1_pin);
    36. // 配置复用功能
    37. gpio_af_set(timerx_ch1_port, timerx_ch1_af, timerx_ch1_pin);
    38. //// ch2
    39. // 配置时钟
    40. rcu_periph_clock_enable(timerx_ch2_port_rcu);
    41. // 配置GPIO模式
    42. gpio_mode_set(timerx_ch2_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch2_pin);
    43. // 配置GPIO输出
    44. gpio_output_options_set(timerx_ch2_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch2_pin);
    45. // 配置复用功能
    46. gpio_af_set(timerx_ch2_port, timerx_ch2_af, timerx_ch2_pin);
    47. /*************** Timer config *************/
    48. // 时钟配置
    49. rcu_periph_clock_enable(timerx_rcu);
    50. // 复位定时器
    51. timer_deinit(timerx);
    52. // 倍频配置
    53. rcu_timer_clock_prescaler_config(timerx_psc);
    54. // 初始化定时器
    55. timer_parameter_struct tps;
    56. timer_struct_para_init(&tps);
    57. tps.prescaler = timerx_prescaler; // 分频计数
    58. tps.period = timerx_period; // 周期计数
    59. timer_init(timerx, &tps);
    60. ////////// 配置输出通道
    61. timer_oc_parameter_struct tops;
    62. // ch0
    63. timer_channel_output_struct_para_init(&tops);
    64. tops.outputstate = TIMER_CCX_ENABLE;
    65. timer_channel_output_config(timerx, TIMER_CH_0, &tops);
    66. // ch1
    67. timer_channel_output_struct_para_init(&tops);
    68. tops.outputstate = TIMER_CCX_ENABLE;
    69. timer_channel_output_config(timerx, TIMER_CH_1, &tops);
    70. // ch2
    71. timer_channel_output_struct_para_init(&tops);
    72. tops.outputstate = TIMER_CCX_ENABLE;
    73. timer_channel_output_config(timerx, TIMER_CH_2, &tops);
    74. #endif
    75. ////////// 输出模式配置
    76. // ch0
    77. timer_channel_output_mode_config(timerx, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    78. // ch1
    79. timer_channel_output_mode_config(timerx, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    80. // ch2
    81. timer_channel_output_mode_config(timerx, TIMER_CH_2, TIMER_OC_MODE_PWM0);
    82. // 初始化
    83. timer_enable(timerx);
    84. }
  • 定时器是哪个

  • 开启了几个通道
  • 周期是多少
  • 分频是多少

    完整代码

    ```c

    include “gd32f4xx.h”

    include “systick.h”

    include

    include “main.h”

    include “Usart0.h”

// T2 // ch0 PC6 // ch1 PC7 // ch2 PC8

// 分频计数 // 周期计数

define PRESCALER (10 - 1)

define PERIOD (SystemCoreClock / 100000 - 1)

void Usart0_recv(uint8_t *data, uint32_t len) {

}

static void PWM_config() { uint32_t timerx = TIMER2; uint32_t timerx_rcu = RCU_TIMER2; uint32_t timerx_psc = RCU_TIMER_PSC_MUL4;

  1. uint32_t timerx_prescaler = PRESCALER; // 分频计数
  2. uint32_t timerx_period = PERIOD; // 周期计数
  3. uint32_t timerx_ch0_port = GPIOC;
  4. uint32_t timerx_ch0_port_rcu = RCU_GPIOC;
  5. uint32_t timerx_ch0_pin = GPIO_PIN_6;
  6. uint32_t timerx_ch0_af = GPIO_AF_2;
  7. uint32_t timerx_ch1_port = GPIOC;
  8. uint32_t timerx_ch1_port_rcu = RCU_GPIOC;
  9. uint32_t timerx_ch1_pin = GPIO_PIN_7;
  10. uint32_t timerx_ch1_af = GPIO_AF_2;
  11. uint32_t timerx_ch2_port = GPIOC;
  12. uint32_t timerx_ch2_port_rcu = RCU_GPIOC;
  13. uint32_t timerx_ch2_pin = GPIO_PIN_8;
  14. uint32_t timerx_ch2_af = GPIO_AF_2;
  15. /*************** GPIO config **************/
  16. //// ch0
  17. // 配置时钟
  18. rcu_periph_clock_enable(timerx_ch0_port_rcu);
  19. // 配置GPIO模式
  20. gpio_mode_set(timerx_ch0_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch0_pin);
  21. // 配置GPIO输出
  22. gpio_output_options_set(timerx_ch0_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch0_pin);
  23. // 配置复用功能
  24. gpio_af_set(timerx_ch0_port, timerx_ch0_af, timerx_ch0_pin);
  25. //// ch1
  26. // 配置时钟
  27. rcu_periph_clock_enable(timerx_ch1_port_rcu);
  28. // 配置GPIO模式
  29. gpio_mode_set(timerx_ch1_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch1_pin);
  30. // 配置GPIO输出
  31. gpio_output_options_set(timerx_ch1_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch1_pin);
  32. // 配置复用功能
  33. gpio_af_set(timerx_ch1_port, timerx_ch1_af, timerx_ch1_pin);
  34. //// ch2
  35. // 配置时钟
  36. rcu_periph_clock_enable(timerx_ch2_port_rcu);
  37. // 配置GPIO模式
  38. gpio_mode_set(timerx_ch2_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch2_pin);
  39. // 配置GPIO输出
  40. gpio_output_options_set(timerx_ch2_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch2_pin);
  41. // 配置复用功能
  42. gpio_af_set(timerx_ch2_port, timerx_ch2_af, timerx_ch2_pin);
  43. /*************** Timer config *************/
  44. // 时钟配置
  45. rcu_periph_clock_enable(timerx_rcu);
  46. // 复位定时器
  47. timer_deinit(timerx);
  48. // 倍频配置
  49. rcu_timer_clock_prescaler_config(timerx_psc);
  50. // 初始化定时器
  51. timer_parameter_struct tps;
  52. timer_struct_para_init(&tps);
  53. tps.prescaler = timerx_prescaler; // 分频计数
  54. tps.period = timerx_period; // 周期计数
  55. timer_init(timerx, &tps);
  56. ////////// 配置输出通道
  57. timer_oc_parameter_struct tops;
  58. // ch0
  59. timer_channel_output_struct_para_init(&tops);
  60. tops.outputstate = TIMER_CCX_ENABLE;
  61. timer_channel_output_config(timerx, TIMER_CH_0, &tops);
  62. // ch1
  63. timer_channel_output_struct_para_init(&tops);
  64. tops.outputstate = TIMER_CCX_ENABLE;
  65. timer_channel_output_config(timerx, TIMER_CH_1, &tops);
  66. // ch2
  67. timer_channel_output_struct_para_init(&tops);
  68. tops.outputstate = TIMER_CCX_ENABLE;
  69. timer_channel_output_config(timerx, TIMER_CH_2, &tops);
  70. ////////// 输出模式配置
  71. // ch0
  72. timer_channel_output_mode_config(timerx, TIMER_CH_0, TIMER_OC_MODE_PWM1);
  73. // ch1
  74. timer_channel_output_mode_config(timerx, TIMER_CH_1, TIMER_OC_MODE_PWM1);
  75. // ch2
  76. timer_channel_output_mode_config(timerx, TIMER_CH_2, TIMER_OC_MODE_PWM1);
  77. // 初始化
  78. timer_enable(timerx);

}

static void PWM_update_ch0(float duty) { uint32_t timerx = TIMER2; uint32_t timerx_chn = TIMER_CH_0;

  1. uint32_t pulse = duty * (PERIOD + 1) / 100;
  2. /***************** pwm update *******************/
  3. // 配置输出的占空比
  4. timer_channel_output_pulse_value_config(timerx, timerx_chn, pulse);

}

static void PWM_update_ch1(float duty) { uint32_t timerx = TIMER2; uint32_t timerx_chn = TIMER_CH_1;

  1. uint32_t pulse = duty * (PERIOD + 1) / 100;
  2. /***************** pwm update *******************/
  3. // 配置输出的占空比
  4. timer_channel_output_pulse_value_config(timerx, timerx_chn, pulse);

}

static void PWM_update_ch2(float duty) { uint32_t timerx = TIMER2; uint32_t timerx_chn = TIMER_CH_2;

  1. uint32_t pulse = duty * (PERIOD + 1) / 100;
  2. /***************** pwm update *******************/
  3. // 配置输出的占空比
  4. timer_channel_output_pulse_value_config(timerx, timerx_chn, pulse);

}

static void GPIO_config() { // 配置时钟 rcu_periph_clock_enable(RCU_GPIOA); // 配置GPIO模式 gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_3); // 配置GPIO输出 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, GPIO_PIN_3); }

int main(void) { systick_config(); nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2); Usart0_init(); GPIO_config(); PWM_config();

  1. gpio_bit_reset(GPIOA, GPIO_PIN_3);
  2. int8_t i;
  3. while(1) {
  4. for(i = 0; i < 25; i++) {
  5. PWM_update_ch0(i);
  6. PWM_update_ch1(i);
  7. PWM_update_ch2(i);
  8. delay_1ms(50);
  9. }
  10. for(i = 24; i >= 0; i--) {
  11. PWM_update_ch0(i);
  12. PWM_update_ch1(i);
  13. PWM_update_ch2(i);
  14. delay_1ms(50);
  15. }
  16. delay_1ms(1000);
  17. }

}

  1. <a name="jHb7L"></a>
  2. ### 高等级定时器多通道
  3. <a name="H2UDK"></a>
  4. #### 开发流程
  5. 1. 添加Timer依赖
  6. 2. 初始化PWM,包含多通道配置
  7. 3. Break配置
  8. 4. PWM占空比控制
  9. <a name="uN2bU"></a>
  10. #### 多通道配置
  11. ```c
  12. timer_oc_parameter_struct tops;
  13. // ch0
  14. timer_channel_output_struct_para_init(&tops);
  15. tops.outputnstate = TIMER_CCXN_ENABLE;
  16. timer_channel_output_config(timerx, TIMER_CH_0, &tops);
  17. // ch1
  18. timer_channel_output_struct_para_init(&tops);
  19. tops.outputnstate = TIMER_CCXN_ENABLE;
  20. timer_channel_output_config(timerx, TIMER_CH_1, &tops);
  21. // ch2
  22. timer_channel_output_struct_para_init(&tops);
  23. tops.outputnstate = TIMER_CCXN_ENABLE;
  24. timer_channel_output_config(timerx, TIMER_CH_2, &tops);
  • 特别强调,这里的引脚分为P和N类型,不同引脚要配置不同的输出状态

    Break配置

    1. timer_break_struct_para_init(&tbps);
    2. tbps.breakpolarity = TIMER_BREAK_POLARITY_HIGH;
    3. tbps.outputautostate = TIMER_OUTAUTO_ENABLE;
    4. tbps.breakstate = TIMER_BREAK_ENABLE;
    5. timer_break_config(timerx, &tbps);
  • breakstate:break状态开启

  • ouputostate:输出状态,自动开启
  • breakpolarity:输出极性,高电平

    占空比

    1. // ch0
    2. timer_channel_output_pulse_value_config(timerx, TIMER_CH_0, 0);
    3. // ch1
    4. timer_channel_output_pulse_value_config(timerx, TIMER_CH_1, 0);
    5. // ch2
    6. timer_channel_output_pulse_value_config(timerx, TIMER_CH_2, 0);

    关心的内容

    完整代码

    ```c

    include “gd32f4xx.h”

    include “systick.h”

    include

    include “main.h”

    include “Usart0.h”

// T7 // ch0 PA7 ON // ch1 PB14 ON // ch2 PB15 ON

// 分频计数 // 周期计数

define PRESCALER (10 - 1)

define PERIOD (SystemCoreClock / 100000 - 1)

void Usart0_recv(uint8_t *data, uint32_t len) {

}

static void PWM_config() { uint32_t timerx = TIMER7; uint32_t timerx_rcu = RCU_TIMER7; uint32_t timerx_psc = RCU_TIMER_PSC_MUL2;

  1. uint32_t timerx_prescaler = PRESCALER; // 分频计数
  2. uint32_t timerx_period = PERIOD; // 周期计数
  3. uint32_t timerx_ch0_port = GPIOA;
  4. uint32_t timerx_ch0_port_rcu = RCU_GPIOA;
  5. uint32_t timerx_ch0_pin = GPIO_PIN_7;
  6. uint32_t timerx_ch0_af = GPIO_AF_3;
  7. uint32_t timerx_ch1_port = GPIOB;
  8. uint32_t timerx_ch1_port_rcu = RCU_GPIOB;
  9. uint32_t timerx_ch1_pin = GPIO_PIN_14;
  10. uint32_t timerx_ch1_af = GPIO_AF_3;
  11. uint32_t timerx_ch2_port = GPIOB;
  12. uint32_t timerx_ch2_port_rcu = RCU_GPIOB;
  13. uint32_t timerx_ch2_pin = GPIO_PIN_15;
  14. uint32_t timerx_ch2_af = GPIO_AF_3;
  15. /*************** GPIO config **************/
  16. //// ch0
  17. // 配置时钟
  18. rcu_periph_clock_enable(timerx_ch0_port_rcu);
  19. // 配置GPIO模式
  20. gpio_mode_set(timerx_ch0_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch0_pin);
  21. // 配置GPIO输出
  22. gpio_output_options_set(timerx_ch0_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch0_pin);
  23. // 配置复用功能
  24. gpio_af_set(timerx_ch0_port, timerx_ch0_af, timerx_ch0_pin);
  25. //// ch1
  26. // 配置时钟
  27. rcu_periph_clock_enable(timerx_ch1_port_rcu);
  28. // 配置GPIO模式
  29. gpio_mode_set(timerx_ch1_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch1_pin);
  30. // 配置GPIO输出
  31. gpio_output_options_set(timerx_ch1_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch1_pin);
  32. // 配置复用功能
  33. gpio_af_set(timerx_ch1_port, timerx_ch1_af, timerx_ch1_pin);
  34. //// ch2
  35. // 配置时钟
  36. rcu_periph_clock_enable(timerx_ch2_port_rcu);
  37. // 配置GPIO模式
  38. gpio_mode_set(timerx_ch2_port, GPIO_MODE_AF, GPIO_PUPD_NONE, timerx_ch2_pin);
  39. // 配置GPIO输出
  40. gpio_output_options_set(timerx_ch2_port, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, timerx_ch2_pin);
  41. // 配置复用功能
  42. gpio_af_set(timerx_ch2_port, timerx_ch2_af, timerx_ch2_pin);
  43. /*************** Timer config *************/
  44. // 时钟配置
  45. rcu_periph_clock_enable(timerx_rcu);
  46. // 复位定时器
  47. timer_deinit(timerx);
  48. // 倍频配置
  49. rcu_timer_clock_prescaler_config(timerx_psc);
  50. // 初始化定时器
  51. timer_parameter_struct tps;
  52. timer_struct_para_init(&tps);
  53. tps.prescaler = timerx_prescaler; // 分频计数
  54. tps.period = timerx_period; // 周期计数
  55. timer_init(timerx, &tps);
  56. ////////// 配置输出通道
  57. timer_oc_parameter_struct tops;
  58. // ch0
  59. timer_channel_output_struct_para_init(&tops);
  60. tops.outputnstate = TIMER_CCXN_ENABLE;
  61. timer_channel_output_config(timerx, TIMER_CH_0, &tops);
  62. // ch1
  63. timer_channel_output_struct_para_init(&tops);
  64. tops.outputnstate = TIMER_CCXN_ENABLE;
  65. timer_channel_output_config(timerx, TIMER_CH_1, &tops);
  66. // ch2
  67. timer_channel_output_struct_para_init(&tops);
  68. tops.outputnstate = TIMER_CCXN_ENABLE;
  69. timer_channel_output_config(timerx, TIMER_CH_2, &tops);
  70. ////////// 输出模式配置
  71. // ch0
  72. timer_channel_output_mode_config(timerx, TIMER_CH_0, TIMER_OC_MODE_PWM1);
  73. // ch1
  74. timer_channel_output_mode_config(timerx, TIMER_CH_1, TIMER_OC_MODE_PWM1);
  75. // ch2
  76. timer_channel_output_mode_config(timerx, TIMER_CH_2, TIMER_OC_MODE_PWM1);
  77. ////////// Break配置
  78. timer_break_parameter_struct tbps;
  79. timer_break_struct_para_init(&tbps);
  80. tbps.breakpolarity = TIMER_BREAK_POLARITY_HIGH;
  81. tbps.outputautostate = TIMER_OUTAUTO_ENABLE;
  82. tbps.breakstate = TIMER_BREAK_ENABLE;
  83. timer_break_config(timerx, &tbps);
  84. // 初始化
  85. timer_enable(timerx);

}

static void PWM_update_ch0(float duty) { uint32_t timerx = TIMER7; uint32_t timerx_chn = TIMER_CH_0;

  1. uint32_t pulse = duty * (PERIOD + 1) / 100;
  2. /***************** pwm update *******************/
  3. // 配置输出的占空比
  4. timer_channel_output_pulse_value_config(timerx, timerx_chn, pulse);

}

static void PWM_update_ch1(float duty) { uint32_t timerx = TIMER7; uint32_t timerx_chn = TIMER_CH_1;

  1. uint32_t pulse = duty * (PERIOD + 1) / 100;
  2. /***************** pwm update *******************/
  3. // 配置输出的占空比
  4. timer_channel_output_pulse_value_config(timerx, timerx_chn, pulse);

}

static void PWM_update_ch2(float duty) { uint32_t timerx = TIMER7; uint32_t timerx_chn = TIMER_CH_2;

  1. uint32_t pulse = duty * (PERIOD + 1) / 100;
  2. /***************** pwm update *******************/
  3. // 配置输出的占空比
  4. timer_channel_output_pulse_value_config(timerx, timerx_chn, pulse);

}

static void GPIO_config() { // 配置时钟 rcu_periph_clock_enable(RCU_GPIOA); // 配置GPIO模式 gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_3); // 配置GPIO输出 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, GPIO_PIN_3); }

int main(void) { systick_config(); nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2); Usart0_init(); GPIO_config(); PWM_config();

  1. gpio_bit_reset(GPIOA, GPIO_PIN_3);
  2. int8_t i;
  3. while(1) {
  4. for(i = 0; i < 25; i++) {
  5. PWM_update_ch0(i);
  6. PWM_update_ch1(i);
  7. PWM_update_ch2(i);
  8. delay_1ms(50);
  9. }
  10. for(i = 24; i >= 0; i--) {
  11. PWM_update_ch0(i);
  12. PWM_update_ch1(i);
  13. PWM_update_ch2(i);
  14. delay_1ms(50);
  15. }
  16. delay_1ms(1000);
  17. }

}

```

练习

  • 实现多个通道的pwm点灯
  • 实现通用定时器pwm点灯
  • 实现高级定时器pwm点灯