Arduino-like millis() function.

Dependents:   QSL_SimplePublish MAX30100_FirstTry MAX30100_FirstTry MAX30100_V04 ... more

Arduino-like millis() function.

Returns the number of milliseconds elapsed since the millisStart() function has been called.
It will roll over back to zero after roughly 49.7 days.
If you would like to create short term timers or to trigger short term events then consider to use Timer, Timeout, Ticker, wait or us_ticker_read. For more details have a look at the Handbook.
In case you are looking for scheduling of long term events then you could be interested also in the RTC library or the Clock library.

Example of use:

#include "mbed.h"
#include "millis.h"

Serial  pc(USBTX, USBRX);

int main() {
    millisStart();
    while(1) {
        pc.printf("millis = %d\r\n", millis());        
        wait(1.0);
    }
}


NOTE: Make sure you call millisStart() before using millis().

Warning

Works only on mbed boards equipped with SysTick!

Committer:
hudakz
Date:
Thu Jun 02 16:51:36 2016 +0000
Revision:
2:ac7586424119
Parent:
1:69c49c2be760
rev. 02

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:b69f7e12064b 1 #include "mbed.h"
hudakz 0:b69f7e12064b 2 #include "millis.h"
hudakz 0:b69f7e12064b 3 /*
hudakz 0:b69f7e12064b 4 millis.cpp
hudakz 0:b69f7e12064b 5 Copyright (c) 2016 Zoltan Hudak <hudakz@inbox.com>
hudakz 0:b69f7e12064b 6 All rights reserved.
hudakz 0:b69f7e12064b 7
hudakz 0:b69f7e12064b 8 This program is free software: you can redistribute it and/or modify
hudakz 0:b69f7e12064b 9 it under the terms of the GNU General Public License as published by
hudakz 0:b69f7e12064b 10 the Free Software Foundation, either version 3 of the License, or
hudakz 0:b69f7e12064b 11 (at your option) any later version.
hudakz 0:b69f7e12064b 12
hudakz 0:b69f7e12064b 13 This program is distributed in the hope that it will be useful,
hudakz 0:b69f7e12064b 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:b69f7e12064b 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:b69f7e12064b 16 GNU General Public License for more details.
hudakz 0:b69f7e12064b 17
hudakz 0:b69f7e12064b 18 You should have received a copy of the GNU General Public License
hudakz 0:b69f7e12064b 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:b69f7e12064b 20 */
hudakz 0:b69f7e12064b 21
hudakz 0:b69f7e12064b 22 volatile unsigned long _millis;
hudakz 0:b69f7e12064b 23
hudakz 1:69c49c2be760 24 void millisStart(void) {
hudakz 0:b69f7e12064b 25 SysTick_Config(SystemCoreClock / 1000);
hudakz 0:b69f7e12064b 26 }
hudakz 0:b69f7e12064b 27
hudakz 1:69c49c2be760 28 extern "C" void SysTick_Handler(void) {
hudakz 0:b69f7e12064b 29 _millis++;
hudakz 0:b69f7e12064b 30 }
hudakz 0:b69f7e12064b 31
hudakz 2:ac7586424119 32 unsigned long millis(void) {
hudakz 0:b69f7e12064b 33 return _millis;
hudakz 0:b69f7e12064b 34 }
hudakz 0:b69f7e12064b 35
hudakz 2:ac7586424119 36