메뉴 건너뛰기

시니어월천

비즈79통합검색

쿠팡 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다. ( jp레코더 후원 까지 )

목록

esp8266 timer isr

2018.02.24 17:29

mynick 조회 수:892

ESP8266 Timer and Ticker Example

There are two timers in ESP8266 Timer0 and Timer1, one timer is used by its WiFi functions. We get only one timer to work. To avoid crash issues I recommend use of Ticker instead of Timer. Ticker performs same function as timer.

esp8266 timer

In this tutorial we will see both Timer and Ticker examples

ESP8266 Ticker Example

Ticker is library for calling functions repeatedly with a certain period. Ticker is os_timerEach Ticker calls one function. You can have as many Tickers as you like, memory being the only limitation.  A function may be attached to a ticker and detached from the ticker. There are two variants of the attach function: attach and attach_ms. The first one takes period in seconds, the second one in milliseconds.

LED Blinking using ESP8266 Ticker

This program demonstrates LED blinking ticker example. This function starts timers similar to attach interrupt blinker.attach(0.5, changeState); to stop timer use blinker.detach();

In this tutorial we will see both Timer and Ticker examples

ESP8266 Ticker Example

Ticker is library for calling functions repeatedly with a certain period. Ticker is os_timer Each Ticker calls one function. You can have as many Tickers as you like, memory being the only limitation.  A function may be attached to a ticker and detached from the ticker. There are two variants of the attach function: attach and attach_ms. The first one takes period in seconds, the second one in milliseconds.

 

LED Blinking using ESP8266 Ticker

 

This program demonstrates LED blinking ticker example. This function starts timers similar to attach interrupt blinker.attach(0.5, changeState); to stop timer use blinker.detach();

 

To use Ticker os_timer we need Ticker.h Timer Library

 

 

/*

    Ticker ESP8266

    Hardware: NodeMCU

    Circuits4you.com

    2018

    LED Blinking using Ticker

*/

#include <ESP8266WiFi.h>

#include <Ticker.h>  //Ticker Library

 

Ticker blinker;

 

#define LED 2  //On board LED

 

//=======================================================================

void changeState()

{

  digitalWrite(LED, !(digitalRead(LED)));  //Invert Current State of LED  

}

//=======================================================================

//                               Setup

//=======================================================================

void setup()

{

    Serial.begin(115200);

    Serial.println("");

 

    pinMode(LED,OUTPUT);

 

    //Initialize Ticker every 0.5s

    blinker.attach(0.5, changeState); //Use <strong>attach_ms</strong> if you need time in ms

}

//=======================================================================

//                MAIN LOOP

//=======================================================================

void loop()

{

}

//=======================================================================

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

/*

    Ticker ESP8266

    Hardware: NodeMCU

    Circuits4you.com

    2018

    LED Blinking using Ticker

*/

#include <ESP8266WiFi.h>

#include <Ticker.h>  //Ticker Library

 

Ticker blinker;

 

#define LED 2  //On board LED

 

//=======================================================================

void changeState()

{

  digitalWrite(LED, !(digitalRead(LED)));  //Invert Current State of LED  

}

//=======================================================================

//                               Setup

//=======================================================================

void setup()

{

    Serial.begin(115200);

    Serial.println("");

 

    pinMode(LED,OUTPUT);

 

    //Initialize Ticker every 0.5s

    blinker.attach(0.5, changeState); //Use <strong>attach_ms</strong> if you need time in ms

}

//=======================================================================

//                MAIN LOOP

//=======================================================================

void loop()

{

}

//=======================================================================

Upload the program and see LED starts blinking at every 0.5 seconds.

 

ESP8266 Timer Example

Hardware Timer0 is used by WiFi Functions. We can use only Timer1. Use of timer instead of Ticker gives advantage of precision timing and You can get timer interrupt in micro seconds.

 

 

