学习目标

  1. 学习如何在STC8H上配置PWMB
  2. 掌握PWMB的各个配置
  3. 学习如何使用PWMB控制震动马达
  4. 掌握调试PWM的方法

    学习内容

    PWMB的应用

    实现不同占空比下的震动的效果

    拷贝依赖

  5. STC8H_PWM.h``STC8H_PWM.c

  6. NVIC.h``NVIC.c
  7. Switch.h

    实现main.c

    ```c

    include “Config.h”

    include “Delay.h”

    include “GPIO.h”

    include “STC8H_PWM.h”

    include “NVIC.h”

    include “Switch.h”

define MOTOR P01

define PREQ 1000

define PERIOD (MAIN_Fosc / PREQ)

void GPIO_config(void) { GPIO_InitTypeDef GPIO_InitStructure; //结构定义 GPIO_InitStructure.Pin = GPIO_Pin_1; //指定要初始化的IO, GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP GPIO_Inilize(GPIO_P0, &GPIO_InitStructure);//初始化 } void PWM_config(void) { PWMx_InitDefine PWMx_InitStructure;

  1. PWMx_InitStructure.PWM_Mode = CCMRn_PWM_MODE1; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
  2. PWMx_InitStructure.PWM_Duty = 0; //PWM占空比时间, 0~Period
  3. PWMx_InitStructure.PWM_EnoSelect = ENO6P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
  4. PWM_Configuration(PWM6, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
  5. PWMx_InitStructure.PWM_Period = PERIOD - 1; //周期时间, 0~65535
  6. PWMx_InitStructure.PWM_DeadTime = 0; //死区发生器设置, 0~255
  7. PWMx_InitStructure.PWM_MainOutEnable= ENABLE; //主输出使能, ENABLE,DISABLE
  8. PWMx_InitStructure.PWM_CEN_Enable = ENABLE; //使能计数器, ENABLE,DISABLE
  9. PWM_Configuration(PWMB, &PWMx_InitStructure); //初始化PWM通用寄存器, PWMA,PWMB
  10. PWM6_SW(PWM6_SW_P01); //PWM6_SW_P21,PWM6_SW_P54,PWM6_SW_P01,PWM6_SW_P75
  11. NVIC_PWM_Init(PWMB,DISABLE,Priority_0);

}

void main(){ PWMx_Duty duty; u8 duty_percent = 0; // 0 - 100

  1. EAXSFR();
  2. GPIO_config();
  3. PWM_config();
  4. duty.PWM6_Duty = 0;
  5. UpdatePwm(PWM6, &duty);
  6. while(1){
  7. delay_ms(10);
  8. // 设置占空比
  9. duty.PWM6_Duty = PERIOD * duty_percent / 100;
  10. UpdatePwm(PWMB, &duty);
  11. // 修改占空比 0 -> 100
  12. duty_percent++;
  13. if(duty_percent > 100){
  14. duty_percent = 0;
  15. }
  16. }

} ```

练习题

  1. 实现马达震动。
  2. 分析电动牙刷从原理图到编码实现过程中的环节。