This demo shows how to write to and read from the internal battery-backed memory on the DS1307 RTC chip as used on the RTC Pi and Expander Pi with Python.
Step 1: With your Raspberry Pi switched off, install the RTC Pi on the Raspberry Pi GPIO port and insert a CR2032 coin battery into the battery holder. Using the RTC Pi without a battery installed may damage the RTC Pi and will stop it from appearing on the I2C bus.
Step 2: Follow the instructions on how to install and configure I2C on Raspbian Linux.
We will create a new Python program file for this tutorial called demo_memory_int.py. You can use your favourite text editor to write the program. You can find a complete example of demo_memory_int.py in the ABElectronics_Python_Libraries/RTCPi/demo folder.
At the top of your program, you must import the struct library and the RTC class from the RTCPi library.
from RTCPi import RTC
We need some helper functions to convert an integer into a four-byte array and a four-byte array into an integer.
def int_to_array(val): # convert an integer into a four byte array x = [0, 0, 0, 0] x[3] = val & 0xFF val >>= 8 x[2] = val & 0xFF val >>= 8 x[1] = val & 0xFF val >>= 8 x[0] = val & 0xFF return x def array_to_int(x): # convert a four byte array into an integer val = (x[0]<<24) + (x[1]<<16) + (x[2]<<8) + x[3] return val
Create a new instance of the RTC class
rtc = RTC()
We will define an integer to be written to the RTC memory
writeval = 176247
And now, convert the integer into an array of bytes
writearray = int_to_array(writeval)
Next, write the array to the RTC memory
rtc.write_memory(0x08, writearray)
We can now read four bytes from the RTC memory into an array to check that the data has been saved.
read_array = rtc.read_memory(0x08, 4)
Print the individual array values to the console.
print (read_array[0], read_array[1], read_array[2], read_array[3])
To convert it back to an integer, you must combine the array values into an integer and print it.
print (array_to_int(read_array))
To run the demo in a console window, run the following command:
python3 demo-memory-int.py
The complete code for this demo is as follows:
#!/usr/bin/env python from RTCPi import RTC import struct """ ================================================ ABElectronics RTC Pi real-time clock | RTC memory demo run with: python3 demo_memory_int.py ================================================ This demo shows how to write to and read from the internal battery backed memory on the DS1307 RTC chip """ def int_to_array(val): # convert an integer into a four byte array x = [0, 0, 0, 0] x[3] = val & 0xFF val >>= 8 x[2] = val & 0xFF val >>= 8 x[1] = val & 0xFF val >>= 8 x[0] = val & 0xFF return x def array_to_int(x): # convert a four byte array into an integer val = (x[0]<<24) + (x[1]<<16) + (x[2]<<8) + x[3] return val # create a new instance of the RTC class rtc = RTC() # integer to be written to the RTC memory writeval = 176247 # convert the integer into an array of bytes writearray = int_to_array(writeval) # write the array to the RTC memory rtc.write_memory(0x08, writearray) # read four bytes from the RTC memory into an array read_array = rtc.read_memory(0x08, 4) # print the individual array values print(read_array[0], read_array[1], read_array[2], read_array[3]) # combine the array values into an integer and print it print(array_to_int(read_array))