/*

    ESP8266 Timer Example

    Hardware: NodeMCU

    Circuits4you.com

    2018

    LED Blinking using Timer

*/

#include <ESP8266WiFi.h>

#include <Ticker.h>

 

Ticker blinker;

 

#define LED 2  //On board LED

 

//=======================================================================

void ICACHE_RAM_ATTR onTimerISR(){

    digitalWrite(LED,!(digitalRead(LED)));  //Toggle LED Pin

    timer1_write(600000);//12us

}

//=======================================================================

//                               Setup

//=======================================================================

void setup()

{

    Serial.begin(115200);

    Serial.println("");

 

    pinMode(LED,OUTPUT);

 

    //Initialize Ticker every 0.5s

    timer1_attachInterrupt(onTimerISR);

    timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);

    timer1_write(600000); //120000 us

}

//=======================================================================

//                MAIN LOOP

//=======================================================================

void loop()

{

}

//=======================================================================

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

/*

    ESP8266 Timer Example

    Hardware: NodeMCU

    Circuits4you.com

    2018

    LED Blinking using Timer

*/

#include <ESP8266WiFi.h>

#include <Ticker.h>

 

Ticker blinker;

 

#define LED 2  //On board LED

 

//=======================================================================

void ICACHE_RAM_ATTR onTimerISR(){

    digitalWrite(LED,!(digitalRead(LED)));  //Toggle LED Pin

    timer1_write(600000);//12us

}

//=======================================================================

//                               Setup

//=======================================================================

void setup()

{

    Serial.begin(115200);

    Serial.println("");

 

    pinMode(LED,OUTPUT);

 

    //Initialize Ticker every 0.5s

    timer1_attachInterrupt(onTimerISR);

    timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);

    timer1_write(600000); //120000 us

}

//=======================================================================

//                MAIN LOOP

//=======================================================================

void loop()

{

}

//=======================================================================

Upload program and observe LED Blinking.

페이지 바로가기휴대폰 카메라앱 실행
번호 제목 글쓴이 날짜 조회 수
57 작심삼일비디오일기 쿠팡파트너스 이용 안내 [3] file mynick 2021.11.08 1880
56 청년의꿈 회원가입 예약받습니다. [44] file mynick 2021.11.12 894
» esp8266 timer isr mynick 2018.02.24 892
54 국민의힘 당원가입 안내 작비당원가입 [2] file mynick 2021.10.14 679
53 제이피팡 청년의꿈 홈페이지 바로가기 file mynick 2021.11.15 549
52 자동차 공기압 얼마로 설정해야 ? psi bar kpa k/gcm mynick 2021.10.12 508
51 당랑의꿈 읽어며 홍준표 응원합니다. file mynick 2021.10.16 484
50 후보교체 여론조사 [4] file mynick 2021.12.19 373
49 정규재 진흥법 107 촉진법 108 발전법 38 지원법 172 육성법 69 증진법 38 mynick 2021.11.24 308
48 홍준표정신 되살려야 mynick 2021.11.05 274
47 20220106 오늘 4 / 정규재 / 미국 창업 50% 늘어난다 [2] file mynick 2022.01.04 208
46 어린잎채소 microgreen mynick 2018.01.01 205
45 KBS라디오 최경영의최강시사 21.12.23 [1] mynick 2021.12.23 203
44 온 국민이 정치에 미친 잔머리 [3] mynick 2021.12.26 196
43 홍준표 살려낼 인물 중 가장 가능성 있어 보이는 사람 ? mynick 2021.11.21 181
42 20211206 mynick 2021.12.06 178
41 20211216 [2] mynick 2021.12.16 170
40 2021.11.23 작심이생각 mynick 2021.11.23 165
39 홍준표 탈락에 뿔난 2030 탈당 인증 노인의힘 떠난다 박사모 정광용 이언주출연 [1] file mynick 2021.11.06 163
38 이재명이 아닌 이준석과 싸우는 윤핵관 mynick 2022.01.03 159
위로