Giỏ hàng
Danh mục sản phẩm

Lập Trình Ngắt Timer ESP32 ,Arduino

Đăng bởi Trần Văn Bùi ngày bình luận

Hôm nay mình giới thiệu qua bài viết Ngắt timer 
Các ứng dụng Ngắt timer như quét led 7 đoạn
Board Mạch ESP32 HDSHOP Phát triển :

IO2 Chính chân LED Nháy 

Chú ý trong quá trình nạp Rút jumper WATCH DOG TIMER ngoài để nạp ESP32 Tránh Reset liên tục 

Các bạn nối LED với chân IO ESP32 

Chương trình chạy như sau:

hw_timer_t * timer = NULL;

/* LED pin */
int led = 2;
/* LED state */
volatile byte state = LOW;

void IRAM_ATTR onTimer(){
  state = !state;
  digitalWrite(led, state);
}

void setup() {
  Serial.begin(115200);

  pinMode(led, OUTPUT);

  /* Use 1st timer of 4 */
  /* 1 tick take 1/(80MHZ/80) = 1us so we set divider 80 and count up */
  timer = timerBegin(0, 80, true);

  /* Attach onTimer function to our timer */
  timerAttachInterrupt(timer, &onTimer, true);

  /* Set alarm to call onTimer function every second 1 tick is 1us
  => 1 second is 1000000us */
  /* Repeat the alarm (third parameter) */
  timerAlarmWrite(timer, 1000000, true);

  /* Start an alarm */
  timerAlarmEnable(timer);
  Serial.println("start timer");
}

void loop() {

}
Mình giải thích qua code như sau :
"hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp)"
Num:Timer  0,1,2,3
divider:Nó là tần số chia timer 
Để lập lịch 1S chúng ta sử dụng bộ chia 80,Tần số chính timer 80MHZ T=1/(80M/80)=1us=1000000
timer = timerBegin(0, 80, true);
Timer 0, Tần số chia 80
 timerAlarmWrite(timer, 1000000, true);
Tạo 1s là 1000000us để cảnh báo 
 timerAlarmEnable(timer);
Bắt đầu Timer chạy 

 


← Cũ hơn Mới hơn →