Created
November 16, 2018 10:07
-
-
Save pral2a/11a6bf7e5aaf2d020616687710a31635 to your computer and use it in GitHub Desktop.
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
/* | |
ESP8266 example for tone via timer1 | |
Hardware: NodeMCU | |
2018 | |
tone using Timer | |
*/ | |
//define ITONE_CYCLE 2500000 // 1 second cycle in 1/16 CPU clock (1/5 µs) for 80 MHz | |
#define ITONE_CYCLE 5000000 // 1 second cycle in 1/16 CPU clock (1/5 µs) for 160 MHz | |
//======================================================================= | |
// itone, noItone | |
//======================================================================= | |
byte _itonepin = D0; | |
byte _itoneval = HIGH; | |
void ICACHE_RAM_ATTR _onItoneTimerISR(){ | |
_itoneval ^= 1; | |
digitalWrite(_itonepin,_itoneval); //Toggle LED Pin | |
} | |
void itone(byte pin,unsigned long frequency) { | |
timer1_detachInterrupt(); | |
timer1_attachInterrupt(_onItoneTimerISR); | |
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP); | |
_itoneval = HIGH; | |
_itonepin = pin; | |
pinMode(pin,OUTPUT); | |
digitalWrite(pin,HIGH); | |
timer1_write(ITONE_CYCLE/frequency); | |
} | |
void noItone() { | |
timer1_detachInterrupt(); | |
} | |
//======================================================================= | |
// Setup | |
// we test the serial connection during itone running | |
// result: it still works | |
//======================================================================= | |
void setup() | |
{ | |
Serial.begin(115200); | |
itone(D0,2000); // LED blinks with 1 Hz | |
delay(3000); | |
noItone(); | |
} | |
//======================================================================= | |
// MAIN LOOP | |
//======================================================================= | |
void loop() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment