1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-17 12:02:15 +03:00

Allow waveforms to be specified in clock cycles (#7211)

* Allow waveforms to be specified in clock cycles

Allow the PWM to specify sub-microsecond waveform edges, as have been
proposed by @dok-net and me.  No other changes intended.

This will increase the linearity at 30 and 40 kHZ PWM rates, but leave
most other things unaffected.

* Cycle-accurate wafveform to specify Tone periods

Co-authored-by: Develo <deveyes@gmail.com>
This commit is contained in:
Earle F. Philhower, III
2020-04-16 14:40:20 -07:00
committed by GitHub
parent 6cb16997d8
commit 1af4ea661f
4 changed files with 20 additions and 12 deletions

View File

@ -22,6 +22,7 @@
*/
#include "Arduino.h"
#include "user_interface.h"
#include "core_esp8266_waveform.h"
// Which pins have a tone running on them?
@ -35,10 +36,10 @@ static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long
pinMode(_pin, OUTPUT);
high = std::max(high, (uint32_t)25); // new 20KHz maximum tone frequency,
low = std::max(low, (uint32_t)25); // (25us high + 25us low period = 20KHz)
high = std::max(high, (uint32_t)microsecondsToClockCycles(25)); // new 20KHz maximum tone frequency,
low = std::max(low, (uint32_t)microsecondsToClockCycles(25)); // (25us high + 25us low period = 20KHz)
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
if (startWaveformCycles(_pin, high, low, microsecondsToClockCycles(duration * 1000))) {
_toneMap |= 1 << _pin;
}
}
@ -48,7 +49,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
if (frequency == 0) {
noTone(_pin);
} else {
uint32_t period = 1000000L / frequency;
uint32_t period = (1000000L * system_get_cpu_freq()) / frequency;
uint32_t high = period / 2;
uint32_t low = period - high;
_startTone(_pin, high, low, duration);
@ -62,7 +63,7 @@ void tone(uint8_t _pin, double frequency, unsigned long duration) {
if (frequency < 1.0) { // FP means no exact comparisons
noTone(_pin);
} else {
double period = 1000000.0 / frequency;
double period = (1000000.0L * system_get_cpu_freq()) / frequency;
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
uint32_t low = (uint32_t)(period + 0.5) - high;
_startTone(_pin, high, low, duration);