
ESP-0806
قابل استفاده برای تمامی مدلهای ESP-08
در این آموزش با نحوه استفاده از دستور espGetCurrentTime برای استفاده در کنترل روند برنامه در پی ال سی ایرانی ESP-0806 آشنا می شوید.
espGetCurrentTime زمان فعلی سیستم را از زمان شروع سیستم به میلی ثانیه برمیگرداند.
ساختار کلی دستور
تعریف دستور espGetCurrentTime
uint32_t espGetCurrentTime(void);
مقدار بازگشتی
uint32_t: زمان فعلی به میلیثانیه (از شروع سیستم)
روشهای استفاده
1- دریافت زمان فعلی
// دریافت زمان فعلی uint32_t currentTime = espGetCurrentTime(); // استفاده مستقیم if (espGetCurrentTime() > 10000) { // بیش از 10 ثانیه از شروع سیستم گذشته }
2- ذخیره زمان شروع
// ذخیره زمان شروع عملیات
uint32_t startTime = espGetCurrentTime();
// استفاده بعدی
uint32_t elapsedTime = espGetCurrentTime() - startTime;
مثالهای کاربردی
مثال 1- اندازه گیری زمان عملیات
// اندازهگیری زمان اجرای عملیات
void measureOperationTime() {
uint32_t startTime = espGetCurrentTime();
// انجام عملیات
SetQ(0, 0);
Delay(1000);
ResetQ(0, 0);
uint32_t endTime = espGetCurrentTime();
uint32_t operationTime = endTime - startTime;
// operationTime زمان کل عملیات به میلیثانیه است
}
مثال2- کنترل زمان بندی
// کنترل عملیات هر 5 ثانیه
void timeBasedControl() {
static uint32_t lastOperationTime = 0;
uint32_t currentTime = espGetCurrentTime();
if (currentTime - lastOperationTime >= 5000) { // هر 5 ثانیه
// انجام عملیات
SetQ(0, 0);
Delay(100);
ResetQ(0, 0);
lastOperationTime = currentTime;
}
}
مثال3- کنترل با تاخیر متغیر
// کنترل با تاخیر بر اساس زمان
void adaptiveDelay() {
uint32_t startTime = espGetCurrentTime();
// تاخیر بر اساس زمان روز
uint32_t timeOfDay = startTime % 86400000; // میلیثانیه در روز
if (timeOfDay < 43200000) { // قبل از ظهر
Delay(100); // تاخیر کوتاه
} else {
espDelay(1000); // تاخیر طولانی
}
}
مثال4- کنترل توالی زمانی
// کنترل توالی با زمانبندی دقیق
void timedSequence() {
uint32_t sequenceStart = espGetCurrentTime();
// مرحله 1: 0 تا 2 ثانیه
if (espGetCurrentTime() - sequenceStart < 2000) {
SetQ(0, 0);
}
// مرحله 2: 2 تا 4 ثانیه
else if (espGetCurrentTime() - sequenceStart < 4000) {
ResetQ(0, 0);
SetQ(0, 1);
}
// مرحله 3: 4 تا 6 ثانیه
else if (espGetCurrentTime() - sequenceStart < 6000) {
ResetQ(0, 1);
SetQ(0, 2);
}
// پایان توالی
else {
ResetQ(0, 2);
}
}
مثال5- کنترل با حلقه زمانی
// کنترل با حلقه زمانی
void timeLoopControl() {
static uint32_t lastTime = 0;
uint32_t currentTime = espGetCurrentTime();
// عملیات هر 1 ثانیه
if (currentTime - lastTime >= 1000) {
SetQ(0, 0);
Delay(100);
ResetQ(0, 0);
lastTime = currentTime;
}
}
مثال6- کنترل با چندین تایمر
// کنترل با چندین تایمر همزمان
void multiTimerControl() {
static uint32_t timer1 = 0;
static uint32_t timer2 = 0;
static uint32_t timer3 = 0;
uint32_t currentTime = espGetCurrentTime();
// تایمر 1: هر 1 ثانیه
if (currentTime - timer1 >= 1000) {
SetQ(0, 0);
timer1 = currentTime;
}
// تایمر 2: هر 2 ثانیه
if (currentTime - timer2 >= 2000) {
SetQ(0, 1);
timer2 = currentTime;
}
// تایمر 3: هر 5 ثانیه
if (currentTime - timer3 >= 5000) {
SetQ(0, 2);
timer3 = currentTime;
}
}
مثال7 - کنترل با زمان بندی نسبی
// کنترل با زمانبندی نسبی
void relativeTiming() {
static uint32_t baseTime = 0;
uint32_t currentTime = espGetCurrentTime();
if (baseTime == 0) {
baseTime = currentTime;
}
uint32_t relativeTime = currentTime - baseTime;
// عملیات در زمانهای نسبی
if (relativeTime % 1000 < 100) { // 100 میلیثانیه اول هر ثانیه
SetQ(0, 0);
} else {
ResetQ(0, 0);
}
}
مثال8- بررسی گذشت زمان
// بررسی گذشت زمان از نقطه شروع
bool espTimeElapsed(uint32_t startTime, uint32_t duration) {
return (espGetCurrentTime() - startTime) >= duration;
}
// استفاده
uint32_t startTime = espGetCurrentTime();
if (espTimeElapsed(startTime, 5000)) {
// 5 ثانیه گذشته
}
مثال 9- تابع تاخیر با بررسی زمان
// تاخیر با بررسی زمان
void espDelayUntil(uint32_t targetTime) {
while (espGetCurrentTime() < targetTime) {
// انتظار تا رسیدن به زمان هدف
}
}
// استفاده
uint32_t targetTime = espGetCurrentTime() + 5000; // 5 ثانیه بعد
espDelayUntil(targetTime);
مدیریت ایمنی و خطا
بررسی سرریز زمان
// بررسی سرریز زمان (هر 49.7 روز)
bool espTimeOverflow(uint32_t oldTime, uint32_t newTime) {
return newTime < oldTime; // سرریز رخ داده
}
// استفاده ایمن
uint32_t startTime = espGetCurrentTime();
// ... عملیات ...
uint32_t currentTime = espGetCurrentTime();
if (espTimeOverflow(startTime, currentTime)) {
// سرریز رخ داده - محاسبه متفاوت
uint32_t elapsedTime = (0xFFFFFFFF - startTime) + currentTime;
} else {
// محاسبه عادی
uint32_t elapsedTime = currentTime - startTime;
}
تابع تاخیر ایمن
// تاخیر ایمن با بررسی شرایط
void espSafeDelay(uint16_t delayTime) {
uint32_t startTime = espGetCurrentTime();
while ((espGetCurrentTime() - startTime) < delayTime) {
// بررسی شرایط ایمنی
if (emergencyStopCondition()) {
return; // خروج در صورت توقف اضطراری
}
}
}
نکات مهم:
دقت: زمانسنجی بر اساس تایمر سیستم است
سرریز: هر 49.7 روز یک بار سرریز رخ میدهد
محدوده: حداکثر مقدار 4,294,967,295 میلیثانیه
کارایی: دسترسی سریع به زمان سیستم
ایمنی: همیشه سرریز را در محاسبات طولانی بررسی کنید