void vTimerCallback(TimerHandle_t xTimer) {
printf("Timer expired\n");
}
int main() {
TimerHandle_t xTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, vTimerCallback);
if (xTimer == NULL) {
printf("Timer creation failed\n");
} else {
xTimerStart(xTimer, 0);
vTaskDelay(pdMS_TO_TICKS(1500));
}
return 0;
}The timer is created as a one-shot (pdFALSE). It fires once after 1000 ms. The delay of 1500 ms allows the timer to expire and call the callback, printing "Timer expired" once.
void vTimerCallback(TimerHandle_t xTimer) {
printf("Timer expired\n");
}
int main() {
TimerHandle_t xTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, vTimerCallback);
if (xTimer == NULL) {
printf("Timer creation failed\n");
} else {
xTimerStart(xTimer, 0);
xTimerStop(xTimer, 0);
vTaskDelay(pdMS_TO_TICKS(1500));
}
return 0;
}The timer is started but immediately stopped before it can expire. Therefore, the callback is never called and no output is printed.
void vTimerCallback(TimerHandle_t xTimer) {
printf("Timer expired\n");
}
int main() {
TimerHandle_t xTimer = xTimerCreate("Periodic", pdMS_TO_TICKS(500), pdTRUE, NULL, vTimerCallback);
if (xTimer == NULL) {
printf("Timer creation failed\n");
} else {
xTimerStart(xTimer, 0);
vTaskDelay(pdMS_TO_TICKS(600));
xTimerStop(xTimer, 0);
xTimerStart(xTimer, 0);
vTaskDelay(pdMS_TO_TICKS(600));
}
return 0;
}The periodic timer fires every 500 ms. It fires once before stopping, then after restarting it fires again. So the callback prints twice.
Option D is missing a comma between pdFALSE and NULL, causing a syntax error.
void vTimerCallback(TimerHandle_t xTimer) {
static int count = 0;
count++;
printf("Timer expired %d\n", count);
if (count == 3) {
xTimerStop(xTimer, 0);
}
}
int main() {
TimerHandle_t xTimer = xTimerCreate("Periodic", pdMS_TO_TICKS(400), pdTRUE, NULL, vTimerCallback);
if (xTimer == NULL) {
printf("Timer creation failed\n");
} else {
xTimerStart(xTimer, 0);
vTaskDelay(pdMS_TO_TICKS(2000));
}
return 0;
}The periodic timer fires every 400 ms. The callback increments count and stops the timer when count reaches 3. The delay of 2000 ms is enough for 5 periods, but the timer stops after 3 callbacks, so it prints 3 times.
