-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple tone(), analogWrite(), and Servo (#4640)
Remove and rewrite all the parts of the core/libraries using TIMER1 and consolidate into a single, shared waveform generation interrupt structure. Tone, analogWrite(), Servo all now just call into this shared resource to perform their tasks so are all compatible and can be used simultaneously. This setup enables multiple tones, analogWrites, servos, and stepper motors to be controlled with reasonable accuracy. It uses both TIMER1 and the internal ESP cycle counter to handle timing of waveform edges. TIMER1 is used in non-reload mode and only edges cause interrupts. The interrupt is started and stopped as required, minimizing overhead when these features are not being used. A generic "startWaveform(pin, high-US, low-US, runtime-US)" and "stopWaveform(pin)" allow for further types of interfaces. Minimum high or low period is ~1 us. Add a tone(float) method, useful when working with lower frequencies. Fixes #4321. Fixes 4349.
- Loading branch information
1 parent
ea4720b
commit ebda795
Showing
12 changed files
with
600 additions
and
841 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Oops, something went wrong.
ebda795
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
ebda795
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@earlephilhower
re. Support multiple tone(), analogWrite(), and Servo (#4640) [commit 8Jun18]
This commit breaks:
Reason: because there are now two alternatives being:
and
To avoid user-confusion(!?), I would suggest it would benefit from:
EITHER: changing the "unsigned int frequency" to just "int frequency" in the relevant [Arduino.h] and assoicated files?
OR: adding a 3rd alternative of "void tone(uint8_t _pin, int frequency, unsigned long duration = 0);" in the relevant [Arduino.h] and associated files?
FYI: My setup is Arduino-IDE using latest github-ESP8266-arduino-software and a D1-mini-ESP8266.
ebda795
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PWM doesn't work with this. I tested only this commit merged in 2.4.1 and then the complete git version, but no PWM. I need PWM and tone in one sketch.
tested with Wemos D1 mini with pin D1 connected to A0. with 2.4.1 it prints corresponding analog values. with this commit nothing.
Edit: with
pinMode(D1, OUTPUT);
it does something but A0 is at 1023 from PWM value 250Edit2: with
analogWriteRange(1023);
it almost works. only the analog values in under PWM 100 are not good. default range until 2.4.1 was 1023, now it is 256?ebda795
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JAndrassy PWM and analog in are completely different. PWM will drive either a full 3.3 or 0 volts, with the duty cycle being the percent of time it's on. If you physically connect the pins, which you should not because the ESP Ain only had a 1.0 v range. Assuming it does not kill the device, you'll just be reading garbage. This is not an AVR and there are no built in DACs, and the built in ADC is very limited and relatives slow.
ebda795
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@earlephilhower Wemos has a cirquit for 3.3 V A0. Not ADC is the issue. Backward compatibility is the issue. You did a great work for this commit, but until now it was not required to set pin mode before using analogWrite and the default range was 1023. Arduino docs for analogWrite has "You do not need to call pinMode() to set the pin as an output before calling analogWrite()." and esp8266 core has met this. And the esp8266 docs has " value may be in range from 0 to PWMRANGE, which is equal to 1023 by default. ".
ebda795
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@earlephilhower
re. Compatibility and IRQ fixed for waveform/tone/pwm (#4872) - committed 2Jul18 (be7a732)
The latest commit (as above 2Jul18) fixes the "tone(D8,3000);" and "tone(D8,3000,100);" compile-issues.
Interestinly, "tone(D8,(uint16_t)3000)" now also works again!
Mnay thanks! :-)