Created
October 25, 2021 12:38
-
-
Save CloudyPadmal/c544e41960af46a741e6178e7025f09d to your computer and use it in GitHub Desktop.
DS1307 Real Time Clock I2C module time setting and fetching using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def DS1307_Set_Time(): | |
from datetime import datetime | |
now=datetime.now() | |
seconds = int(now.second) | |
sec_1 = '{0:04b}'.format(seconds % 10) | |
sec_2 = '{0:04b}'.format(int(seconds / 10)) | |
b_seconds = sec_2 + sec_1 | |
minutes = int(now.minute) | |
min_1 = '{0:04b}'.format(minutes % 10) | |
min_2 = '{0:04b}'.format(int(minutes / 10)) | |
b_minutes = min_2 + min_1 | |
hours = int(now.hour) | |
hour_1 = '{0:04b}'.format(hours % 10) | |
hour_2 = '{0:02b}'.format(int(hours / 10)) | |
b_hours = '01' + hour_2 + hour_1 # 24 hour format | |
b_day = '{0:08b}'.format(now.isoweekday()) | |
date = int(now.day) | |
date_1 = '{0:04b}'.format(date % 10) | |
date_2 = '{0:02b}'.format(int(date / 10)) | |
b_date = '00' + date_2 + date_1 | |
month = int(now.month) | |
month_1 = '{0:04b}'.format(month % 10) | |
month_2 = '{0:01b}'.format(int(month / 10)) | |
b_month = '000' + month_2 + month_1 | |
year = int(str(now.year)[2:]) | |
year_1 = '{0:04b}'.format(year % 10) | |
year_2 = '{0:01b}'.format(int(year / 10)) | |
b_year = '000' + year_2 + year_1 | |
self._device.send_byte(CP.SENSORS) | |
self._device.send_byte(CP.SET_TIME) | |
self._device.send_byte(Byte.pack(int(b_seconds, 2))) | |
self._device.send_byte(Byte.pack(int(b_minutes, 2))) | |
self._device.send_byte(Byte.pack(int(b_hours, 2))) | |
self._device.send_byte(Byte.pack(int(b_day, 2))) | |
self._device.send_byte(Byte.pack(int(b_date, 2))) | |
self._device.send_byte(Byte.pack(int(b_month, 2))) | |
self._device.send_byte(Byte.pack(int(b_year, 2))) | |
self._device.send_byte(Byte.pack(128)) | |
status = self._device.get_ack() | |
if (status == 1): | |
print("Date set") | |
else: | |
print("Error") | |
def DS1307_Get_Time(): | |
self._device.send_byte(CP.SENSORS) | |
self._device.send_byte(CP.GET_TIME) | |
A = [] | |
for i in range(7): | |
A.append('{0:08b}'.format((self._device.get_byte()))) | |
self._device.get_ack() | |
# Seconds | |
sec_1 = A[0][4:] | |
sec_2 = A[0][:4] | |
seconds = str(int(sec_2, 2)) + '' + str(int(sec_1, 2)) | |
# Minutes | |
min_1 = A[1][4:] | |
min_2 = A[1][:4] | |
minutes = str(int(min_2, 2)) + '' + str(int(min_1, 2)) | |
# Hours | |
hr_1 = A[2][4:] | |
hr_2 = A[2][3] | |
hours = str(int(hr_2, 2)) + '' + str(int(hr_1, 2)) | |
# Day of week | |
import calendar | |
day = str(int(A[3][5:], 2) - 1) | |
day = calendar.day_name[int(day)] | |
# Date | |
date_1 = A[4][4:] | |
date_2 = A[4][2:4] | |
date = str(int(date_2, 2)) + '' + str(int(date_1, 2)) | |
# Month | |
month_1 = A[5][4:] | |
month_2 = A[5][3] | |
month = str(int(month_2, 2)) + '' + str(int(month_1, 2)) | |
# Year | |
year_1 = A[6][4:] | |
year_2 = A[6][:4] | |
year = str(int(year_2, 2)) + '' + str(int(year_1, 2)) | |
K = str(year + '/' + month + '/' + date + ' (' + day + ') ' + hours + ':' + minutes + ':' + seconds) | |
print(K) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment