當前位置: 首頁>>代碼示例>>Python>>正文


Python machine.RTC屬性代碼示例

本文整理匯總了Python中machine.RTC屬性的典型用法代碼示例。如果您正苦於以下問題:Python machine.RTC屬性的具體用法?Python machine.RTC怎麽用?Python machine.RTC使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在machine的用法示例。


在下文中一共展示了machine.RTC屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: await_transition

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def await_transition(self):  # Wait until DS3231 seconds value changes
        self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
        ss = self.timebuf[0]
        while ss == self.timebuf[0]:
            self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
        return self.timebuf

# Get calibration factor for Pyboard RTC. Note that the DS3231 doesn't have millisecond resolution so we
# wait for a seconds transition to emulate it.
# This function returns the required calibration factor for the RTC (approximately the no. of ppm the
# RTC lags the DS3231).
# Delay(min) Outcome (successive runs). Note 1min/yr ~= 2ppm
#   5 173 169 173 173 173
#  10 171 173 171
#  20 172 172 174
#  40 173 172 173 Mean: 172.3 
# Note calibration factor is not saved on power down unless an RTC backup battery is used. An option is
# to store the calibration factor on disk and issue rtc.calibration(factor) on boot. 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:20,代碼來源:ds3231_pb.py

示例2: get_time

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def get_time(self, set_rtc = False):
        if set_rtc:
            data = self.await_transition()      # For accuracy set RTC immediately after a seconds transition
        else:
            self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
            data = self.timebuf
        ss = bcd2dec(data[0])
        mm = bcd2dec(data[1])
        if data[2] & 0x40:
            hh = bcd2dec(data[2] & 0x1f)
            if data[2] & 0x20:
                hh += 12
        else:
            hh = bcd2dec(data[2])
        wday = data[3]
        DD = bcd2dec(data[4])
        MM = bcd2dec(data[5] & 0x1f)
        YY = bcd2dec(data[6])
        if data[5] & 0x80:
            YY += 2000
        else:
            YY += 1900
        if set_rtc:
            rtc.init((YY, MM, DD, hh, mm, ss, 0))
        return (YY, MM, DD, hh, mm, ss, 0, 0) # Time from DS3231 in time.time() format (less yday) 
開發者ID:ayoy,項目名稱:upython-aq-monitor,代碼行數:27,代碼來源:ds3231.py

示例3: run

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def run(appname='home'):
    rtc = machine.RTC()
    rtc.memory(appname)
    print('Restrt to run app {}'.format(appname))
    restart() 
開發者ID:IBM-Developer-Korea,項目名稱:developer-badge-2018-apps,代碼行數:7,代碼來源:util.py

示例4: settime

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def settime():
    t = time()
    import machine
    import utime
    tm = utime.localtime(t)
    tm = tm[0:3] + (0,) + tm[3:6] + (0,)
    machine.RTC().datetime(tm)
    print(utime.localtime()) 
開發者ID:BradenM,項目名稱:micropy-cli,代碼行數:10,代碼來源:ntptime.py

示例5: set_time

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def set_time(rtc_time):
    rtc = None
    try:
        # Pyboard (pyboard doesn't have machine.RTC()).
        # The pyb.RTC.datetime function takes the arguments in the order:
        # (year, month, day, weekday, hour, minute, second, subseconds)
        # http://docs.micropython.org/en/latest/library/pyb.RTC.html#pyb.RTC.datetime
        import pyb
        rtc = pyb.RTC()
        rtc.datetime(rtc_time)
    except:
        try:
            import pycom
            # PyCom's machine.RTC takes its arguments in a slightly different order
            # than the official machine.RTC.
            # (year, month, day, hour, minute, second[, microsecond[, tzinfo]])
            # https://docs.pycom.io/firmwareapi/pycom/machine/rtc/#rtc-init-datetime-none-source-rtc-internal-rc
            rtc_time2 = (rtc_time[0], rtc_time[1], rtc_time[2], rtc_time[4], rtc_time[5], rtc_time[6])
            import machine
            rtc = machine.RTC()
            rtc.init(rtc_time2)
        except:
            try:
                # The machine.RTC documentation was incorrect and doesn't agree with the code, so no link
                # is presented here. The order of the arguments is the same as the pyboard.
                import machine
                rtc = machine.RTC()
                try:
                    # ESP8266 uses rtc.datetime() rather than rtc.init()
                    rtc.datetime(rtc_time)
                except:
                    # ESP32 (at least Loboris port) uses rtc.init()
                    rtc.init(rtc_time)
            except:
                pass


# 0x0D's sent from the host get transformed into 0x0A's, and 0x0A sent to the
# host get converted into 0x0D0A when using sys.stdin. sys.tsin.buffer does
# no transformations, so if that's available, we use it, otherwise we need
# to use hexlify in order to get unaltered data. 
開發者ID:dhylands,項目名稱:rshell,代碼行數:43,代碼來源:main.py

