The code below has worked on a set of Raspberry Pi 2B single board computers in my workshop for some years now, each of them using a local real time clock, a DS1307. (Internet access is inconveniently far away.) The most recent version among these is Python 3.7.3
Now I want to improve the accuracy by changing to the adjustable MCP7940N, and want to test the programming using a Pi 4 running the latest linux distro and Python 3.11.2 before changing the code in the instruments. Of course, the address was changed to suit the new RTC. However, this has thrown up a problem:
I have run out of ideas, and would be most grateful for advice. I apologise that the code snippet above isn't in BBCode form - I couldn't see how to get to that - the info on it seemed to suggest that I would be pointed to that format anyway, but.... Incidentally, I typed it in with the correct indents, but they seem to have vanished.
Thanks for reading this far! NewbyTyro
Now I want to improve the accuracy by changing to the adjustable MCP7940N, and want to test the programming using a Pi 4 running the latest linux distro and Python 3.11.2 before changing the code in the instruments. Of course, the address was changed to suit the new RTC. However, this has thrown up a problem:
#code to write data to a real time clock e.g. DS1307: import smbus bus = smbus.SMBus(1) address = 0x68 def set_clk(): global bus global address prmts = ['Year (0 - 99) = ? ', 'Month (1 - 12) = ? ', 'Date (1 - 31) = ? ', 'Day (1 - 7) = ? ', 'Hour (1 - 24) = ? ', 'Minute (0 - 59) = ? 'Second (0 - 59) = ? '] vars = ['yr2', 'mon2', 'date2', 'day2', 'hr2', 'min2', 'sec2'] for i in range(0,7): val = input(prmts[i]) val = int(val) hi = int(val/10) lo = val - 10*hi val = 16*hi + lo vars[i] = val bus.write_byte_data(address, 0x00, vars[6]) bus.write_byte_data(address, 0x00, vars[5]) bus.write_byte_data(address, 0x00, vars[4]) bus.write_byte_data(address, 0x00, vars[3]) bus.write_byte_data(address, 0x00, vars[2]) bus.write_byte_data(address, 0x00, vars[1]) bus.write_byte_data(address, 0x00, vars[0])This code on Python3.11.2, gives two errors: the first requires the removal of the (1) in the bus = smbus.SMBus(1) line. OK, so we can only have one I2C bus at a time now. The second is more of a problem:
Error:Traceback (most recent call last)
File "/home/pi/set_clock.py, line 15, in<module>
bus.write_byte_data(address, 0x00, vars[6])
TypeError: descriptor 'write_byte_data for 'SMBus' objects doesn't apply to a 'int' object
I have tried the write_i2c_block_data( , , ) form of the command (same error as above). and many other imagined possibilities, none of which was accepted. I have downloaded the language reference, but 'SMBus' is nowhere to be found in the 2,200 pages - and the data types are said to be restricted to 'int' and 'str' - no 'byte' data type. I have tried putting in explicit binary values in the bus.write_byte_data() command, but everything I have tried is an 'int'.I have run out of ideas, and would be most grateful for advice. I apologise that the code snippet above isn't in BBCode form - I couldn't see how to get to that - the info on it seemed to suggest that I would be pointed to that format anyway, but.... Incidentally, I typed it in with the correct indents, but they seem to have vanished.
Thanks for reading this far! NewbyTyro