Merge branch 'patch-7' of https://github.com/stevstrong/Arduino_STM32 into stevstrong-patch-7

This commit is contained in:
Roger Clark 2017-12-10 20:52:20 +11:00
commit dbbb6b0c25
1 changed files with 77 additions and 47 deletions

View File

@ -4,9 +4,9 @@
based on https://github.com/rogerclarkmelbourne/Arduino_STM32 based on https://github.com/rogerclarkmelbourne/Arduino_STM32
1. Blink on PC13 per 4s or 7s by attachAlarmInterrupt for 10 times 1. Blink on PC13 per 4s or 7s by attachAlarmInterrupt for 10 times
2. Second counter by attachSecondsInterrpt 2. Second counter by attachSecondsInterrupt
3. Serial output on(41s) or off(21s) by creatAlarm 3. Serial output on(41s) or off(21s) by creatAlarm
4. change to your timezone in the sketch; . 4. change to your timezone in the sketch;
3. get Unix epoch time from https://www.epochconverter.com/ ; 3. get Unix epoch time from https://www.epochconverter.com/ ;
4. last step input the 10 bits number( example: 1503945555) to Serialport ; 4. last step input the 10 bits number( example: 1503945555) to Serialport ;
5. the clock will be reset to you wanted. 5. the clock will be reset to you wanted.
@ -28,70 +28,110 @@
RTClock rtclock (RTCSEL_LSE); // initialise RTClock rtclock (RTCSEL_LSE); // initialise
int timezone = 8; // change to your timezone int timezone = 8; // change to your timezone
time_t tt; time_t tt, tt1;
time_t tt1; tm_t mtt;
tm_t mtt = { 47, 9, 13, 3, 11, 22, 30, 30 }; // init time 47+1970 = 2017 Unix epoch Time counted from 00:00:00 1 Jan 1970
char weekday1[][7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // 0,1,2,3,4,5,6
uint8_t dateread[11]; uint8_t dateread[11];
int globAlmCount = 0; int globAlmCount = 0;
int lastGlobAlmCount; int lastGlobAlmCount;
int SPECAlmCount = 0; int SPECAlmCount = 0;
int lastSPECAlmCount; int lastSPECAlmCount;
int i = 0;
int alarmcount = 3; int alarmcount = 3;
uint8_t AlarmExchange = 0; uint8_t AlarmExchange = 0;
bool dispflag = true; bool dispflag = true;
#define LED_PIN PC13 //-----------------------------------------------------------------------------
const char * weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// This function is called in the attachSecondsInterrpt const char * months[] = {"Dummy", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
//-----------------------------------------------------------------------------
uint8_t str2month(const char * d)
{
uint8_t i = 13;
while ( (--i) && strcmp(months[i], d)!=0 );
return i;
}
//-----------------------------------------------------------------------------
const char * delim = " :";
char s[128]; // for sprintf
//-----------------------------------------------------------------------------
void ParseBuildTimestamp(tm_t & mt)
{
// Timestamp format: "Dec 8 2017, 22:57:54"
sprintf(s, "Timestamp: %s, %s\n", __DATE__, __TIME__);
//Serial.print(s);
char * token = strtok(s, delim); // get first token
// walk through tokens
while( token != NULL ) {
uint8_t m = str2month((const char*)token);
if ( m>0 ) {
mt.month = m;
//Serial.print(" month: "); Serial.println(mt.month);
token = strtok(NULL, delim); // get next token
mt.day = atoi(token);
//Serial.print(" day: "); Serial.println(mt.day);
token = strtok(NULL, delim); // get next token
mt.year = atoi(token) - 1970;
//Serial.print(" year: "); Serial.println(mt.year);
token = strtok(NULL, delim); // get next token
mt.hour = atoi(token);
//Serial.print(" hour: "); Serial.println(mt.hour);
token = strtok(NULL, delim); // get next token
mt.minute = atoi(token);
//Serial.print(" minute: "); Serial.println(mt.minute);
token = strtok(NULL, delim); // get next token
mt.second = atoi(token);
//Serial.print(" second: "); Serial.println(mt.second);
}
token = strtok(NULL, delim);
}
}
//-----------------------------------------------------------------------------
// This function is called in the attachSecondsInterrupt
//-----------------------------------------------------------------------------
void SecondCount () void SecondCount ()
{ {
tt++; tt++;
} }
// This function is called in the attachAlarmInterrpt //-----------------------------------------------------------------------------
// This function is called in the attachAlarmInterrupt
//-----------------------------------------------------------------------------
void blink () void blink ()
{ {
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
globAlmCount++; globAlmCount++;
//tt++;
} }
//-----------------------------------------------------------------------------
void OnOffSerial () void OnOffSerial ()
{ {
dispflag = !dispflag; dispflag = !dispflag;
SPECAlmCount++; SPECAlmCount++;
} }
//-----------------------------------------------------------------------------
void setup() void setup()
{ {
lastGlobAlmCount = ~globAlmCount; lastGlobAlmCount = ~globAlmCount;
lastSPECAlmCount = ~SPECAlmCount; lastSPECAlmCount = ~SPECAlmCount;
Serial.begin(115200); Serial.begin(115200);
pinMode(LED_PIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
tt = rtclock.makeTime(mtt); //while (!Serial); delay(1000);
rtclock.setTime(tt); ParseBuildTimestamp(mtt); // get the Unix epoch Time counted from 00:00:00 1 Jan 1970
tt = rtclock.makeTime(mtt) + 25; // additional seconds to compensate build and upload delay
rtclock.setTime(tt);
tt1 = tt; tt1 = tt;
rtclock.attachAlarmInterrupt(blink);// Call blink rtclock.attachAlarmInterrupt(blink);// Call blink
rtclock.attachSecondsInterrupt(SecondCount);// Call SecondCount rtclock.attachSecondsInterrupt(SecondCount);// Call SecondCount
} }
//-----------------------------------------------------------------------------
void loop() void loop()
{ {
while (Serial.available()) if ( Serial.available()>10 ) {
{ dateread[i] = Serial.read(); for (uint8_t i = 0; i<11; i++) {
if (i < 11) { dateread[i] = Serial.read();
i++;
}
else {
i = 0;
tt = (dateread[0] - '0') * 1000000000 + (dateread[1] - '0') * 100000000 + (dateread[2] - '0') * 10000000 + (dateread[3] - '0') * 1000000 + (dateread[4] - '0') * 100000;
tt += (dateread[5] - '0') * 10000 + (dateread[6] - '0') * 1000 + (dateread[7] - '0') * 100 + (dateread[8] - '0') * 10 + (dateread[9] - '0');
rtclock.TimeZone(tt, timezone); //adjust to your local date
rtclock.setTime(rtclock.TimeZone(tt, timezone));
} }
Serial.flush();
tt = atol((char*)dateread);
rtclock.setTime(rtclock.TimeZone(tt, timezone)); //adjust to your local date
} }
if (lastGlobAlmCount != globAlmCount | lastSPECAlmCount != SPECAlmCount ) { if (lastGlobAlmCount != globAlmCount || lastSPECAlmCount != SPECAlmCount ) {
if (globAlmCount == 10) { // to detachAlarmInterrupt and start creatAlarm after 10 times about 110s if (globAlmCount == 10) { // to detachAlarmInterrupt and start creatAlarm after 10 times about 110s
rtclock.detachAlarmInterrupt(); rtclock.detachAlarmInterrupt();
globAlmCount = 0; globAlmCount = 0;
@ -117,23 +157,13 @@ void loop()
} }
} }
} }
if (tt1 != tt & dispflag == true ) if (tt1 != tt && dispflag == true )
{ {
tt1 = tt; tt1 = tt;
//rtclock.breakTime(tt, mtt); // get and print actual RTC timestamp
Serial.print("Date: "); rtclock.breakTime(rtclock.now(), mtt);
Serial.print(rtclock.day()); sprintf(s, "RTC timestamp: %s %u %u, %s, %02u:%02u:%02u\n",
Serial.print("- "); months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second);
Serial.print(rtclock.month()); Serial.print(s);
Serial.print(" ");
Serial.print(rtclock.year() + 1970);
Serial.print(" ");
Serial.print(weekday1[rtclock.weekday()]);
Serial.print(" Time: ");
Serial.print(rtclock.hour());
Serial.print(" : ");
Serial.print(rtclock.minute());
Serial.print(" : ");
Serial.println(rtclock.second());
} }
} }