Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By StevieBread
#45812 Hello all,
I try to setup a microsecond timer, but I can't get it work.
Independent 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 -
User avatar
By gellpro
#48469 Hi, StevieBread.

When I wrote system_timer_reinit(void) as the attached document says on p14, perhaps ets_timer_arm_new worked at microseconds.
However, it's useless because of its inaccuracy.
Have you resolved this problem yet?
I want to know how to call a function periodically at microseconds.

Best regards
gellpro
You do not have the required permissions to view the files attached to this post.
User avatar
By TJF
#56231 Hi Stevie
did you find a solution to this?
In an Arduino Sketch I am looking for a µS one-shot timer function that I could arm in an ISR. Eg. 350 µS after an external Interrupt occured. Use of delayMicroseconds(350) in an ISR is probably not a brillant idea, especially because there is a webserver running in parallel and some UDP communication going on. So an alternative to delayMicroseconds is what I am looking for.
Any new information on this?
Best regards
Thomas
User avatar
By Dakota
#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]