메뉴 건너뛰기

시니어월천

비즈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 20220106 오늘 4 / 정규재 / 미국 창업 50% 늘어난다 [2] file mynick 2022.01.04 208
56 홍준표 이준석 이면합의 이미 끝난듯 보입니다. file mynick 2022.01.03 138
55 이재명이 아닌 이준석과 싸우는 윤핵관 mynick 2022.01.03 159
54 허경영은 어떠십니까 mynick 2022.01.02 149
53 온 국민이 정치에 미친 잔머리 [3] mynick 2021.12.26 196
52 KBS라디오 최경영의최강시사 21.12.23 [1] mynick 2021.12.23 203
51 후보교체 여론조사 [4] file mynick 2021.12.19 373
50 20211216 [2] mynick 2021.12.16 170
49 윤석열 병역면제 시력 0.8 0.1 부동시 오신환 의원 질의 [2] mynick 2021.12.13 148
48 장제원 윤석열 청문희 / 이준석 인터뷰 [1] file mynick 2021.12.13 144
47 20211206 mynick 2021.12.06 178
46 개인이라 불리우는 기적 file mynick 2021.12.05 110
45 붉은색 후드티 이준석 file mynick 2021.12.05 135
44 실시간 방송 프로그램 개발 mynick 2021.11.27 98
43 [류석춘 칼럼] 김병헌의 '빨간 수요일'… 그리고 '발전사회학' file mynick 2021.11.26 93
42 정규재 진흥법 107 촉진법 108 발전법 38 지원법 172 육성법 69 증진법 38 mynick 2021.11.24 308
41 2021.11.23 작심이생각 mynick 2021.11.23 165
40 홍준표 살려낼 인물 중 가장 가능성 있어 보이는 사람 ? mynick 2021.11.21 181
39 제이피팡 청년의꿈 홈페이지 바로가기 file mynick 2021.11.15 549
38 청년의꿈 회원가입 예약받습니다. [44] file mynick 2021.11.12 894
위로