Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others
Embed:
(wiki syntax)
Show/hide line numbers
mx_tick.h
00001 /** 00002 * File: mx_timer.cpp 00003 * 00004 * Author: Modtronix Engineering - www.modtronix.com 00005 * 00006 * Description: 00007 * 00008 * Software License Agreement: 00009 * This software has been written or modified by Modtronix Engineering. The code 00010 * may be modified and can be used free of charge for commercial and non commercial 00011 * applications. If this is modified software, any license conditions from original 00012 * software also apply. Any redistribution must include reference to 'Modtronix 00013 * Engineering' and web link(www.modtronix.com) in the file header. 00014 * 00015 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS, 00016 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 00017 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE 00018 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 00019 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 00020 */ 00021 #ifndef MODTRONIX_NZ32S_MX_TICK_H_ 00022 #define MODTRONIX_NZ32S_MX_TICK_H_ 00023 00024 #include "nz32s_default_config.h" 00025 00026 #define MODTRONIX_NZ32S_MX_TICK_INC MxTick::increment() 00027 00028 /** A general purpose milli-second and second timer. 00029 * 00030 * Because the read_ms() function return a 32 bit value, it can be used to time up 00031 * to a maximum of 2^31-1 millseconds = 596 Hours = 24.8 Days 00032 * 00033 * Example: 00034 * @code 00035 * // Count the time to toggle a LED 00036 * 00037 * #include "mbed.h" 00038 * #include "mx_tick.h" 00039 * 00040 * MxTimer mxTmr; 00041 * int tmrLED = 0; 00042 * DigitalOut led(LED1); 00043 * 00044 int main() { 00045 00046 //Main loop 00047 while(1) { 00048 //Flash LED 00049 if (mxTmr.read_ms() >= tmrLED) { 00050 tmrLED += 1000; //Wait 1000mS before next LED toggle 00051 led = !led; 00052 } 00053 } 00054 } 00055 * @endcode 00056 */ 00057 class MxTick { 00058 00059 public: 00060 MxTick(bool autoInc = true); 00061 00062 static inline void increment() { 00063 tickMs++; 00064 if (--countMs == 0) { 00065 countMs = 1000; 00066 tickSec++; 00067 } 00068 } 00069 00070 00071 /** Get the current 32-bit milli-second tick value. 00072 * Because the read_ms() function return a 32 bit value, it can be used to time up 00073 * to a maximum of 2^31-1 millseconds = 596 Hours = 24.8 Days 00074 * 00075 * This function is thread save! 00076 * 00077 * @return The tick value in milli-seconds 00078 */ 00079 static inline int read_ms() { 00080 return (int)tickMs; 00081 } 00082 00083 /** Get the current 32-bit "10 milli-second" tick value. It is incremented each 10ms. 00084 * Because the read_ms() function return a 32 bit value, it can be used to time up 00085 * to a maximum of 2^31-1 * 10 millseconds = 5960 Hours = 248 Days 00086 * 00087 * This function is NOT thread save. Takes multiple cycles to calculate value. 00088 * 00089 * Note this function takes MUCH longer than read_ms() and read_sec(). 00090 * 00091 * @return The tick value in 10 x milli-seconds 00092 */ 00093 static int read_ms10() { 00094 //For example, if tickMs was 456789, we only want to use the 78 part. 00095 //The 456 is seconds, and we get that from tickSec, so don't use it 00096 //The 9 is 1xms, and don't need that 00097 // 456789 / 10 = 45678 00098 // 45678 % 100 = 89 00099 return (int)( ((tickMs/10)%100) + (tickSec*100) ); 00100 } 00101 00102 /** Get the current 32-bit "100 milli-second" tick value. It is incremented each 100ms. 00103 * Because the read_ms() function return a 32 bit value, it can be used to time up 00104 * to a maximum of 2^31-1 * 100 millseconds = 59600 Hours = 2480 Days = 6.79 Years 00105 * 00106 * This function is NOT thread save. Takes multiple cycles to calculate value. 00107 * 00108 * Note this function takes MUCH longer than read_ms() and read_sec(). 00109 * 00110 * @return The tick value in 100 x milli-seconds 00111 */ 00112 static int read_ms100() { 00113 //For example, if tickMs was 456789, we only want to use the 7 part(100 x Ms). 00114 //The 456 is seconds, and we get that from tickSec, so don't use it 00115 //The 8 is 1xMs, the 7 is 10xMs, and we don't need them 00116 // 456789 / 100 = 4567 00117 // 45678 % 10 = 7 00118 return (int)( ((tickMs/100)%10) + (tickSec*10) ); 00119 } 00120 00121 /** Get the current 32-bit second tick value. 00122 * Because the read_sec() function return a 32 bit value, it can be used to time up 00123 * to a maximum of 2^31-1 seconds = 596,523 Hours = 24855 Days = 68 Years 00124 * 00125 * This function is thread save! 00126 * 00127 * @return The tick value in seconds 00128 */ 00129 static inline int read_sec() { 00130 return (int)tickSec; 00131 } 00132 00133 protected: 00134 static uint16_t countMs; 00135 static int32_t tickSec; 00136 static int32_t tickMs; 00137 static bool running; 00138 }; 00139 00140 00141 00142 #endif /* MODTRONIX_NZ32S_MX_TICK_H_ */
Generated on Tue Jul 12 2022 15:45:25 by 1.7.2