Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ds3231 alarn
- # Alarm test using a modify version of Peter Hinch ds3231 RTC library
- # Author: Daniel Perron
- # date: August 14, 2022
- # Copyright Daniel Perron Released under the MIT license
- import sys
- from machine import Pin, I2C
- import utime
- from ds3231_port import DS3231
- #utime index dictionary
- time_idx={'year':0,'month':1,'mday':2,'hour':3,
- 'minute':4,'second':5,'weekday':6,'yearday':7}
- # set I2C
- scl_pin = Pin(17, pull=Pin.PULL_UP, mode=Pin.OPEN_DRAIN)
- sda_pin = Pin(16, pull=Pin.PULL_UP, mode=Pin.OPEN_DRAIN)
- i2c = I2C(0, scl=scl_pin, sda=sda_pin)
- #set input pin to read ds3131 interrupt
- ds3231_int_pin = Pin(14,pull=Pin.PULL_UP, mode=Pin.IN)
- # create ds3231 object
- ds3231 = DS3231(i2c)
- #disable alarm
- ds3231.enable_alarm(1,False)
- ds3231.enable_alarm(2,False)
- #get ds3231 time and set rtc
- # print RTC time (RTC was set)
- print('DS3231 time:', ds3231.get_time(set_rtc=True))
- print('RTC time: ', utime.localtime())
- print('')
- print('')
- # set alarm1 for 10 second
- now = utime.localtime()
- alarm1 = utime.localtime(utime.mktime(now)+5)
- print("set alarm1 for 5 sec")
- ds3231.set_alarm(alarm_id=1,alarm_time=alarm1,match=ds3231.ALARM_HMS_MATCH)
- print("now ",now)
- print("target",alarm1)
- print("======== step 1 sec")
- while True:
- print(utime.localtime())
- if ds3231_int_pin.value()==0:
- break
- utime.sleep(1)
- print("done")
- print("========")
- #disable alarm1
- ds3231.enable_alarm(1,False)
- print('')
- print("set alarm2 for next min start")
- # wait to get at least 2 seconds ti prevent false int
- while True:
- now = utime.localtime()
- alarm2 = utime.localtime(utime.mktime(now)+60)
- #reset second
- alarm2 = (alarm2[0], alarm2[1], alarm2[2], alarm2[3], alarm2[4], 0 , alarm2[6], alarm2[7])
- if (utime.mktime(alarm2) - utime.mktime(now)) > 2:
- break;
- #alarm2 doesnt have second
- ds3231.set_alarm(alarm_id=2,alarm_time=alarm2)
- print("now ",now)
- print("target",alarm2)
- print("======== step 5 sec")
- while True:
- if (utime.localtime()[5]%5) == 0:
- print(utime.localtime())
- if ds3231_int_pin.value()==0:
- break
- utime.sleep(1)
- print("done")
- print("========")
- ds3231.enable_alarm(2,False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement