Working with the AT24C32n.py package #9378
-
Hello Folks, Using the AT24C32 chip on the common DS3231 i2c module, I am trying to use a single memory location for a stored variable. Using AT24C32.py, reading a value, hexlify'ing it to an integer, to then use '+=" to increment, converting that back to hex to then write it back seems long winded. I can read and write my text string data to the EE ok, but I keep running foul of my hotch potch of using hex, integers, and different commands' syntax requirements trying to manage this single variable stored in the EE. I am sure I am tackling this the wrong way. Is there a simpler way to do this task? Perhaps there is a better package than AT24C32n.py to work with this EE chip? Could an experienced soul please give me a code snipper, or point me in the right direction to a TUT or example to get me going? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Are you talking about this one? I added some egs. to the readme: >>> import at24c32n
>>> eeprom = at24c32n.AT24C32N(i2c)
# the write() function expects bytes or a bytearray to set one or more values
# create a byte array to store a single byte (a value 0-255)
>>> buf = bytearray(1)
# lets say you want to store the integer value 70 in memory address 5.
# 70 in dec == 46 in hex == "F" in ascii.
# set the decimal integer value as the first element in the byte array
>>> buf[0] = 70
# you can also use hex
>>> buf[0] = 0x46
# you can also use bytes
>>> buf[0] = b'F'[0]
# if you hex value is in a string, you can convert it to an int like so
>>> buf[0] = int('0x46', base=16)
# write one byte starting at address 5
>>> eeprom.write(5, buf)
# read one byte from address 5
>>> eeprom.read(5, 1)
b'F'
# To make that an integer again, pluck the first byte
>>> b'F'[0]
70
# want that as hex?
>>> hex(b'F'[0])
'0x46'
>>> hex(70)
'0x46'
# If you wanted that hex value as a string without the 0x prefix
>>> '{:x}'.format(b'F'[0])
'46'
>>> '{:x}'.format(70)
'46' |
Beta Was this translation helpful? Give feedback.
-
I have tried iterations of what you have written above....... I think the problem I have had is getting from the read byte to placing it into INT to increment it, then back into a hex byte to write it back as a single byte. In your examples above you have inserted actual values into someof the syntax, which works. I need to read that value using eeprom.read, at which point it is assigned to a variable (i.e. adata), which cannot be them inserted into the INT syntax (I am saying this based on my usage and the errors thrown. As a newbie, my undestanding is that only a value can be used in "buf[0] = int('0x46', base=16)" as you have used '0x46'. A variable (like "adata") cannot be used in this syntax structure? read one byte from address 5 turn it into an INT increment that value save that back I think the above saves it in EE as a 2 byte string as "71" ? |
Beta Was this translation helpful? Give feedback.
-
I'm pretty sure the following will work:
Your mistake here is to assume that eeprom.write() can handle plain integers (hence the question of @mcauser above).
This clearly indicates that the buf argument to write() needs to be some kind of array. |
Beta Was this translation helpful? Give feedback.
I'm pretty sure the following will work:
Your mistake here is to assume that eeprom.write() can handle plain integers (hence the question of @mcauser above).
This is wrong - the code in https://github.com/mcauser/micropython-tinyrtc-i2c/blob/master/at24c32n.py reads:
This clearly indicates that the buf argument to write() needs to b…