Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By soroosh
#70844 Hi
I use os_timer_arm_us to generate 4uS square wave with 50% duty cycle but that makes 4mS square wave!!!
What is my mistake?
Thank you
Code: Select all#define out_pin 5
volatile unsigned long timeSpent=1;
volatile int toggle;
unsigned long prevMillis=0;
unsigned long current_time=0;

extern "C" {
#define USE_US_TIMER
#include "user_interface.h"
#include "osapi.h"
#define os_timer_arm_us(a, b, c) ets_timer_arm_new(a, b, c, 0)
}

os_timer_t t0;


void myIsrTimer(void*z)
{
  current_time = micros();
  timeSpent = current_time - prevMillis;
  prevMillis =  current_time; 
  //toggle = (toggle == 1) ? 0 : 1;
  //digitalWrite(out_pin,toggle); 
}

void setup() {
  pinMode(out_pin, OUTPUT); 
  os_timer_setfn(&t0, myIsrTimer, NULL);
  os_timer_arm(&t0, 4, true);
  // Initialise Serial connection
  Serial.begin(115200);
 }

void loop() {
  Serial.print(timeSpent);Serial.print(" ");
}
User avatar
By Henning
#75433 Hello,
I have the same problem ... soroosh, did you fixed this problem meanwhile ?

Example:
Code: Select allos_timer_arm_us( &myTimer, 100, 1 );

This results in a 100ms Interval, not 100us.

Did anybody already used an us-timer successfully on the esp8266 ?

friendly regards
Henning
User avatar
By tele_player
#75454 I found this somewhere:


void os_timer_arm_us(os_timer_t *ptimer,
uint32_t microseconds,
bool repeat_flag)
os_timer_t *ptimer: timer structure.
uint32_t microseconds: timing; unit: microsecond, the minimum value is 0x64, the maximum value allowed to input is 0xFFFFFFF.
bool repeat_flag: whether the timer will be invoked repeatedly or not.


Apparently, this comes from Espressif SDK documentation.

So, I would try much larger values, just to see if it works, such as 1000, 10000.
User avatar
By RuiViana
#80418 Hi, Soroosh and Henning,
I had the same problem, and I solved
calling the function "system_timer_reinit ();" at the
start of my "user_init".
Like this:

extern "C" {
#define USE_US_TIMER
#include "user_interface.h"
#include "osapi.h"
#define os_timer_arm_us (a, b, c) ets_timer_arm_new (a, b, c, 0)
}
os_timer_t mTimer;
// ------------------------------------------------ -------------------
void usrInit (void) {
     system_timer_reinit ();
     os_timer_setfn (& mTimer, tCallback, NULL);
     os_timer_arm_us (& mTimer, 1000, true);
}

RV