Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Micropython
#3
I have some Micropython code working with the KC868-A6.  For some reason the clock can be set and the time read back fine with Micropython when it can't be set reliably with any of the Arduino code I've tested on this board.

This is a simple get/set time script. I tried various other more common get/set time methods without success but this one works well. To set the time remove the # before this line in the code below  -  #await set_time(i2c, year, month, day, hour, minute, second), then enter the current time in the section below this (# Set initial time using variables) and run the script. To read the time replace the # and run the script. 


Code:
from machine import Pin, SoftI2C
import time
import uasyncio as asyncio  # Use uasyncio for MicroPython

# DS1307 I2C address
DS1307_ADDRESS = 0x68

def bcd_to_decimal(bcd):
    return (bcd >> 4) * 10 + (bcd & 0x0F)

def decimal_to_bcd(decimal):
    return ((decimal // 10) << 4) | (decimal % 10)

async def safe_writeto_mem(i2c, address, mem_address, data):
    try:
        i2c.writeto_mem(address, mem_address, bytes(data))
    except Exception as e:
        print("Error writing to I2C:", e)

async def safe_readfrom_mem(i2c, address, mem_address, num_bytes):
    try:
        return i2c.readfrom_mem(address, mem_address, num_bytes)
    except Exception as e:
        print("Error reading from I2C:", e)
        return None

async def set_time(i2c, year, month, day, hour, minute, second):
    weekday = (time.mktime((year, month, day, hour, minute, second, 0, 0, -1)) // 86400) % 7

    data = [
        decimal_to_bcd(second),
        decimal_to_bcd(minute),
        decimal_to_bcd(hour),
        decimal_to_bcd(weekday),
        decimal_to_bcd(day),
        decimal_to_bcd(month),
        decimal_to_bcd(year - 2000)
    ]
   
    await safe_writeto_mem(i2c, DS1307_ADDRESS, 0x00, data)
   
    control = await safe_readfrom_mem(i2c, DS1307_ADDRESS, 0x00, 1)
    if control is not None:
        control = control[0] & 0x7F
        await safe_writeto_mem(i2c, DS1307_ADDRESS, 0x00, [control])

async def get_time(i2c):
    data = await safe_readfrom_mem(i2c, DS1307_ADDRESS, 0x00, 7)
    if data is not None:
        second = bcd_to_decimal(data[0] & 0x7F)
        minute = bcd_to_decimal(data[1])
        hour = bcd_to_decimal(data[2] & 0x3F)
        day = bcd_to_decimal(data[4])
        month = bcd_to_decimal(data[5])
        year = bcd_to_decimal(data[6]) + 2000
        return year, month, day, hour, minute, second
    return None, None, None, None, None, None

# Initialize SoftI2C
scl_pin = Pin(15)
sda_pin = Pin(4)
i2c = SoftI2C(scl=scl_pin, sda=sda_pin, freq=100000)

# Set initial time using variables
year = 2025
month = 3
day = 16
hour = 9
minute = 26
second = 6

async def main():
    global year, month, day, hour, minute, second  # Declare as global
    # Set the time asynchronously
    #await set_time(i2c, year, month, day, hour, minute, second)

    # Loop to read time asynchronously
    while True:
        year, month, day, hour, minute, second = await get_time(i2c)
        if year is not None:
            print(f"Time: {hour:02}:{minute:02}:{second:02}, Date: {year:04}/{month:02}/{day:02}")
        await asyncio.sleep(1)

# Run the main loop
asyncio.run(main())
Reply


Messages In This Thread
Micropython - by ghazidrira - 11-03-2023, 09:04 AM
RE: Micropython - by admin - 11-03-2023, 11:13 AM
RE: Micropython - by Shadowboxer - 03-15-2025, 10:45 PM
RE: Micropython - by admin - 03-15-2025, 11:20 PM
RE: Micropython - by Shadowboxer - 03-15-2025, 11:26 PM
RE: Micropython - by Shadowboxer - 03-16-2025, 05:28 AM
RE: Micropython - by admin - 03-16-2025, 08:00 AM
RE: Micropython - by Shadowboxer - 03-16-2025, 05:55 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)