示例6: get_time

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def get_time(self, set_rtc=False):
        if set_rtc:
            self.await_transition()  # For accuracy set RTC immediately after a seconds transition
        else:
            self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf) # don't wait
        return self.convert(set_rtc) 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:8,代碼來源:ds3231_port.py

示例7: save_time

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def save_time(self):
        (YY, MM, mday, hh, mm, ss, wday, yday) = utime.localtime()  # Based on RTC
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 0, tobytes(dec2bcd(ss)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 1, tobytes(dec2bcd(mm)))
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 2, tobytes(dec2bcd(hh)))  # Sets to 24hr mode
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 3, tobytes(dec2bcd(wday + 1)))  # 1 == Monday, 7 == Sunday
        self.ds3231.writeto_mem(DS3231_I2C_ADDR, 4, tobytes(dec2bcd(mday)))  # Day of month
        if YY >= 2000:
            self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM) | 0b10000000))  # Century bit
            self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-2000)))
        else:
            self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM)))
            self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-1900)))

    # Wait until DS3231 seconds value changes before reading and returning data 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:17,代碼來源:ds3231_port.py

示例8: await_transition

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def await_transition(self):
        self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
        ss = self.timebuf[0]
        while ss == self.timebuf[0]:
            self.ds3231.readfrom_mem_into(DS3231_I2C_ADDR, 0, self.timebuf)
        return self.timebuf

    # Test hardware RTC against DS3231. Default runtime 10 min. Return amount
    # by which DS3231 clock leads RTC in PPM or seconds per year.
    # Precision is achieved by starting and ending the measurement on DS3231
    # one-seond boundaries and using ticks_ms() to time the RTC.
    # For a 10 minute measurement +-1ms corresponds to 1.7ppm or 53s/yr. Longer
    # runtimes improve this, but the DS3231 is "only" good for +-2ppm over 0-40C. 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:15,代碼來源:ds3231_port.py

