TH4.3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

TH4.

3
#include <Arduino_FreeRTOS.h>
// Kích thước stack của mỗi task
#define STACK_SIZE 250

// Ưu tiên của các task


#define TASK_PRIORITY (tskIDLE_PRIORITY + 1)

// Handle của các task


TaskHandle_t Task1Handle, Task2Handle;

// Task1: In ra "Hello, world!" 5 lần


void Task1(void *pvParameters) {
UBaseType_t priority = uxTaskPriorityGet(NULL);
Serial.print("Task1 priority: ");
Serial.println(priority);

for(;;) {
for (int i = 0; i <= 5; i++) {
Serial.println("Hello, world!");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
// Notify Task2 to run
xTaskNotifyGive(Task2Handle);
// Wait for Task2 to suspend this task
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
}
}

// Task2: In ra "Bye Task1" và đình chỉ Task1 trong 10s


void Task2(void *pvParameters) {
UBaseType_t priority = uxTaskPriorityGet(NULL);
Serial.print("Task2 priority: ");
Serial.println(priority);
for(;;) {
// Wait for Task1 to notify
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
Serial.println("Bye Task1");
// Suspend Task1
vTaskSuspend(Task1Handle);
// Delay for 10 seconds
vTaskDelay(5000 / portTICK_PERIOD_MS);
// Resume Task1
vTaskResume(Task1Handle);
// Notify Task1 to continue
xTaskNotifyGive(Task1Handle);
}
}

void setup() {
// Khởi tạo cổng Serial để in ra màn hình
Serial.begin(9600);

// Tạo Task1
xTaskCreate(Task1, "Task1", STACK_SIZE, NULL, TASK_PRIORITY,
&Task1Handle);

// Tạo Task2
xTaskCreate(Task2, "Task2", STACK_SIZE, NULL, TASK_PRIORITY,
&Task2Handle);

// Khởi động scheduler


vTaskStartScheduler();
}

void loop() {
}

You might also like