메뉴 건너뛰기

시니어월천

비즈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 어린잎채소 microgreen mynick 2018.01.01 205
56 youtube의 microgreens 영상을 보고 mynick 2018.01.03 156
» esp8266 timer isr mynick 2018.02.24 892
54 산양삼 10년 키우면 몇그램 ? 씨앗에서 10년까지 년도별 비교 동영상 작심삼일비디오일기 mynick 2020.07.15 20
53 가성비 이동성 최고 스폿용접기 Portable spot welding machine diy [ 작심삼일비디오일기 ] 18650 배터리 file mynick 2020.07.15 25
52 작심삼일비디오일기 호두나무 삽목 귀농귀촌 잡담 골프카트가 이렇게 mynick 2020.07.17 35
51 페이스북 실시간 그룹 방송 obs 활용 더 화려하게 방송하기 (기초) mynick 2020.07.19 17
50 산삼의 역사 산양삼 장뇌삼 장뇌산삼 산양산삼 mynick 2020.07.22 15
49 피부관리 화이트닝 미백 브라이트닝 mynick 2020.08.03 24
48 경제수장이 이럴수가 mynick 2020.11.03 23
47 홍준표 인간성 file mynick 2021.09.24 12
46 자동차 공기압 얼마로 설정해야 ? psi bar kpa k/gcm mynick 2021.10.12 508
45 국민의힘 당원가입 안내 작비당원가입 [2] file mynick 2021.10.14 679
44 당랑의꿈 읽어며 홍준표 응원합니다. file mynick 2021.10.16 484
43 안상수 펜엔드마이크에서 입을열다 mynick 2021.10.20 5
42 홍준표에 겁먹은 조중동 / 그는 지금 두개의 적과 싸우고있다 mynick 2021.10.24 12
41 강용석이 말하는 윤석열 mynick 2021.10.25 10
40 한국의 핵무장 주장한 美 다트머스대 제니퍼 린드, 대릴 프레스 부부 교수Daryl Press file mynick 2021.10.25 17
39 주호영, 홍준표를 몰라도 너무 모른다 mynick 2021.10.26 16
38 썩은 세끼줄 마지막 까지 놓지 못하는 진성호 안타깝다 mynick 2021.10.26 4
위로