[ESP8266] PWM Demo_For_ESP8266_NONOS_SDK

ESP_Faye
Posts: 1623
Joined: Mon Oct 27, 2014 11:08 am

[ESP8266] PWM Demo_For_ESP8266_NONOS_SDK

Postby ESP_Faye » Fri Nov 13, 2015 6:47 pm

This is for ESP8266_NONOS_SDK.
If using ESP8266_NONOS_SDK_V1.5.0 or later version, please add "-lcrypto" in 'LINKFLAGS_eagle.app.v6" area of Makefile.

The PWM sample in IOT_Demo of ESP8266_NONOS_SDK:
1. It has a 100us tuning of PWM duty besides the original period.
2. During the tuning, it can not reach the 100% high-level. The tuning may seem like a noise wave.
3. This method can get a high accuracy(long period leads to high accuracy), but if duty gets close to 100%, the accuracy turns to be lower.

There is another PWM demo as the attachment which can get 100% duty, and reliable period, but the accuracy is lower.

How to use this demo:
1. Copy pwm.c into app/user, copy pwm .h into app/include.
Remove "-lpwm" in app/Makefile.

2. Sample codes:

Code: Select all

#define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PWM_0_OUT_IO_NUM 12
#define PWM_0_OUT_IO_FUNC  FUNC_GPIO12

#define PWM_1_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PWM_1_OUT_IO_NUM 15
#define PWM_1_OUT_IO_FUNC  FUNC_GPIO15

#define PWM_2_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PWM_2_OUT_IO_NUM 13
#define PWM_2_OUT_IO_FUNC  FUNC_GPIO13

uint32 io_info[][3] = {   {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM},
                          {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM},
                          {PWM_2_OUT_IO_MUX,PWM_2_OUT_IO_FUNC,PWM_2_OUT_IO_NUM},
                          };

u32 duty[3] = {600,604,634};
pwm_init(1000, duty,3,io_info);

// Set duty, the max duty is 8191 which is set in pwm.h
pwm_set_duty(duty, channel);   // 0~2
pwm_set_period(uint32 period);  // set period;
pwm_start();


本帖基于 ESP8266_NONOS_SDK。
如果使用 ESP8266_NONOS_SDK_V1.5.0 或之后版本,请在示例的 Makefile 中 'LINKFLAGS_eagle.app.v6" 区域增加 "-lcrypto" 。

ESP8266_NONOS_SDK 的 IOT Demo 中提供的 PWM 示例版本特性如下:
1.在原始的 period 周期之外,外加一个 100us 的时间进行 duty 的微调。
2.在微调的时间内,不能达到 100% 的高电平(毛刺就是微调)。
3.这样做的优点是总体的精度变高(周期越大,精度也会越高)。缺点是在 duty 接近100%时的,精度会变低(因为 duty 高时,精度低肉眼不易察觉)。

乐鑫还提供另外一种 PWM 工作方式,可以到达 100% Duty,周期也是准的,但总体精度低 2us。

附件是另一种 PWM 的实现代码,可以设置100%的占空比。

请按如下方法使用:

1. pwm.c 放在 app/user 下面 pwm.h 放在 app/include 下面。

同时 app/Makefile 里去掉 -lpwm,即不再使用原有的 PWM 库文件。

2. 使用示例代码请参考如下,最大占空比值是 8191,在 pwm.h 中有定义。

Code: Select all

//初始化
#define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PWM_0_OUT_IO_NUM 12
#define PWM_0_OUT_IO_FUNC  FUNC_GPIO12

#define PWM_1_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PWM_1_OUT_IO_NUM 15
#define PWM_1_OUT_IO_FUNC  FUNC_GPIO15

#define PWM_2_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PWM_2_OUT_IO_NUM 13
#define PWM_2_OUT_IO_FUNC  FUNC_GPIO13

uint32 io_info[][3] = {   {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM},
                          {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM},
                          {PWM_2_OUT_IO_MUX,PWM_2_OUT_IO_FUNC,PWM_2_OUT_IO_NUM},
                          };

u32 duty[3] = {600,604,634};
pwm_init(1000, duty,3,io_info);

// 重设占空比。最大占空比在 pwm.h 里设置 8191
pwm_set_duty(duty, channel);   // 0~2
pwm_set_period(uint32 period);  // set period
pwm_start();
Attachments
PWM_Demo_2.zip
(4.4 KiB) Downloaded 1261 times

dearwind153
Posts: 49
Joined: Sun Feb 28, 2016 7:47 pm

Re: [ESP8266] PWM Demo

Postby dearwind153 » Wed Apr 06, 2016 7:13 pm

good job!

berkutta
Posts: 15
Joined: Tue Mar 15, 2016 6:30 pm

Re: [ESP8266] PWM Demo

Postby berkutta » Wed Apr 06, 2016 7:38 pm

Doesn't work, no voltage or pwm on the 3 pins, what can I do?

berkutta
Posts: 15
Joined: Tue Mar 15, 2016 6:30 pm

Re: [ESP8266] PWM Demo

Postby berkutta » Mon May 02, 2016 9:18 pm

Can you give sample code for 1 channel PWM?

JayYang
Posts: 1
Joined: Fri Jul 01, 2016 5:38 pm

Re: [ESP8266] PWM Demo_For_ESP8266_NONOS_SDK

Postby JayYang » Fri Jul 01, 2016 5:54 pm

Hi 乐鑫FAE,

我使用这个PWM demo code时,当duty设置为0时,LED会有微弱光,不能关闭,用逻辑分析仪测得有1.6us的高电平每1000ms周期内,见附件图。但是duty为100时,PWM只有10ns的低电平,效果比SDK自带PWM库会好一点。为什么duty设置为0时,duty不为0?麻烦帮我解决。谢谢!
Attachments
pwm1.png

pillarpeng
Posts: 16
Joined: Sat May 14, 2016 10:31 am

Re: [ESP8266] PWM Demo_For_ESP8266_NONOS_SDK

Postby pillarpeng » Tue Aug 09, 2016 10:04 am

Hi 乐鑫FAE,

遇到同样的问题,可以解决一下吗??

我使用这个PWM demo code时,当duty设置为0时,LED会有微弱光,不能关闭,用逻辑分析仪测得有1.6us的高电平每1000ms周期内,见附件图。但是duty为100时,PWM只有10ns的低电平,效果比SDK自带PWM库会好一点。为什么duty设置为0时,duty不为0?麻烦帮我解决。谢谢!

Who is online

Users browsing this forum: No registered users and 1 guest