Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Calibrate.ino
- *
- * Arduino sketch for the CompileTime library
- * to calibrate the exact timing to compensate
- * for Arduino crystal speed differences/
- *
- * version 1.0 written June 2023 - Trent M. Wyatt
- *
- */
- #include <CompileTime.h>
- using namespace CompileTime;
- static uint32_t sys_start_secs;
- static uint32_t arduino_start_micros;
- static uint32_t arduino_stop_micros;
- static uint32_t duration_micros;
- //name of months
- const char* months[12] {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
- #define CALIBRATE_MINUTES_TIME 5
- void setup() {
- // pass:
- // - the number of seconds it takes to upload
- CompileTime::setCompileTime(8);
- arduino_start_micros = micros();
- CompileTime::uSecAdjust = 1000000UL;
- duration_micros = CALIBRATE_MINUTES_TIME * 60UL * 1000000UL;
- arduino_stop_micros = arduino_start_micros + duration_micros;
- sys_start_secs = (CompileTime::hour * 60UL * 60UL) + (CompileTime::minute * 60UL) + CompileTime::second;
- char buff[32] = "";
- sprintf(buff, "%d:%02d:%02d", CompileTime::hour, CompileTime::minute, CompileTime::second);
- Serial.begin(115200);
- Serial.println(buff);
- Serial.print(F("Running calibration for "));
- Serial.print(CALIBRATE_MINUTES_TIME, DEC);
- Serial.println(F(" minutes"));
- Serial.println(F("Minute total secs: ............................................................"));
- Serial.print(F("Minute 1 progress: "));
- }
- void loop() {
- static int16_t lasts = CompileTime::second;
- static int16_t secs_passed = 0;
- static int16_t min_num = 1;
- uint32_t const now_us = micros();
- updateTime(now_us);
- if (now_us < arduino_stop_micros) {
- if (CompileTime::second != lasts) {
- lasts = CompileTime::second;
- secs_passed++;
- Serial.print('.');
- }
- if (secs_passed >= 60) {
- secs_passed -= 60;
- min_num++;
- Serial.print(F("\nMinute "));
- Serial.print(min_num);
- Serial.print(F(" progress: "));
- }
- }
- else {
- Serial.println("\nEnter the system time string in the monitor window as hours:minutes:seconds");
- while (Serial.available() < 8) { }
- String timestr = Serial.readString();
- timestr.trim();
- char const * ptr = timestr.c_str();
- uint16_t sysh = atoi(ptr);
- uint16_t sysm = atoi(ptr+3);
- uint16_t syss = atoi(ptr+6);
- uint32_t const sys_end_secs = (sysh * 60UL * 60UL) + (sysm * 60UL) + syss;
- uint32_t const sys_delta_secs = sys_end_secs - sys_start_secs;
- Serial.print(F("System seconds passed = "));
- Serial.println(sys_delta_secs);
- float sys_us = float(sys_delta_secs) * 1000000.0;
- uint32_t cal_us = (uint32_t)(((float)duration_micros / sys_us) * 1000000.0);
- Serial.print(F("New microseconds per second = "));
- Serial.println(cal_us);
- cal_us -= 8; // internal adjust
- CompileTime::writeEepromCalibrate(cal_us);
- Serial.println(F("New microseconds has been written to EEPROM!"));
- CompileTime::uSecAdjust = cal_us;
- char buff[32] = "";
- int16_t lasts = CompileTime::second;
- while (true) {
- CompileTime::updateTime(micros());
- if (lasts != CompileTime::second) {
- lasts = CompileTime::second;
- sprintf(buff, "%s, %d, %d - %2d:%02d:%02d", months[month], day, year, hour, minute, second);
- Serial.println(buff);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment