PWM
更新时间:2019-01-25 14:44:23
接口列表
接口详情
int32_t hal_pwm_init(pwm_dev_t *pwm)
描述 |
初始化指定PWM |
参数 |
pwm:PWM设备描述,定义需要初始化的PWM参数 |
返回值 |
返回成功或失败, 返回0表示PWM初始化成功,非0表示失败 |
int32_t hal_pwm_start(pwm_dev_t *pwm)
描述 |
开始输出指定PWM |
参数 |
pwm:PWM设备描述 |
返回值 |
返回成功或失败, 返回0表示PWM开始输出成功,非0表示失败 |
int32_t hal_pwm_stop(pwm_dev_t *pwm)
描述 |
停止输出指定PWM |
参数 |
pwm:PWM设备描述 |
返回值 |
返回成功或失败, 返回0表示PWM停止输出成功,非0表示失败 |
描述 |
修改指定PWM参数 |
参数 |
pwm:PWM设备描述 |
|
para:新配置参数 |
返回值 |
返回成功或失败, 返回0表示PWM参数修改成功,非0表示失败 |
int32_t hal_pwm_finalize(pwm_dev_t *pwm)
描述 |
关闭指定PWM |
参数 |
pwm:PWM设备描述 |
返回值 |
返回成功或失败, 返回0表示PWM关闭成功,非0表示失败 |
相关结数据结构
pwm_dev_t
typedef struct {
uint8_t port;
pwm_config_t config;
void *priv;
} pwm_dev_t;
pwm_config_t
typedef struct {
float duty_cycle;
uint32_t freq;
} pwm_config_t;
使用示例
#include <aos/hal/pwm.h>
#define PWM1_PORT_NUM 1
pwm_dev_t pwm1;
int application_start(int argc, char *argv[])
{
int ret = -1;
pwm_config_t pwm_cfg;
static int count = 0;
pwm1.port = PWM1_PORT_NUM;
pwm1.config.duty_cycle = 0.5f;
pwm1.config.freq = 300000;
ret = hal_pwm_init(&pwm1);
if (ret != 0) {
printf("pwm1 init error !\n");
}
ret = hal_pwm_start(&pwm1);
if (ret != 0) {
printf("pwm1 start error !\n");
}
while(1) {
if (count == 5) {
memset(&pwm_cfg, 0, sizeof(pwm_config_t));
pwm_cfg.duty_cycle = 0.3f;
ret = hal_pwm_para_chg(&pwm1, pwm_cfg);
if (ret != 0) {
printf("pwm1 para change error !\n");
}
}
if (count == 20) {
hal_pwm_stop(&pwm1);
hal_pwm_finalize(&pwm1);
}
aos_msleep(1000);
count++;
};
}
注:port为逻辑端口号,其与物理端口号的对应关系见具体的对接实现
移植说明
新建hal_pwm_xxmcu.c和hal_pwm_xxmcu.h的文件,并将这两个文件放到platform/mcu/xxmcu/hal目录下。在hal_pwm_xxmcu.c中实现所需要的hal函数,hal_pwm_xxmcu.h中放置相关宏定义。<br /> 参考platform/mcu/stm32l4xx/src/STM32L496G-Discovery/hal/hal_pwm_stm32l4.c