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();