示例9: rtc_test

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def rtc_test(self, runtime=600, ppm=False, verbose=True):
        if rtc is None:
            raise RuntimeError('machine.RTC does not exist')
        verbose and print('Waiting {} minutes for result'.format(runtime//60))
        factor = 1_000_000 if ppm else 114_155_200  # seconds per year

        self.await_transition()  # Start on transition of DS3231. Record time in .timebuf
        t = utime.ticks_ms()  # Get system time now
        ss = rtc.datetime()[6]  # Seconds from system RTC
        while ss == rtc.datetime()[6]:
            pass
        ds = utime.ticks_diff(utime.ticks_ms(), t)  # ms to transition of RTC
        ds3231_start = utime.mktime(self.convert())  # Time when transition occurred
        t = rtc.datetime()
        rtc_start = utime.mktime((t[0], t[1], t[2], t[4], t[5], t[6], t[3] - 1, 0))  # y m d h m s wday 0

        utime.sleep(runtime)  # Wait a while (precision doesn't matter)

        self.await_transition()  # of DS3231 and record the time
        t = utime.ticks_ms()  # and get system time now
        ss = rtc.datetime()[6]  # Seconds from system RTC
        while ss == rtc.datetime()[6]:
            pass
        de = utime.ticks_diff(utime.ticks_ms(), t)  # ms to transition of RTC
        ds3231_end = utime.mktime(self.convert())  # Time when transition occurred
        t = rtc.datetime()
        rtc_end = utime.mktime((t[0], t[1], t[2], t[4], t[5], t[6], t[3] - 1, 0))  # y m d h m s wday 0

        d_rtc = 1000 * (rtc_end - rtc_start) + de - ds  # ms recorded by RTC
        d_ds3231 = 1000 * (ds3231_end - ds3231_start)  # ms recorded by DS3231
        ratio = (d_ds3231 - d_rtc) / d_ds3231
        ppm = ratio * 1_000_000
        verbose and print('DS3231 leads RTC by {:4.1f}ppm {:4.1f}mins/yr'.format(ppm, ppm*1.903))
        return ratio * factor 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:36,代碼來源:ds3231_port.py

示例10: getcal

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def getcal(self, minutes=5, cal=0, verbose=True):
        if d_series:
            return self._getcal_d(minutes, cal, verbose)
        verbose and print('Pyboard 1.x. Waiting {} minutes for calibration factor.'.format(minutes))
        rtc.calibration(cal)  # Clear existing cal
        self.save_time()  # Set DS3231 from RTC
        self.await_transition()  # Wait for DS3231 to change: on a 1 second boundary
        tus = utime.ticks_us()
        st = rtc.datetime()[7]
        while rtc.datetime()[7] == st:  # Wait for RTC to change
            pass
        t1 = utime.ticks_diff(utime.ticks_us(), tus)  # t1 is duration (μs) between DS and RTC change (start)
        rtcstart = get_ms(rtc.datetime())  # RTC start time in mS
        dsstart = utime.mktime(self.convert())  # DS start time in secs as recorded by await_transition

        utime.sleep(minutes * 60)

        self.await_transition()  # DS second boundary
        tus = utime.ticks_us()
        st = rtc.datetime()[7]
        while rtc.datetime()[7] == st:
            pass
        t2 = utime.ticks_diff(utime.ticks_us(), tus)  # t2 is duration (μs) between DS and RTC change (end)
        rtcend = get_ms(rtc.datetime())
        dsend = utime.mktime(self.convert())
        dsdelta = (dsend - dsstart) * 1000000  # Duration (μs) between DS edges as measured by DS3231
        if rtcend < rtcstart:  # It's run past midnight. Assumption: run time < 1 day!
            rtcend += 24 * 3_600_000
        rtcdelta = (rtcend - rtcstart) * 1000 + t1 - t2  # Duration (μs) between DS edges as measured by RTC and corrected
        ppm = (1000000* (rtcdelta - dsdelta))/dsdelta
        if cal:
            verbose and print('Error {:4.1f}ppm {:4.1f}mins/year.'.format(ppm, ppm * 1.903))
            return 0
        cal = int(-ppm / 0.954)
        verbose and print('Error {:4.1f}ppm {:4.1f}mins/year. Cal factor {}'.format(ppm, ppm * 1.903, cal))
        return cal

    # Version for Pyboard D. This has μs resolution. 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:40,代碼來源:ds3231_pb.py

示例11: _getcal_d

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def _getcal_d(self, minutes, cal, verbose):
        verbose and print('Pyboard D. Waiting {} minutes for calibration factor.'.format(minutes))
        rtc.calibration(cal)  # Clear existing cal
        self.save_time()  # Set DS3231 from RTC
        self.await_transition()  # Wait for DS3231 to change: on a 1 second boundary
        t = rtc.datetime()  # Get RTC time
        # Time of DS3231 transition measured by RTC in μs since start of day
        rtc_start_us = get_us(t)
        dsstart = utime.mktime(self.convert())  # DS start time in secs

        utime.sleep(minutes * 60)

        self.await_transition()  # Wait for DS second boundary
        t = rtc.datetime()
        # Time of DS3231 transition measured by RTC in μs since start of day
        rtc_end_us = get_us(t)
        dsend = utime.mktime(self.convert()) # DS end time in secs
        if rtc_end_us < rtc_start_us:  # It's run past midnight. Assumption: run time < 1 day!
            rtc_end_us += 24 * 3_600_000_000

        dsdelta = (dsend - dsstart) * 1_000_000   # Duration (μs) between DS3231 edges as measured by DS3231
        rtcdelta = rtc_end_us - rtc_start_us  # Duration (μs) between DS edges as measured by RTC
        ppm = (1_000_000 * (rtcdelta - dsdelta)) / dsdelta
        if cal:  # We've already calibrated. Just report results.
            verbose and print('Error {:4.1f}ppm {:4.1f}mins/year.'.format(ppm, ppm * 1.903))
            return 0
        cal = int(-ppm / 0.954)
        verbose and print('Error {:4.1f}ppm {:4.1f}mins/year. Cal factor {}'.format(ppm, ppm * 1.903, cal))
        return cal 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:31,代碼來源:ds3231_pb.py

示例12: calibrate

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def calibrate(self, minutes=5):
        cal = self.getcal(minutes)
        rtc.calibration(cal)
        print('Pyboard RTC is calibrated. Factor is {}.'.format(cal))
        return cal 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:7,代碼來源:ds3231_pb.py

示例13: setup_rtc

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def setup_rtc():
    rtc = machine.RTC()
    rtc.ntp_sync("pool.ntp.org")
    while not rtc.synced():
        utime.sleep_ms(100)
    utime.timezone(3600) 
開發者ID:ayoy,項目名稱:upython-aq-monitor,代碼行數:8,代碼來源:helpers.py

示例14: now

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def now():  # Return the current time from the RTC in millisecs from year 2000
    secs = utime.time()
    ms = int(rtc.now()[6]/1000)
    if ms < 50:                                 # Might have just rolled over
        secs = utime.time()
    return 1000 * secs + ms 
開發者ID:ayoy,項目名稱:upython-aq-monitor,代碼行數:8,代碼來源:ds3231.py

示例15: delta

# 需要導入模塊: import machine [as 別名]
# 或者: from machine import RTC [as 別名]
def delta(self):                            # Return no. of mS RTC leads DS3231
        self.await_transition()
        rtc_ms = now()
        t_ds3231 = utime.mktime(self.get_time())  # To second precision, still in same sec as transition
        return rtc_ms - 1000 * t_ds3231 
開發者ID:ayoy,項目名稱:upython-aq-monitor,代碼行數:7,代碼來源:ds3231.py


注:本文中的machine.RTC屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。