TRC
Dependents: RTC_terminal HorlogeSimple
RTC_IUT.h@3:e8cbd2214d3b, 2020-06-03 (annotated)
- Committer:
- gr91
- Date:
- Wed Jun 03 08:00:37 2020 +0000
- Revision:
- 3:e8cbd2214d3b
- Parent:
- 2:1a77823cf4c9
V4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gr91 | 3:e8cbd2214d3b | 1 | /// @file |
gr91 | 0:f17b3622fae1 | 2 | /// @mainpage |
gr91 | 0:f17b3622fae1 | 3 | /// |
gr91 | 0:f17b3622fae1 | 4 | /// |
gr91 | 0:f17b3622fae1 | 5 | /// |
gr91 | 0:f17b3622fae1 | 6 | /// |
gr91 | 0:f17b3622fae1 | 7 | /// @note |
gr91 | 0:f17b3622fae1 | 8 | /// History |
gr91 | 0:f17b3622fae1 | 9 | /// v3.00 31/10/2019 |
gr91 | 0:f17b3622fae1 | 10 | |
gr91 | 0:f17b3622fae1 | 11 | |
gr91 | 3:e8cbd2214d3b | 12 | //#include "mbed.h" |
gr91 | 0:f17b3622fae1 | 13 | |
gr91 | 0:f17b3622fae1 | 14 | typedef int int32_t; |
gr91 | 0:f17b3622fae1 | 15 | |
gr91 | 0:f17b3622fae1 | 16 | |
gr91 | 0:f17b3622fae1 | 17 | #include <time.h> |
gr91 | 0:f17b3622fae1 | 18 | |
gr91 | 0:f17b3622fae1 | 19 | /// RealTimeClock is an interface to the mbed RTC module |
gr91 | 0:f17b3622fae1 | 20 | /// |
gr91 | 0:f17b3622fae1 | 21 | /// It provides interfaces to get and set the time in text representation |
gr91 | 0:f17b3622fae1 | 22 | /// as well as a means to get and set the timezone offset so that it |
gr91 | 0:f17b3622fae1 | 23 | /// maintains time in UTC format. |
gr91 | 0:f17b3622fae1 | 24 | /// |
gr91 | 0:f17b3622fae1 | 25 | |
gr91 | 0:f17b3622fae1 | 26 | /// |
gr91 | 0:f17b3622fae1 | 27 | /// @note It is recommended that the system has a battery connected to |
gr91 | 0:f17b3622fae1 | 28 | /// the mbed battery backup pin. |
gr91 | 0:f17b3622fae1 | 29 | /// |
gr91 | 0:f17b3622fae1 | 30 | |
gr91 | 0:f17b3622fae1 | 31 | /// |
gr91 | 0:f17b3622fae1 | 32 | /// Example: |
gr91 | 0:f17b3622fae1 | 33 | /// @code |
gr91 | 3:e8cbd2214d3b | 34 | ////#include "mbed.h" |
gr91 | 3:e8cbd2214d3b | 35 | ////#include "RTC_IUT.h" |
gr91 | 3:e8cbd2214d3b | 36 | ///Rtc rtc; |
gr91 | 0:f17b3622fae1 | 37 | /// |
gr91 | 1:b85409cf9f7b | 38 | ///int main() |
gr91 | 1:b85409cf9f7b | 39 | ///{ |
gr91 | 1:b85409cf9f7b | 40 | /// //char TimeTest[] = "31/12/1999 23:59:50"; |
gr91 | 1:b85409cf9f7b | 41 | /// //char TimeTest[] = "19/01/2038 03:13:50"; |
gr91 | 1:b85409cf9f7b | 42 | /// char TimeTest[] = "03/06/1970 10:00:00"; |
gr91 | 1:b85409cf9f7b | 43 | /// time_t epoc; |
gr91 | 1:b85409cf9f7b | 44 | /// char buf[50]; |
gr91 | 1:b85409cf9f7b | 45 | /// rtc.SetTimeString(TimeTest); |
gr91 | 1:b85409cf9f7b | 46 | /// rtc.GetTimeString(buf); |
gr91 | 1:b85409cf9f7b | 47 | /// epoc =rtc.GetTimeValue(); |
gr91 | 1:b85409cf9f7b | 48 | /// printf("epoc: %8X, time: %s\r\n", epoc, buf); |
gr91 | 1:b85409cf9f7b | 49 | /// while(1) { |
gr91 | 1:b85409cf9f7b | 50 | /// rtc.SetYear(2020); // Set the year |
gr91 | 3:e8cbd2214d3b | 51 | /// rtc.GetTimeString(buf); // read date time string DD:MM:YYY hh:mm:ss |
gr91 | 1:b85409cf9f7b | 52 | /// epoc =rtc.GetTimeValue(); // Read second since 01/01/1970 |
gr91 | 1:b85409cf9f7b | 53 | /// printf("epoc: %8X, time: %s\r\n", epoc, buf); |
gr91 | 1:b85409cf9f7b | 54 | /// wait(0.9999); |
gr91 | 1:b85409cf9f7b | 55 | /// } |
gr91 | 1:b85409cf9f7b | 56 | ///} |
gr91 | 0:f17b3622fae1 | 57 | /// @endcode |
gr91 | 0:f17b3622fae1 | 58 | /// |
gr91 | 0:f17b3622fae1 | 59 | class Rtc |
gr91 | 0:f17b3622fae1 | 60 | { |
gr91 | 0:f17b3622fae1 | 61 | public: |
gr91 | 0:f17b3622fae1 | 62 | /// Constructor for the RealTimeClock interface, usually implemented |
gr91 | 0:f17b3622fae1 | 63 | /// outside of main( ) |
gr91 | 0:f17b3622fae1 | 64 | /// |
gr91 | 0:f17b3622fae1 | 65 | Rtc(); |
gr91 | 0:f17b3622fae1 | 66 | |
gr91 | 0:f17b3622fae1 | 67 | /// Destructor for the RealTimeClock, usually not executed |
gr91 | 0:f17b3622fae1 | 68 | /// |
gr91 | 0:f17b3622fae1 | 69 | ~Rtc(); |
gr91 | 0:f17b3622fae1 | 70 | |
gr91 | 0:f17b3622fae1 | 71 | /// GetTimeValue gets the current time in time_t format |
gr91 | 0:f17b3622fae1 | 72 | /// |
gr91 | 0:f17b3622fae1 | 73 | /// Gets the current time from the rtc and returns the time in time_t format. |
gr91 | 0:f17b3622fae1 | 74 | /// This is functionally identical to "return time(NULL);", |
gr91 | 0:f17b3622fae1 | 75 | /// simply wrapped in this class for convenience. |
gr91 | 0:f17b3622fae1 | 76 | /// |
gr91 | 0:f17b3622fae1 | 77 | /// @returns current time in time_t format |
gr91 | 0:f17b3622fae1 | 78 | /// |
gr91 | 0:f17b3622fae1 | 79 | time_t GetTimeValue(); |
gr91 | 0:f17b3622fae1 | 80 | |
gr91 | 0:f17b3622fae1 | 81 | /// SetTimeValue will set the current time in time_t format |
gr91 | 0:f17b3622fae1 | 82 | /// |
gr91 | 0:f17b3622fae1 | 83 | /// example SetTimeValue(1463673534) set the current time |
gr91 | 0:f17b3622fae1 | 84 | /// 19 May 2016 15:58:54 |
gr91 | 0:f17b3622fae1 | 85 | /// the min value is 978307200 (01/01/2001 00:00:00) |
gr91 | 0:f17b3622fae1 | 86 | void SetTimeValue(time_t t); |
gr91 | 0:f17b3622fae1 | 87 | |
gr91 | 0:f17b3622fae1 | 88 | /// GetTimeString gets the formatted time |
gr91 | 0:f17b3622fae1 | 89 | /// |
gr91 | 0:f17b3622fae1 | 90 | /// It places the formatted string into the buffer in the format of |
gr91 | 0:f17b3622fae1 | 91 | /// DD/MM/YYYY HH:MM:SS 30/12/2016 07:19:24 |
gr91 | 0:f17b3622fae1 | 92 | /// |
gr91 | 0:f17b3622fae1 | 93 | /// @returns nothing |
gr91 | 0:f17b3622fae1 | 94 | /// |
gr91 | 0:f17b3622fae1 | 95 | void GetTimeString(char * buffer); |
gr91 | 0:f17b3622fae1 | 96 | |
gr91 | 0:f17b3622fae1 | 97 | |
gr91 | 0:f17b3622fae1 | 98 | /// SetTimeString will set the Real Time Clock from a well formatted string |
gr91 | 0:f17b3622fae1 | 99 | /// |
gr91 | 0:f17b3622fae1 | 100 | /// This will read a string, and if it is well formed, it will then |
gr91 | 0:f17b3622fae1 | 101 | /// set the RTC based on this string. The string should be of the form: |
gr91 | 0:f17b3622fae1 | 102 | /// 'DD/MM/YYYY HH:MM:SS' |
gr91 | 0:f17b3622fae1 | 103 | /// |
gr91 | 0:f17b3622fae1 | 104 | /// example: 30/12/2011 13:15:07 |
gr91 | 0:f17b3622fae1 | 105 | /// |
gr91 | 0:f17b3622fae1 | 106 | /// @param timestring is the string to parse |
gr91 | 0:f17b3622fae1 | 107 | /// @returns true if the time was set |
gr91 | 0:f17b3622fae1 | 108 | /// |
gr91 | 0:f17b3622fae1 | 109 | bool SetTimeString(char * timestring); |
gr91 | 0:f17b3622fae1 | 110 | |
gr91 | 0:f17b3622fae1 | 111 | |
gr91 | 0:f17b3622fae1 | 112 | /// SetYear writes the year |
gr91 | 0:f17b3622fae1 | 113 | /// |
gr91 | 2:1a77823cf4c9 | 114 | /// year format YYYY (1970 - 2038) |
gr91 | 0:f17b3622fae1 | 115 | void SetYear(unsigned short yyyy); |
gr91 | 0:f17b3622fae1 | 116 | |
gr91 | 0:f17b3622fae1 | 117 | /// GetYear reads the year |
gr91 | 0:f17b3622fae1 | 118 | /// |
gr91 | 0:f17b3622fae1 | 119 | /// @param void |
gr91 | 0:f17b3622fae1 | 120 | /// @returns the year unsigned short |
gr91 | 0:f17b3622fae1 | 121 | unsigned short GetYear(void); |
gr91 | 0:f17b3622fae1 | 122 | |
gr91 | 0:f17b3622fae1 | 123 | /// SetMon writes the month |
gr91 | 0:f17b3622fae1 | 124 | /// |
gr91 | 0:f17b3622fae1 | 125 | /// month format mm (1 - 12) |
gr91 | 0:f17b3622fae1 | 126 | void SetMon(unsigned char mm); |
gr91 | 0:f17b3622fae1 | 127 | |
gr91 | 0:f17b3622fae1 | 128 | /// GetMon reads the month |
gr91 | 0:f17b3622fae1 | 129 | /// |
gr91 | 0:f17b3622fae1 | 130 | /// @param void |
gr91 | 0:f17b3622fae1 | 131 | /// @returns the month unsigned char |
gr91 | 0:f17b3622fae1 | 132 | unsigned char GetMon(void); |
gr91 | 0:f17b3622fae1 | 133 | |
gr91 | 0:f17b3622fae1 | 134 | /// SetDay writes the day |
gr91 | 0:f17b3622fae1 | 135 | /// |
gr91 | 0:f17b3622fae1 | 136 | /// day format dd (1 - 31) |
gr91 | 0:f17b3622fae1 | 137 | void SetDay(unsigned char dd); |
gr91 | 0:f17b3622fae1 | 138 | |
gr91 | 0:f17b3622fae1 | 139 | /// GetDay reads the day |
gr91 | 0:f17b3622fae1 | 140 | /// |
gr91 | 0:f17b3622fae1 | 141 | /// @param void |
gr91 | 0:f17b3622fae1 | 142 | /// @returns the day unsigned char |
gr91 | 0:f17b3622fae1 | 143 | unsigned char GetDay(void); |
gr91 | 0:f17b3622fae1 | 144 | |
gr91 | 0:f17b3622fae1 | 145 | /// SetHour writes the hour |
gr91 | 0:f17b3622fae1 | 146 | /// |
gr91 | 0:f17b3622fae1 | 147 | /// hour format hh (0 - 23) |
gr91 | 0:f17b3622fae1 | 148 | void SetHour(unsigned char hh); |
gr91 | 0:f17b3622fae1 | 149 | |
gr91 | 0:f17b3622fae1 | 150 | /// GetHour reads the hour |
gr91 | 0:f17b3622fae1 | 151 | /// |
gr91 | 0:f17b3622fae1 | 152 | /// @param void |
gr91 | 0:f17b3622fae1 | 153 | /// @returns the hour unsigned char |
gr91 | 0:f17b3622fae1 | 154 | unsigned char GetHour(void); |
gr91 | 0:f17b3622fae1 | 155 | |
gr91 | 0:f17b3622fae1 | 156 | /// SetMin writes the minutes |
gr91 | 0:f17b3622fae1 | 157 | /// |
gr91 | 0:f17b3622fae1 | 158 | /// minute format mm (0 - 59) |
gr91 | 0:f17b3622fae1 | 159 | void SetMin(unsigned char mm); |
gr91 | 0:f17b3622fae1 | 160 | |
gr91 | 0:f17b3622fae1 | 161 | /// GetMin reads the minute |
gr91 | 0:f17b3622fae1 | 162 | /// |
gr91 | 0:f17b3622fae1 | 163 | /// @param void |
gr91 | 0:f17b3622fae1 | 164 | /// @returns the minute unsigned char |
gr91 | 0:f17b3622fae1 | 165 | unsigned char GetMin(void); |
gr91 | 0:f17b3622fae1 | 166 | |
gr91 | 0:f17b3622fae1 | 167 | /// SetSec writes the seconds |
gr91 | 0:f17b3622fae1 | 168 | /// |
gr91 | 0:f17b3622fae1 | 169 | /// second format ss (0 - 59) |
gr91 | 0:f17b3622fae1 | 170 | void SetSec(unsigned char ss); |
gr91 | 0:f17b3622fae1 | 171 | |
gr91 | 0:f17b3622fae1 | 172 | /// GetSec reads the second |
gr91 | 0:f17b3622fae1 | 173 | /// |
gr91 | 0:f17b3622fae1 | 174 | /// @param void |
gr91 | 0:f17b3622fae1 | 175 | /// @returns the second unsigned char |
gr91 | 0:f17b3622fae1 | 176 | unsigned char GetSec(void); |
gr91 | 0:f17b3622fae1 | 177 | |
gr91 | 0:f17b3622fae1 | 178 | private: |
gr91 | 0:f17b3622fae1 | 179 | |
gr91 | 0:f17b3622fae1 | 180 | }; |
gr91 | 0:f17b3622fae1 | 181 | |
gr91 | 0:f17b3622fae1 | 182 |