- Sat Jun 24, 2017 7:45 am
#67529
extern "C" {
#define USE_US_TIMER 1
#include "user_interface.h"
}
os_timer_t myTimer;
bool tickOccured;
// start of timerCallback
void timerCallback(void *pArg) {
tickOccured = true;
} // End of timerCallback
void user_init(void) {
/*
os_timer_setfn - Define a function to be called when the timer fires
void os_timer_setfn(
os_timer_t *pTimer,
os_timer_func_t *pFunction,
void *pArg)
Define the callback function that will be called when the timer reaches zero. The pTimer parameters is a pointer to the timer control structure.
The pFunction parameters is a pointer to the callback function.
The pArg parameter is a value that will be passed into the called back function. The callback function should have the signature:
void (*functionName)(void *pArg)
The pArg parameter is the value registered with the callback function.
*/
os_timer_setfn(&myTimer, timerCallback, NULL);
/*
os_timer_arm - Enable a millisecond granularity timer.
void os_timer_arm(
os_timer_t *pTimer,
uint32_t milliseconds,
bool repeat)
Arm a timer such that is starts ticking and fires when the clock reaches zero.
The pTimer parameter is a pointed to a timer control structure.
The milliseconds parameter is the duration of the timer measured in milliseconds. The repeat parameter is whether or not the timer will restart once it has reached zero.
*/
// os_timer_arm(&myTimer, 1000, true); // milisegundos
ets_timer_arm_new(&myTimer, 1000, true,0); // microsegundos
} // End of user_init
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println("");
Serial.println("--------------------------");
Serial.println("ESP8266 Timer Test");
Serial.println("--------------------------");
tickOccured = false;
user_init();
system_timer_reinit ();
}
void loop() {
if (tickOccured == true)
{
Serial.println("Tick Occurred");
tickOccured = false;
}
yield(); // or delay(0);
}
ependent from setting the 4th param in ets_timer_arm() to 0 or 1, timeout is always 1000ms and not as expected 1000 µs.
Code: Select all#include "user_interface.h"
#include "osapi.h"
#include "ets_sys.h"
ETSTimer MyHwTimer;
// ------------------------------
void MyTimerFunc(void* pTimerArg)
// ------------------------------
{
static bool bOn = false;
bOn = !bOn;
digitalWrite(14, bOn ? 1 : 0);
}
// --------------------------------------------
void setup()
// --------------------------------------------
{
system_timer_reinit();
pinMode(14, OUTPUT); // configure GPIO14 as output
digitalWrite(14, 0); // LED on
ets_timer_setfn(&MyHwTimer, MyTimerFunc, NULL);
ets_timer_arm_new(&MyHwTimer, 1000, 1, 0); // this results in 1000 ms
//ets_timer_arm_new(&MyHwTimer, 1000, 1, 1); // and this is also 1000 ms
}
// --------------------------------------------
void loop()
// --------------------------------------------
{
}
Any idea?
Thanks in advance
- StevieBread -[/quote]