This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-09-10

class tick_counter - Stop Watch

class tick_counter - Stop Watch
    Stop Watch is used to measure very short processing times in the msec and usec domains. It uses the microcontroller’s hardware counting function.

    class tick_counter - Stop Watch

    Used to measure very short processing times in the msec and usec ranges. It uses the microcontroller’s hardware counting function.

    For this library code, it is recommended to keep its use experimental after sufficient verification. If measurement in units of approximately 30usec is sufficient, using the wake-up timer’s count value is simpler.

    • It controls the CoreDebug->DEMCR and DWT registers.

      • It is believed that these registers are not intended for general, widespread use. While you may not experience major issues during temporary time measurements in development, their use in firmware at the final operational stage is not recommended.
    • It is possible to construct multiple tick_counter objects simultaneously.

      • The counting function used is a single one and is shared among the objects.
      • The counting function is started when the first object is constructed and stopped when all objects are destroyed.

    Example:

    #include <mwf_stop_watch.hpp>
    
    void some_func() {
        // ...
    
        // Start measurement 1
        mwf::periph::tick_counter sw;
    
        sw.lap(); // Start measurement 1 (although lap() is also called when sw is constructed, call it directly before the process for more precise measurement)
        // ...       // Process to be measured
        sw.lap(); // End measurement 1
    
        // Display value (Start measurement 1 to End measurement 1)
        PRINTF("%dusec", sw.get_us());
    
        // Next process
        sw.lap(); // Start measurement 2
        // ... // Process to be measured
        sw.lap(); // End measurement 2
    
        // Display value (Start measurement 2 to End measurement 2)
        PRINTF("%dusec", sw.get_us());
    }

    _start_counter(), _stop_counter()

    static void _start_counter();
    static void _stop_counter();

    These functions start and stop the count timer using the CoreDebug feature.

    tick_counter()

    This is the constructor. If no other objects have been constructed, it starts the counter and also calls lap() to begin measurement.

    ~tick_counter()

    This is the destructor. It stops the counter when all class objects have been destroyed.

    lap()

    This function saves the previous count value and stores the count value at the time it was called.

    The elapsed time is obtained with get_us().