[MCU Notes] NodeMcu Introduction - LUA Scripting Language PWM Dimming

Publisher:数据梦行者Latest update time:2019-02-15 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

First, the code


--pwm.lua

pin = 4

dutyMAX = 1023

dutyMIN = 0

flag = 0

pwm.setup(pin,100,dutyMAX)

pwm.start(pin)

function changeDuty()

    local duty = pwm.getduty(pin);

    print(duty);

    if(flag == 0) then

        if (duty > dutyMIN+10) then

            pwm.setduty(pin,duty-10);

        else

            flag = 1;

        end

    else

        if (duty < dutyMAX-10) then

            pwm.setduty(pin,duty + 10);

        else

            flag = 0;

        end

    end

end

tmr.alarm(0, 50, tmr.ALARM_AUTO, changeDuty);

The function of this program is very simple, that is, calling the changeDuty function every 50ms, and this function is implemented by timer callback.


tmr.alarm(0, 50, tmr.ALARM_AUTO, changeDuty);

The above sentence is to register the changeDuty function to the timer, and the timer will call back every 50ms. It is even simpler in this function. Each time it enters, it will print the current PWM duty cycle and change the duty cycle to achieve PWM dimming breathing light.


Let’s explain step by step:


Pin initialization PWM function:


pwm.setup(pin,clock,duty)

pin 1~12, IO index

clock 1~1000, pwm frequency

duty 0~1023, pwm duty cycle, maximum 1023 (10bit)

Custom Functions

function function name()

Code Blocks

end 

If flow control

if (condition) then

[Statements executed when conditions are met]

else

[Statements that are executed if the condition is not met]

end 

Timer

NodeMCU provides 7 static timers, numbered 0-6.

To release resources using this timer, call tmr.unregister().

tmr.alarm([id/ref], interval_ms, mode, func())

parameter:

id/ref timer ID (0-6) or object,

interval_ms The timer interval in milliseconds. The maximum value is 6870947.

mode Timer mode:

tmr.ALARM_SINGLE One-time alarm (no need to call tmr.unregister())

tmr.ALARM_SEMI Manual repeat alarm (call tmr.start() to restart)

tmr.ALARM_AUTO Automatically repeat the alarm

func(timer) callback function called with the timer object as parameter 

There are two reference materials that are worth downloading. You can find them on Baidu, so I won’t upload them here.


Keywords:MCU Reference address:[MCU Notes] NodeMcu Introduction - LUA Scripting Language PWM Dimming

Previous article:[MCU Notes] Getting Started with NodeMcu—LAN Communication
Next article:[MCU Notes] Getting Started with NodeMcu—Building the ESP8266 Development Environment

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号