1 Star 0 Fork 5.2K

OpenHarmony-build / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
driver-platform-pwm-des.md 31.11 KB
一键复制 编辑 原始数据 按行查看 历史
duangavin123 提交于 2021-11-12 17:25 . update 导入OpenHarmony工程

PWM

概述

PWM是脉冲宽度调制(Pulse Width Modulation)的缩写,是一种对模拟信号电平进行数字编码并将其转换为脉冲的技术。常用于马达控制、背光亮度调节等。

PWM接口定义了操作PWM设备的通用方法集合,包括:

  • PWM设备句柄获取和释放。
  • PWM周期、占空比、极性的设置。
  • PWM使能和关闭。
  • PWM配置信息的获取和设置

接口说明

表 1 PWM驱动API接口功能介绍

功能分类

接口名

描述

PWM设备句柄获取和释放

PwmOpen

获取PWM设备句柄

PwmClose

释放PWM设备句柄

PWM周期、占空比、极性的设置

PwmSetPeriod

设置PWM周期

PwmSetDuty

设置PWM占空比

PwmSetPolarity

设置PWM极性

PWM使能和关闭

PwmEnable

使能PWM

PwmDisable

禁用PWM

PWM配置信息的获取和设置

PwmSetConfig

设置PWM设备配置信息

PwmGetConfig

获取PWM设备配置信息

说明: 本文涉及的所有接口,仅限内核态使用,不支持在用户态使用。

使用指导

使用流程

使用PWM的一般流程如图1所示。

图 1 PWM使用流程图

获取PWM设备句柄

在操作PWM设备时,首先要调用PwmOpen获取PWM设备句柄,该函数会返回指定设备号的PWM设备句柄。

DevHandle PwmOpen(uint32_t num);

表 2 PwmOpen参数和返回值描述

参数

参数描述

num

PWM设备号

返回值

返回值描述

NULL

获取PWM设备句柄失败

设备句柄

对应的PWM设备句柄

假设系统中的PWM设备号为0,获取该PWM设备句柄的示例如下:

uint32_t num = 0;             /* PWM设备号 */
DevHandle pwm = NULL; /* PWM设备句柄 /

/* 获取PWM设备句柄 */
pwm = PwmOpen(num);
if (pwm == NULL) {
    HDF_LOGE("PwmOpen: pwm%d failed", num);
    return;
}

设置PWM周期

int32_t PwmSetPeriod(DevHandle handle, uint32_t period);

表 3 PwmSetPeriod参数和返回值描述

参数

参数描述

handle

PWM设备句柄

period

周期,单位纳秒

返回值

返回值描述

0

设置周期成功

负数

设置周期失败

int32_t ret;
uint32_t period = 1000;          /* 周期1000纳秒 */
ret = PwmSetPeriod(pwm, period); /* 设置PWM周期 */
if (ret != 0) {
    HDF_LOGE("PwmSetPeriod: failed, ret %d", ret);
}

设置PWM占空比

int32_t PwmSetDuty(DevHandle handle, uint32_t duty);

表 4 PwmSetDuty参数和返回值描述

参数

参数描述

handle

PWM设备句柄

duty

占空比,单位纳秒

返回值

返回值描述

0

设置占空比成功

负数

设置占空比失败

int32_t ret;
uint32_t duty = 500;        /* 占空比500纳秒 */
ret = PwmSetDuty(pwm, duty); /* 设置PWM占空比 */
if (ret != 0) {
    HDF_LOGE("PwmSetDuty: failed, ret %d", ret);
}

设置PWM极性

int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity);

表 5 PwmSetPolarity参数和返回值描述

参数

参数描述

handle

PWM设备句柄

polarity

极性,PWM_NORMAL_POLARITY为正常极性,PWM_INVERTED_POLARITY为反转极性。

返回值

返回值描述

0

设置极性成功

负数

设置极性失败

int32_t ret;
uint8_t polarity = PWM_INVERTED_POLARITY; /* 反转极性 */
ret = PwmSetPolarity(pwm, polarity);      /* 设置PWM反转极性 */
if (ret != 0) {
    HDF_LOGE("PwmSetPolarity: failed, ret %d", ret);
}

使能PWM

