学习目标
- 学习如何在STC8H上配置PWMB
- 掌握PWMB的各个配置
- 学习如何使用PWMB控制震动马达
-
学习内容
PWMB的应用
拷贝依赖
STC8H_PWM.h``STC8H_PWM.cNVIC.h``NVIC.cSwitch.h实现main.c
```cinclude “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;
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_MODE2PWMx_InitStructure.PWM_Duty = 0; //PWM占空比时间, 0~PeriodPWMx_InitStructure.PWM_EnoSelect = ENO6P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8PPWM_Configuration(PWM6, &PWMx_InitStructure); //初始化PWM, PWMA,PWMBPWMx_InitStructure.PWM_Period = PERIOD - 1; //周期时间, 0~65535PWMx_InitStructure.PWM_DeadTime = 0; //死区发生器设置, 0~255PWMx_InitStructure.PWM_MainOutEnable= ENABLE; //主输出使能, ENABLE,DISABLEPWMx_InitStructure.PWM_CEN_Enable = ENABLE; //使能计数器, ENABLE,DISABLEPWM_Configuration(PWMB, &PWMx_InitStructure); //初始化PWM通用寄存器, PWMA,PWMBPWM6_SW(PWM6_SW_P01); //PWM6_SW_P21,PWM6_SW_P54,PWM6_SW_P01,PWM6_SW_P75NVIC_PWM_Init(PWMB,DISABLE,Priority_0);
}
void main(){ PWMx_Duty duty; u8 duty_percent = 0; // 0 - 100
EAXSFR();GPIO_config();PWM_config();duty.PWM6_Duty = 0;UpdatePwm(PWM6, &duty);while(1){delay_ms(10);// 设置占空比duty.PWM6_Duty = PERIOD * duty_percent / 100;UpdatePwm(PWMB, &duty);// 修改占空比 0 -> 100duty_percent++;if(duty_percent > 100){duty_percent = 0;}}
} ```
练习题
- 实现马达震动。
- 分析电动牙刷从原理图到编码实现过程中的环节。
