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.
Previous article:[MCU Notes] Getting Started with NodeMcu—LAN Communication
Next article:[MCU Notes] Getting Started with NodeMcu—Building the ESP8266 Development Environment
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Case! EMI rectification of power supply
- Reasons for choosing 4-20mA for flow meter
- WiMax - the new wireless broadband technology
- The US-China patent dispute - MPEG-2 - is over, but what about MPEG-4?
- How to deal with the situation when the corresponding type of Flash cannot be found when using Keil for STM32 development
- I want to ask why this motor cannot start? The voltage from B+ to B- is normal, but after starting, the voltage from B+ to M- jumps and the motor vibrates.
- Summary of equivalent circuits of common electronic components
- Comparison of several MicroPython IDEs
- Study on digital filter to filter out power frequency and harmonic interference in electronic measurement system
- [RVB2601 Creative Application Development] @fxyc87 RVB2601-Snake Game