RTC Pi storing integers in RTC memory

Created 17/06/2017 | A tutorial showing how to use the internal memory for storing integers on the RTC Pi

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 using 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.

For this tutorial we will start by creating a new python program file called demo_memory_int.py. You can use your favourite text editor for writing 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 will need to import the struct library and the RTC class from the RTCPi library.

from RTCPi import RTC

We need some helper functions convert an integer into a four byte array and also for converting 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)

We now write the array to the RTC memory

rtc.write_memory(0x08, writearray)

To check that the data has saved we can now read four bytes from the RTC memory into an array

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 back to an integer you need to combine the array values into an integer and print it

print (array_to_int(read_array))

To run the demo in a console window enter:

python demo-memory-int.py

The full code for this demo is:

#!/usr/bin/env python

from RTCPi import RTC
import struct

"""
================================================
ABElectronics RTC Pi real-time clock | RTC memory demo

run with: python 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))

 

Shop

Support

Products added to basket

Items

Total (inc shipping)

CheckoutView Basket

Free Shipping on all orders over £30

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.