int32_t PwmEnable(DevHandle handle);

表 6 PwmEnable参数和返回值描述

参数

参数描述

handle

PWM设备句柄

返回值

返回值描述

0

使能PWM成功

负数

使能PWM失败

int32_t ret;
ret = PwmEnable(pwm); /* 使能PWM */
if (ret != 0) {
    HDF_LOGE("PwmEnable: failed, ret %d", ret);
}

禁用PWM

int32_t PwmDisable(DevHandle handle);

表 7 PwmDisable参数和返回值描述

参数

参数描述

handle

PWM设备句柄

返回值

返回值描述

0

关闭PWM成功

负数

关闭PWM失败

int32_t ret;
ret = PwmDisable(pwm); /* 禁用PWM */
if (ret != 0) {
    HDF_LOGE("PwmDisable: failed, ret %d", ret);
}

获取PWM设备配置信息

int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config);

表 8 PwmGetConfig参数和返回值描述

参数

参数描述

handle

PWM设备句柄

config

PWM设备配置信息

返回值

返回值描述

0

获取配置成功

负数

获取配置失败

int32_t ret;
struct PwmConfig config= {0};     /* PWM配置信息 */
ret = PwmGetConfig(pwm, &config); /* 获取PWM设备配置信息 */
if (ret != 0) {
    HDF_LOGE("PwmGetConfig: failed, ret %d", ret);
}

设置PWM设备配置信息

int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config);

表 9 PwmSetConfig参数和返回值描述

参数

参数描述

handle

PWM设备句柄

config

PWM设备配置信息

返回值

返回值描述

0

设置配置成功

负数

设置配置失败

int32_t ret;
struct PwmConfig config= {0};          /* PWM配置信息 */
config.duty = 500;                     /* 占空比500纳秒 */
config.period = 1000;                  /* 周期1000纳秒 */
config.number = 0;                     /* 一直输出方波 */ 
config.polarity = PWM_NORMAL_POLARITY; /* 正常极性 */
ret = PwmSetConfig(pwm, &config);      /* 设置PWM设备配置信息 */
if (ret != 0) {
    HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret);
}

释放PWM设备句柄

void PwmClose(DevHandle handle);

该函数会释放掉由PwmClose申请的资源。

表 10 PwmClose参数描述

参数

参数描述

handle

PWM设备句柄

PwmClose(pwm); /* 释放PWM设备句柄 */

使用实例

PWM设备完整的使用示例如下所示,首先获取PWM设备句柄,然后设置PWM设备配置信息,使能PWM,最后释放PWM设备句柄。

#include "hdf_log.h"
#include "osal_time.h"
#include "pwm_if.h"

void PwmTestSample(void)
{
    int32_t ret;
    struct PwmConfig config;      /* PWM配置信息 */
    DevHandle pwm = NULL;         /* PWM设备句柄 */
  
    pwm = PwmOpen(0);             /* 获取PWM设备句柄 */
    if (pwm == NULL) {
        HDF_LOGE("PwmOpen: pwm0 failed");
        return;
    }
    /* 获取PWM设备配置信息 */
    ret = PwmGetConfig(pwm, &config);
    if (ret != 0) {
        HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret);
        goto err;
    }
    config.duty = 500;            /* 占空比500纳秒 */
    config.period = 1000;         /* 周期1000纳秒 */
    /* 设置PWM设备配置信息 */
    ret = PwmSetConfig(pwm, &config);
    if (ret != 0) {
        HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret);
        goto err;
    }
    /* 使能PWM */
    ret = PwmEnable(pwm);
    if (ret != 0) {
        HDF_LOGE("PwmEnable: failed, ret %d\n", ret);
        goto err;
    }
    /* 睡眠10秒 */
    OsalSleep(10);
    /* 禁用PWM */
    ret = PwmDisable(pwm);
    if (ret != 0) {
        HDF_LOGE("PwmDisable: failed, ret %d\n", ret);
        goto err;
    }
err:
    /* 释放PWM设备句柄 */
    PwmClose(pwm);
}
1
https://gitee.com/openharmony-build/docs.git
git@gitee.com:openharmony-build/docs.git
openharmony-build
docs
docs
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891