millis()

概要

millis()は、内部で保持しているArduinoを起動してからの時間をミリ秒単位で返します。

millis()のリファレンスはこちらをご覧ください。

ソースコード

millis()は、hardware/arduino/avr/cores/arduino/wiring.c に定義されています。以下に全ソースコードを示します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
volatile unsigned long timer0_millis = 0;
 
unsigned long millis()
{
    unsigned long m;
    uint8_t oldSREG = SREG;
 
    // disable interrupts while we read timer0_millis or we might get an
    // inconsistent value (e.g. in the middle of a write to timer0_millis)
    cli();
    m = timer0_millis;
    SREG = oldSREG;
 
    return m;
}

millis()には、入力はありません。unsigned longを返します。

まず、状態レジスタを保存します。

3
4
5
6
unsigned long millis()
    {
    unsigned long m;
    uint8_t oldSREG = SREG;

SREGは、ATMega328Pの状態レジスタです。

次に、割り込みを禁止した後、timer0_millisをmにコピーし、保存した状態レジスタをもとに戻し、mを返します。

 8
 9
10
11
12
13
14
15
    // disable interrupts while we read timer0_millis or we might get an
    // inconsistent value (e.g. in the middle of a write to timer0_millis)
    cli();
    m = timer0_millis;
    SREG = oldSREG;
 
    return m;
}

cli()は割り込みを禁止するアセンブラ命令を実行するマクロです。

timer0_millisは、Arduinoを起動してからのミリ秒を保持している変数です。この変数は、TIMER0_OVF_vectという割り込みハンドラで設定されている変数です。

timer0_millisを直接returnせずに、いったん変数mに代入しているのは、割り込みを禁止した状態でtimer0_millisのコピーを作成するためだと思われます。SREG=oldSREGとして状態レジスタをもとに戻すと、割り込みが許可される可能性があり、その後、return timer0_millis()を実行してしまうと、書き込み中のtimer0_millisにアクセスしてしまう可能性があるのではないかと思われます(コメントからの類推です)

バージョン

Arduino AVR Boards 1.8.6

ライセンス

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
  Copyright (c) 2016 garretlab.
  Added source code explanation by garretlab.
*/

/*
  wiring.c - Partial implementation of the Wiring API for the ATmega8.
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

最終更新日

July 31, 2025

inserted by FC2 system