- 타이머 설정하기
타이머 인터럽트를 사용하기 위해서는 타이머를 먼저 설정해야 합니다. 아래 코드는 1초마다 타이머 인터럽트를 발생시키는 타이머를 설정하는 코드입니다.
You must set the timer before you can use the timer interrupt.
The code below sets a timer that generates a timer interrupt every second.
hw_timer_t * timer = NULL;
void setup() {
timer = timerBegin(0, 80, true); // 타이머 초기화, 주파수는 80MHz, 분주비는 1
timerAttachInterrupt(timer, &onTimerISR, true); // 타이머 인터럽트 함수를 등록
timerAlarmWrite(timer, 1000000, true); // 1초마다 타이머 인터럽트 발생
timerAlarmEnable(timer); // 타이머 시작
}
void loop() {
// 아무것도 하지 않음
}
void onTimerISR() {
// 타이머 인터럽트가 발생했을 때 수행할 작업
}
- 타이머 인터럽트 함수 작성하기
타이머 인터럽트 함수를 작성해야 합니다. 이 함수는 타이머 인터럽트가 발생했을 때 호출됩니다. 아래 코드는 타이머 인터럽트가 발생하면 LED를 깜빡이는 함수를 작성하는 코드입니다.
You must create a timer interrupt function.
This function is invoked when a timer interrupt occurs.
The code below is to write a function that blinks the LED when a timer interrupt occurs.
hw_timer_t * timer = NULL;
int ledPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
timer = timerBegin(0, 80, true); // 타이머 초기화, 주파수는 80MHz, 분주비는 1
timerAttachInterrupt(timer, &onTimerISR, true); // 타이머 인터럽트 함수를 등록
timerAlarmWrite(timer, 1000000, true); // 1초마다 타이머 인터럽트 발생
timerAlarmEnable(timer); // 타이머 시작
}
void loop() {
// 아무것도 하지 않음
}
void onTimerISR() {
digitalWrite(ledPin, !digitalRead(ledPin)); // LED를 깜빡임
}
- 타이머 인터럽트 활성화하기
타이머 인터럽트를 활성화해야 합니다. 이를 위해 timerAlarmEnable() 함수를 사용합니다.
Timer interrupt must be enabled.
To do this, we use the timerAlarmEnable() function.
timerAlarmEnable(timer); // 타이머 시작
- 타이머 인터럽트 비활성화하기
타이머 인터럽트를 비활성화하려면 timerAlarmDisable() 함수를 사용합니다.
Use the timerAlarmDisable() function to disable timer interrupt.
timerAlarmDisable(timer); // 타이머 중지
ESP32 아두이노에서 타이머 인터럽트를 사용하기 위해서는 타이머를 설정하고 인터럽트 함수를 작성한 후, timerAttachInterrupt()로 등록하고 timerAlarmEnable()로 활성화해야 합니다.
To use timer interrupt on ESP32 Arduino, you must set the timer, create an interrupt function, register it as timerAttachInterrupt(), and enable it as timerAlarmEnable().
timerBegin() 함수는 ESP32의 타이머를 초기화합니다. 함수의 원형은 다음과 같습니다.
The timerBegin() function initializes the timer for ESP32.
The prototype of the function is as follows.
hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp);
- num: 사용할 타이머의 번호 (0 ~ 3), Number of timers to use (0 to 3)
- divider: 타이머의 분주비 (1 ~ 65535), Timer division ratio (1 to 65535)
- countUp: 타이머를 업 카운트 모드로 설정할지 여부, Whether to set the timer to up count mode
함수는 초기화된 타이머에 대한 'hw_timer_t' 구조체 포인터를 반환합니다. 이 구조체는 나중에 타이머 설정이나 제어에 사용됩니다.
예를 들어, 다음 코드는 'timerBegin()' 함수를 사용하여 0번 타이머를 80MHz 주파수로 초기화합니다.
The function returns a 'hw_timer_t' structure pointer to the initialized timer.
This structure is later used to set or control timers.
For example, the following code initializes timer 0 to 80 MHz frequency using the 'timerBegin()' function:
hw_timer_t * timer = NULL;
void setup() {
timer = timerBegin(0, 80, true);
}
'ESP32' 카테고리의 다른 글
[강좌]ESP32 4.3" TFT-LCD HMI - 4. TFT 예제 살펴보기(Explore Examples) (7) | 2023.03.27 |
---|---|
[강좌]ESP32 4.3" TFT-LCD HMI - 3. 데모 프로그램 라이팅(Demo binary writing) (0) | 2023.03.23 |
[강좌]ESP32 4.3" TFT-LCD HMI - 2. 제품 상세소개 및 관련 자료 설명(Detailed introduction & Materials introduction) (0) | 2023.03.21 |
[강좌]ESP32 4.3" TFT-LCD HMI - 1. 제품 소개 (product descriptions) (0) | 2023.03.21 |
ESP-IDF 와 VSCODE를 이용한 개발 환경 꾸미기. (0) | 2021.01.18 |
댓글