# RAT_setTime.py - By: krynitskyja - Wed Jan 8 2020 import sensor, image, time, lcd from pyb import I2C sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) clock = time.clock() i2c = I2C(2) # create on bus 2 i2c = I2C(2, I2C.MASTER) # create and init as a master print(i2c) i2c.init(I2C.MASTER, baudrate=400000) # init as a master # Init LCD lcd.init() timeData = bytearray(7) # create a buffer timeData[0] = 0x00 #seconds timeData[1] = 0x48 #minutes timeData[2] = 0x12 #hours timeData[3] = 0x02 #dayofweek timeData[4] = 0x03 #dayofmonth timeData[5] = 0x02 #month timeData[6] = 0x20 #year # Find RTC i2c address i2c_addr = i2c.scan() RTC_addr = i2c_addr[0] # Write time to RTC memory i2c.mem_write(timeData, RTC_addr, 0, timeout=1000) while True: img = sensor.snapshot() #get RTC time RTC_time = i2c.mem_read(7, RTC_addr, 0) seconds = hex(RTC_time[0])[2:] minutes = hex(RTC_time[1])[2:] hours = hex(RTC_time[2])[2:] dayofweek = hex(RTC_time[3])[2:] dayofmonth = hex(RTC_time[4])[2:] month = hex(RTC_time[5])[2:] year = hex(RTC_time[6])[2:] datetime = (month + "/" + dayofmonth + "/" + year + " " + hours + ":" + minutes + ":" + seconds) print (datetime) img.draw_string(100, 119 ,"Date and time", (255,255,0)) img.draw_string(100, 129 ,"set to:", (255,255,0)) img.draw_string(100, 139 ,datetime, (255,255,255)) lcd.display(img) time.sleep(100)