RTC Pi storing double values in RTC memory

Created 17/06/2017 | A tutorial showing how to use the internal memory for storing double values 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

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/ folder.

At the top of your program you will need to import the IO class from the RTCPi library and ABEHelpers library and import struct.

from ABE_RTCPi import RTC
from ABE_helpers import ABEHelpers
import struct

We need some helper functions convert an integer into a eight byte array and also for converting a eight byte array into an double

def double_to_array(val):
    # convert a double into an eight byte array
    buf = bytearray(struct.pack('d', val))
    x = [0,0,0,0,0,0,0,0]
    for i in range(0, 8):
    	x[i] = buf[i]
    return x
    
    
def array_to_double(val):
    # convert an eight byte array into a double
    dval, = struct.unpack('d', bytearray(val))
    return (dval)

Now we create an instance of the helper class and create an i2c bus object

i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()

And create a new instance of the RTC class

rtc = RTC(bus)

We will define an integer to be written to the RTC memory

value = 0.0005

And now convert the number into an array of bytes

writearray = double_to_array(value)

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 eight bytes from the RTC memory into an array

read_array = rtc.read_memory(0x08, 8)

To convert back to an double you need to combine the array values into an double and print it

print (array_to_double(read_array))

To run the demo in a console window enter:

python demo-memory-double.py

The full code for this demo is:

#!/usr/bin/python

from ABE_RTCPi import RTC
from ABE_helpers import ABEHelpers
import struct

"""
================================================
ABElectronics RTC Pi real-time clock | RTC memory demo
Version 1.0 Created 03/01/2017


run with: python demo-memory-double.py
================================================

This demo shows how to write to and read from the internal battery 
backed memory on the DS1307 RTC chip
"""



def double_to_array(val):
    # convert a double into an eight byte array
    buf = bytearray(struct.pack('d', val))
    x = [0,0,0,0,0,0,0,0]
    for i in range(0, 8):
    	x[i] = buf[i]
    return x
    
    
def array_to_double(val):
    # convert an eight byte array into a double
    dval, = struct.unpack('d', bytearray(val))
    return (dval)


# create an instance of the helper class and create an i2c bus object
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()

# create a new instance of the RTC class
rtc = RTC(bus)

# number to be written to the RTC memory
value = 0.0005

# convert the number into an array of bytes
writearray = double_to_array(value)

# write the array to the RTC memory
rtc.write_memory(0x08, writearray)

# read eight bytes from the RTC memory into an array
read_array = rtc.read_memory(0x08, 8)

# combine the array values into an number and print it
print (array_to_double(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.