Using Arduino sketch with esp8266

hi all

i have this Arduino working code this part of lt what i need is how to use this code with esp8266 note that on arduino i can use also digital pins not only A0 A1 and A2

[code]
#include <TimerOne.h>
volatile int CYCLE_TIME    =1562;
volatile int FREQUENCY     = 640;
volatile int TX_PULSE      = 100;
volatile int PULSE_1_DELAY =  20;
volatile int PULSE_1       =  45;
volatile int PULSE_2_DELAY = 150;
volatile int PULSE_2       =  45;

volatile unsigned long timer = 0;
void setup()
{
  pinMode (A0, OUTPUT);
  pinMode (A1, OUTPUT);  
  pinMode (A2, OUTPUT);
  digitalWrite (A0, LOW);
  digitalWrite (A1, LOW);
  digitalWrite (A2, LOW);

  
  Serial.begin(9600);

  Timer1.initialize(CYCLE_TIME);
  Timer1.attachInterrupt(do_isr);
}

void loop()
{
  
void do_isr()
{
  PINC = 0x01;
  for(int i = 0; i < TX_PULSE; i++)
  {
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\t");
  }
  PINC = 0x01;
  
  for(int i = 0; i < PULSE_1_DELAY; i++)
  {
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\t");
  }
  
  PINC = 0x02;
  for(int i = 0; i < PULSE_1; i++)
  {
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\t");
  }  
  PINC = 0x02;
  
  for(int i = 0; i < PULSE_2_DELAY; i++)
  {
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\t");
  }
  
  PINC = 0x04;
  for(int i = 0; i < PULSE_2; i++)
  {
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
    __asm__("nop\n\tnop\n\t");
  }  
  PINC = 0x04;   
}

[/code]

thanks in advance

Assembly code for, say, an Arduino Uno, is extremely unlikely to compile or work with an ESP8266. But it codes only for very short delays.

1 Like

hi
this is github example what i need to know in my first post is how to use ESP8266 port like arduino PINC = 0x01; PINC = 0x02; PINC = 0x04;

void IRAM_ATTR ws2812b_bit_0(){
  gpio_set_level_fast(WS2812B, 1);

  //delay 0.4us
  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop;");
  gpio_set_level_fast(WS2812B, 0);

  //delay 0.85us
  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop;");
}

void IRAM_ATTR ws2812b_bit_1(){
  gpio_set_level_fast(WS2812B, 1);

  //delay 0.8us
  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop; nop; nop;"
    "nop; nop; nop; nop; nop; nop;");

  gpio_set_level_fast(WS2812B, 0);

  //delay 0.45us
  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop;");
}

Google "fast port i/o esp8266"

1 Like

The ESP8266 doesn't have a "Timer1" like the AVR processors. It looks like you are trying to write to a WS2812B addressable LED strip. I would look for a WS2812B library that works on the ESP8266 and let it do the work, like GitHub - adafruit/Adafruit_NeoPixel: Arduino library for controlling single-wire LED pixels (NeoPixel, WS2812, etc.) or GitHub - kitesurfer1404/WS2812FX: WS2812 FX Library for Arduino and ESP8266.

1 Like

hi sir and thank you

iam not trying to write to a WS2812B addressable LEDstrip

i post the github link " post 3 " just show @jremington that it is possible to use asm("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t"); with ESP8266

what i need is to use the code in my first post with ESP8266

may this library replace timer one or not ????

Right on the front page it says:

Advice: for use with ESP boards and mbed based Arduino boards like Arduino Nano RP2040 Connect and Raspberry Pi Pico (using the official Arduino core) the TickTwo library GitHub - sstaub/TickTwo: Ticker library for ESP and mbed based Arduino boards is recommanded avoiding name conflicts.

Perhaps you should heed that advice and try TickTwo instead.

In addition, your sketch uses the special feature of the AVR processor that toggles a bit in the PORT register when a 1 is written to the corresponding bit in the PIN register. You will need an ESP8266 way to quickly toggle an output pin.

1 Like

Thank you for your kind response

The code in the first post is only part

Is there a way to rewrite the code and get the same result with 8266 ?

You can almost certainly change output pins in the same pattern but the timing is likely to be (very) different. I have no idea if the ESP8266 can digitalRead() an OUTPUT pin. If it can then:
PINC = 0x01; // Toggle A0 output
could be changed to:
digitalWrite(A0, !digitalRead(A0));
(Where 'A0' resolves to an output pin number.)

If it can't, you'll have to keep a state variable for each output pin:

  A0State = !A0State;
  digitalWrite(A0, A0State);
1 Like

To give you an easy to understand analogon:

Trying to port assembler-code from a microcontroller of type Atmel328 (Arduino) to a microcontroller type Xtensa LX106 (ESP8266) is like trying to fasten such a screw
grafik

with this tool
grafik

Assembler-code is below the level of C++-coding.

Each microcontroller has its own "core" which is used to translate "high-level" commands like digitalWrite() if() etc. into that kind of assembler-code that suits to that exact type of microcontroller.

So a direct port of assembler-code is simply impossible.

You have just ask for a detail. What is the final purpose of your project?
Creating nanosecond short pulses is not a self-purpose.

If you describe what you want to do in the end the final purpose
well suited suggestions can be made.

best regards Stefan

1 Like

Beautiful and expressive example, thank you

Creating nanosecond short pulses is not a self-purpose. Yes sure

Using Arduino gives microsecond delay

It fits what I did and what i am using it for it is timing clock for pulsed metal detector. i am Doing these things for fun and entertainment. The rest of the code is to control variables such as frequency, delay, etc.

A0 Generates a signal with a variable frequency in the kilohertz range and a variable pulse width from one hundred and fifty microseconds to four hundred microseconds

A1 generates a delay a hundred microseconds

A2 generates a delay ten microseconds

Here is a picture of A0 output

the code in the first post working fine for the metal detector

what i am thinking now is to use same manner of the arduino uno sketch with ESP8266 to get sub nanosecond delay i want to play with rangefinders or time domain reflectometry motion sensor etc

how to make this using ESP8266 with arduino IDE ؟

I am not restricted to the code above it is just an idea how to make a nanosecond delay using ESP8266 as arduino did in microsecond

** Any help from everyone is appreciated **

hi
Can you explain more how it can be used TickTwo instead ؟

Timer1.initialize(CYCLE_TIME);
  Timer1.attachInterrupt(do_isr);

i google it some pins can yes

I think it is something like:

TickTwo Timer1(do_isr, CYCLE_TIME, 0);

void setup()
{
  Timer1.start();
}

void loop()
{
  Timer1.update();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.