# Time 10, Exercise 1: a watch crystal, its divider chain and its drift SECONDS_PER_DAY = 86400 freq = 32768 # hertz, the usual watch crystal # Part 1: how many times must we halve the frequency to reach 1 tick a second? count = 0 f = freq while f > 1: f = f // 2 # each divider stage halves the frequency count = count + 1 print("halvings from 32768 Hz to 1 Hz:", count, "(2 to the power", count, "=", 2 ** count, ")") print("crystal vibrations per day:", freq * SECONDS_PER_DAY) # Part 2: drift from a rate error given in parts per million (ppm). for ppm in (5, 20): per_day = ppm * 1e-6 * SECONDS_PER_DAY print(ppm, "ppm:", round(per_day, 2), "s a day,", round(per_day * 30, 1), "s a month") # What error in ppm is "one second a month"? one_sec_ppm = 1 / (SECONDS_PER_DAY * 30) * 1e6 print("one second a month is", round(one_sec_ppm, 2), "ppm") # Part 3: temperature. A tuning-fork crystal is fastest at 25 C and slows by # about 0.034 ppm times the square of the distance from 25 C. for temp in (25, 10, 0, 35, 40): error_ppm = -0.034 * (temp - 25) ** 2 seconds_day = error_ppm * 1e-6 * SECONDS_PER_DAY print(temp, "C:", round(error_ppm, 2), "ppm =", round(seconds_day, 2), "s a day") # Output when run: # halvings from 32768 Hz to 1 Hz: 15 (2 to the power 15 = 32768 ) # crystal vibrations per day: 2831155200 # 5 ppm: 0.43 s a day, 13.0 s a month # 20 ppm: 1.73 s a day, 51.8 s a month # one second a month is 0.39 ppm # 25 C: -0.0 ppm = -0.0 s a day # 10 C: -7.65 ppm = -0.66 s a day # 0 C: -21.25 ppm = -1.84 s a day # 35 C: -3.4 ppm = -0.29 s a day # 40 C: -7.65 ppm = -0.66 s a day # Reading the output: 32768 is 2 to the power 15, so fifteen halving stages turn the crystal's # vibrations into one tick a second. The "-0.0" at 25 C is just Python printing a negative zero; # the error there is zero. A watch left on a cold windowsill at 0 C loses nearly two seconds a # day, which is why wearing a watch on the wrist, close to body temperature, helps it keep time.