رفتن به نوشته‌ها

تابع millis در آردینو

unsigned long time;

time = millis();

زمان را بر حسب میلی ثانیه بر میگرداند و حدودا هر ۵۰ ( ۴۹.۷۱ ) روز سر ریز می شود. به عبارت دقیق تر  هر  ۲ به توان ۳۲ میلی ثانیه سر ریز می شود:

۲^۳۲ = ۴۲۹۴۹۶۷۲۹۶  [ms]  -> 1- 4294967296

=>  ۰-۴۲۹۴۹۶۷۲۹۵ [ms]

*تابع millis مقدار بازگشتی را درون متغییر time که از نوع unsigned long هست میریزد. چنانچه time از نوع int تعریف شود با خطا مواجه می شوید.

Clock Cycles

First, some background information. The typical Arduino has a 16MHz oscillator. A 16MHz oscillator results in the microcontroller having a clock cycle once every 16-millionth of a second. A clock cycle is roughly the time it takes for one instruction cycle (there are exceptions). Therefore, on the Arduino, each clock cycle is 1/16,000,000 of a second, which is:

  • 0.0000000625 seconds
  • 0.0000625 milliseconds (ms)
  • 0.0625 microseconds (µs)

Prescaler

The Atmel ATmega168/328 based Arduino has 3 timers, of which the millis function uses the microcontroller’s Timer #0. Before utilizing any timer, several registers must be set. If we examine how the Arduino sets up Timer #0, we find it sets a prescale factor of 64 (from the wiring.c file):

1
2
3
// this combination is for the standard 168/328/1280/2560
sbi(TCCR0B, CS01);
sbi(TCCR0B, CS00);

ATMEL ATMega328 Datasheet page 111 specifies the Timer #0 prescale bits:
ATMega328 Datasheet Detail

While technically a prescaler divides the timer, I like to think of it as a multiplier. For example, with an Arduino clock cycle occurring every 1/16,000,000 of a second, applying a 64 prescale means Timer #0 is ticking at 64 times the base oscillator rate, or 64*1/16,000,000, which is every:

  • 0.000004 seconds
  • 0.004 ms
  • 4 µs

منبع

۲.) millis is not stored in a register, it is stored in 4 bytes (32 bits) of memory. 4 bytes can hold a number as large as:
(2^32-1) = 4294967295
A value this large will roll over to zero every:
4294967295 / 1000 / 60 / 60 / 24 = 49.71 days

منبع

http://www.gammon.com.au/millis

منتشر شده در آردوینو

اولین باشید که نظر می دهید

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *