PWM - Shaofa/AliOS-Things-Certification-Manual GitHub Wiki

#PWM API

API
说明
PWM.start(id)
功能:配置和启动PWM
参数: id:和板级配置文件中的id保持一致
freq:频率
duty:占空比
返回值:成功:返回资源handle,失败:-1
PWM.getDuty(handle)
功能:获取当前pwm周期
参数:handle:资源handle,为PWM.open的返回值;
返回值:占空比值
PWM.setDuty(handle,duty)
功能:配置duty
参数: handle:资源handle,为PWM.open的返回值;
duty:占空比值
返回值:0=ok other=fail
PWM.getFreq(handle)
功能:获取当前pwm频率
参数:handle:资源handle,为PWM.open的返回值;
返回值:频率值
PWM.setFreq(handle, freq)
功能:配置freq
参数: handle:资源handle,为PWM.open的返回值;
freq:频率
返回值:0=ok other=fail
PWM.stop(handle)
功能:停止PWM
参数: handle:资源handle,为PWM.open的返回值;
返回值:0=ok other=fail

#板级配置参数

参数名
类型/功能/值
说明
"id"
功能:资源唯一性标志;
类型:string
        <span data-type="color" style="color:#F5222D">值:</span>任意,保持数组内id值唯一;</div>
    </td>
    <td rowspan="1" colSpan="1">
      <div data-type="p">该id值和js层GPIO.open时的id值保持一致;</div>
    </td>
  </tr>
  <tr height="34px">
    <td rowspan="1" colSpan="1">
      <div data-type="p">&quot;port&quot;</div>
    </td>
    <td rowspan="1" colSpan="1">
      <div data-type="p"><span data-type="color" style="color:#F5222D">功能:</span>端口值; </div>
      <div data-type="p">

        <span data-type="color" style="color:#F5222D">类型:</span>number </div>
      <div data-type="p">
        <span data-type="color" style="color:#F5222D">值:</span>和板级资源描述保持一致;</div>
    </td>
    <td rowspan="1" colSpan="1">
      <div data-type="p">该port值和HAL层API对应的port保持一致;</div>
    </td>
  </tr>
  <tr height="34px">
    <td rowspan="1" colSpan="1">
      <div data-type="p">&quot;freq&quot;</div>
    </td>
    <td rowspan="1" colSpan="1">
      <div data-type="p"><span data-type="color" style="color:#F5222D">功能:</span>频率值; </div>
      <div data-type="p">

        <span data-type="color" style="color:#F5222D">类型:</span>number </div>
      <div data-type="p">
        <span data-type="color" style="color:#F5222D">值:</span>依据需求进行设置;</div>
    </td>
    <td rowspan="1" colSpan="1">
      <div data-type="p"></div>
    </td>
  </tr>
  <tr height="34px">
    <td rowspan="1" colSpan="1">
      <div data-type="p">&quot;duty&quot;</div>
    </td>
    <td rowspan="1" colSpan="1">
      <div data-type="p"><span data-type="color" style="color:#F5222D">功能:</span>占空比; </div>
      <div data-type="p">

        <span data-type="color" style="color:#F5222D">类型:</span>number </div>
      <div data-type="p">
        <span data-type="color" style="color:#F5222D">

值:0-100;

占空比的范围是0-100,表示0%-100%

#板级配置示范

{
	"PWM":[
		{
		"id":"buzzer",
		"port":4,
		"freq":5000,
		"duty":50
		}
	]
}

#ESP32之无源蜂鸣器

#硬件

1jpg.png | left | 423x409

#接线

EPS32 IO4引脚 连接 无源蜂鸣器模块的'+'引脚; EPS32 GND引脚 连接 无源蜂鸣器模块的GND引脚;

#配置

/*apps/js/board_config.json*/
{
	"PWM":[
		{
		"id":"buzzer",
		"port":4,
		"freq":5000,
		"duty":50
		}
	]

}

#代码

/*samples/js/buzzer.js*/
print('buzzer uart test.....................');
var buzzer_handle=PWM.start('buzzer'); 
var cur_duty = PWM.getDuty(buzzer_handle);
console.log('cur_duty:'+cur_duty);
setInterval(function() {
	cur_duty += 1;
	if(cur_duty >= 100)cur_duty=0;
	PWM.setDuty(buzzer_handle,cur_duty);
}, 500);
print('end buzzer test........................');

#现象

蜂鸣器声音由弱到强转换。

#TODO

⚠️ **GitHub.com Fallback** ⚠️