Shop OBEX P1 Docs P2 Docs Learn Events
I2C read/write WORD value to 24lc256 eeprom from BS2p — Parallax Forums

I2C read/write WORD value to 24lc256 eeprom from BS2p

Evel KnievelEvel Knievel Posts: 9
edited 2009-02-12 01:45 in BASIC Stamp
Hello,

I am having trouble with the I2C command reading and writing from a BS2p to a 24LC256 eeprom. I can write a SINGLE byte to the 24LC256 and read it back, but I cannot increment beyond this original byte. ?? I need to log approimately 10000 bytes of GPS data points onto the 24LC256.


'BS2p epmWrite code:

' {$STAMP BS2p}
' {$PBASIC 2.5}
EPMio·········· PIN···· 08
EPMclk········· PIN···· 09
bite·VAR Byte
bite2·VAR Byte
address VAR Word
address = 0
bite·= 122
bite·= 62

I2CIN EPMio, $A0,$00\address, [noparse][[/noparse]bite]
address = address + 1
I2CIN EPMio, $A0,$00\address, [noparse][[/noparse]bite2]
·address = address + 1

'end epmWrite code

I then run a second program to read in the data,--that is how my final program will run. One program will·parse and write the data points to eeprom, a second program will simply read the data from the eeprom and then debug it out to the screen.


'BS2p epmRead code:

' {$STAMP BS2p}
' {$PBASIC 2.5}
EPMio·········· PIN···· 08
EPMclk········· PIN···· 09
bite·VAR Byte
bite2·VAR Byte
address VAR Word
address = 0

I2CIN EPMio, $A1,$00\address, [noparse][[/noparse]bite]
address = address + 1
I2CIN EPMio, $A1,$00\address, [noparse][[/noparse]bite2]

DEBUG DEC? bite
DEBUG DEC? bite2

'end epmRead code

The value of bite is always OK,--it is bite2 that always will read a maximum byte value of 255. I think I am not incrementing the second/slaveID address of·$00. ? I have also tried writing high/lowbytes of Words (2 bytes)·to no avail. I am trying to build a GPS data logger for a R/C boat.--I can parse the data and write it to onboard Stamp memory w/ no problem. But my project has been at a standstill for 2 days over a goofy $2 eeprom! I would appreciate any info/feedback.

Thank· you,

Karl

Comments

  • davejamesdavejames Posts: 4,047
    edited 2009-02-07 04:24
    Hi Karl,

    First - I have no experience with what you're attempting.
    Second - both code samples shown are using the I2CIN command, which reads data FROM the eeprom. Shouldn't the "BS2p epmWrite code" use the I2COUT command?

    Regards,

    DJ
  • Evel KnievelEvel Knievel Posts: 9
    edited 2009-02-07 14:58
    Good Moring DJ,

    You are right about the I2CIN command in my code snippet.--That was a mistake on my part. On the code I run it is [url=mailto:I@COUT]I2COUT[/url].--I typed the code into the forum because I couldnt get·the file·from my work machine to my Internet/email machine easily at that moment.

    I am new to the "forums" myself, I will try to fix my typo.

    Thanks for the heads up.

    Karl
  • Evel KnievelEvel Knievel Posts: 9
    edited 2009-02-07 15:02
    Here is the corrected code I am having trouble reading/writing to a 24LC256 with:

    'BS2p epmWrite code:

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    EPMio·········· PIN···· 08
    EPMclk········· PIN···· 09
    bite·VAR Byte
    bite2·VAR Byte
    address VAR Word
    address = 0
    bite·= 122
    bite·= 62

    I2COUT EPMio, $A0,$00\address, [noparse][[/noparse]bite]
    address = address + 1
    I2COUT EPMio, $A0,$00\address, [noparse][[/noparse]bite2]
    ·address = address + 1

    'end epmWrite code

    I then run a second program to read in the data,--that is how my final program will run. One program will·parse and write the data points to eeprom, a second program will simply read the data from the eeprom and then debug it out to the screen.


    'BS2p epmRead code:

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    EPMio·········· PIN···· 08
    EPMclk········· PIN···· 09
    bite·VAR Byte
    bite2·VAR Byte
    address VAR Word
    address = 0

    I2CIN EPMio, $A1,$00\address, [noparse][[/noparse]bite]
    address = address + 1
    I2CIN EPMio, $A1,$00\address, [noparse][[/noparse]bite2]

    DEBUG DEC? bite
    DEBUG DEC? bite2

    'end epmRead code

    The value of bite is always OK,--it is bite2 that always will read a maximum byte value of 255. I think I am not incrementing the second/slaveID address of·$00. ? I have also tried writing high/lowbytes of Words (2 bytes)·to no avail. I am trying to build a GPS data logger for a R/C boat.--I can parse the data and write it to onboard Stamp memory w/ no problem. But my project has been at a standstill for 2 days over a goofy $2 eeprom! I would appreciate any info/feedback.

    Thank· you,

    Karl
  • kelvin jameskelvin james Posts: 531
    edited 2009-02-07 17:32
    Try a "pause 50" between the reads to give the operation time to complete, and there is a read address incrementer already in the eeprom, it is explained in the datasheet. Depends on how you want to access the data.

    **
  • Evel KnievelEvel Knievel Posts: 9
    edited 2009-02-08 02:59
    Hello Kelvin,

    I will try the pause command later this evening. Ill bet you a dollar that is the problem. If I remember right, the datasheet said something about the latency of the read/write command also. I read·SOME of it quite some time ago.

    I think I got a little spoiled working with the GPS antennae and the DS1620. They are surprisingly easy to work with.

    After hitting the wall w/ the eeprom the other day, I have investigated the other "program slots."--Those are very handy!--and easy to work with.·I am amazed at how much performance has been wrung out of an 8bit microgadget.

    I will repost with an update. I plan on submitting my project when it is finished. I should have some pix and video.

    Thanks,

    Karl
  • davejamesdavejames Posts: 4,047
    edited 2009-02-09 17:05
    Hey Karl,

    It would appear, after perusing the 24LC256 datasheet, that Dout Valid can be as long as 3.5ms and as short as 900ns depending on the supply voltage. Din Setup falls between 250ns and 100ns, again, depending on the supply voltage.

    The question then comes down to, how long do the I2CIN/I2COUT commands take and how much delay time is required for the 24LC256.

    I've found some very useful 'time data' on Tracy Allen's site:

    http://www.emesystems.com/BS2index.htm

    Please post your result - this is something I may have to address later in my project.


    Regards,

    DJ

    Post Edited (davejames) : 2/9/2009 5:10:50 PM GMT
  • AmaralAmaral Posts: 176
    edited 2009-02-09 17:22
    The problem is in the address . the LC256 expects two bytes for it's address .. try:


    I2COUT EPMio, $A0,address.HIGHBYTE\address.LOWBYTE, [noparse][[/noparse]bite]

    and in your bite variable you can write in sequence like:

    I2COUT EPMio, $A0,address.HIGHBYTE\address.LOWBYTE, [noparse][[/noparse]bite,bite1,bite2]

    just remember that when you are reading it you have to sparce the address for reading it right !

    and the minimum time for this king of eeprom with 5V input is 4ms (I have tried even 3 ,, but the result is dubious )

    Hope to help
    Amaral
  • Evel KnievelEvel Knievel Posts: 9
    edited 2009-02-09 22:41
    Hello Amaral and DJ,

    You are both right.--I think! I have not had the time since Saturday to mess with it. I will get it running tonite or tommorrow. Someone else mentioned a pause and I am pretty sure that was the problem. I tried the highbyte\lowbyte thing w/ no results. I am sure I just needed to add approx. 5ms pause. ??

    As far as voltage is concerned, I am now running my breadboard monstrosity off a butchered wall wart that puts out 9V @ 500mA.

    When finished, I will be running it off of a 7.4V LI-? battery for weight reasons. I would like to run a 600mAh pack·for cost and weight, but I·might need a 1000mAh.·?? I just need to run a·BS2p, Grand Studio GPS antennae in raw mode,·24LC256 and a Sensiron SHT for 30 minutes.

    Any suggestions would be appreciated. I will keep you all posted, I promise. I was·sick and tired of talking/working on it a little.--I needed to take a breather.

    Thanks for the help,

    Karl
  • ZootZoot Posts: 2,227
    edited 2009-02-10 00:14
    First, you can do your read/write test in one program, for simplicity, till you get more comfortable with the I2C commands and using your EEPROM.

    Second -- do you have 4.7k pullups on BOTH the SDA and SCL lines? If you are unsure what a pullup is, please ask.

    Third -- you only need a 3ms or so pause AFTER each WRITE, and only if you are going to write again (so the previous byte can finish writing). You do not need pauses during read, and in fact, if you will be reading or writing large amounts of data you are better off using the STR and/or SPSTR modifiers to read/write huge chunks at once, but that is probably something to tackle after the first item.

    Fourth, your initialization of bite2 is non-existent, so you are probably writing a 0 there. In any case...

    Presuming you have your EEPROM hooked up correctly (address bits grounded to give $A0, pullups on SDA and SCL, 5v, common ground with Stamp), try this version of your code:

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    EPMio           PIN     08
    EPMclk          PIN     09
    ioByte VAR Byte
    address VAR Word
    addrHi  VAR address.HIGHBYTE
    addrLo  VAR address.LOWBYTE
    
    DevAddr CON $A0
    i2cWrite CON  %0
    i2cRead  CON %1
    
    DEBUG CLS
    
    address = $0000
    DEBUG "writing alphabet to address ", IHEX4 address, CR
    
    FOR ioByte = "A" TO "Z" ' write 26 ascii bytes
        I2COUT EPMio, DevAddr|i2cWrite, addrHi\addrLo, [noparse][[/noparse]ioByte]
        PAUSE 3
        DEBUG "addr:  ", IHEX4 address, "    data:", ioByte, CR
        address = address + 1
    NEXT
    
    DEBUG CR, CR "Done writing... checking now....", CR, CR
    
    FOR address = 0 TO 25  ' read 26 ascii bytes
        ioByte = "0"  'clear it, if it stays "0", then the read failed, if not zero data should be good
        I2CIN EPMio, DevAddr|i2cRead, addrHi\addrLo, [noparse][[/noparse]ioByte]
        DEBUG "addr:  ", IHEX4 address, "    data:", ioByte, CR
        address = address + 1
    NEXT
    
    END
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Evel KnievelEvel Knievel Posts: 9
    edited 2009-02-10 04:10
    Good body every evening,

    First, All hail Zoot.--I need to converse with you.

    Second, My hookups are as you specified. I am not sure what you mean by "address bits" being grounded. The pins that are grounded on my 24LC256 are: 1, 2, 3, 4 share a common ground with my BS2p. They also have a .1uF cap between these 4 grounds and the regulated +5V provided to the eeprom from the stamp. Pin 7 is left untouched/ungrounded.

    ·I think a pullup/down pulls any current out of a circuit so the Stamp can get a "clear or true" zero current/voltage reading on its pin?

    Third, you answered the question that came to my mind today as I was on my way to the supermarket.--Whether or not I would need the pause of
    ?mS for the READ as well as the WRITE.

    ·I am wary of using anything "fancy" to write the 19B datablocks·I record. I am fearful of corrupting them by mixing the order up.

    ·I need a seperate reader program to keep the finished hardware size to a minimum.--ie, no switches, etc.·My finished datalogger will have a· 3"·x·1.5" footprint and weigh 1-1.5 oz. with a battery pack.·?? What I have learned thus far is simplicity is best.

    Fourth, I am a little confused about this line in your attached code:

    ·I2COUT·EPMio,·DevAddr|i2cWrite,·addrHi\addrLo,·[noparse][[/noparse]ioByte]

    ·I interpret the | as an "OR" operand.--I would ASSume DevAddr&i2cWrite would be correct.--&

    I appreciate any feedback. I dont have any friends who are into this kinda stuff. Any·info provided is read and digested fully, I assure you.·I have bigger plans than this datalogger, but I have to walk before·I can·run.

    Karl
  • ZootZoot Posts: 2,227
    edited 2009-02-10 05:20
    Evel Knievel said...
    Second, My hookups are as you specified. I am not sure what you mean by "address bits" being grounded. The pins that are grounded on my 24LC256 are: 1, 2, 3, 4 share a common ground with my BS2p. They also have a .1uF cap between these 4 grounds and the regulated +5V provided to the eeprom from the stamp. Pin 7 is left untouched/ungrounded.

    Time to read a data sheet! The address pins (3 of them) decide if the address of the EEPROM will be $A0 (hex), $A2, etc. counting by twos (more on that below). If an address bit is tied to ground, it's a "0"; if it's tied to Vcc (5v) then it's a "1".

    And I'm glad you enumerated your pin connections -- duh, I forgot to ask earlier -- YOU MUST GROUND PIN 7 -- this is the write-protect pin. If it's not grounded, you can't write to the chip. The reason for this pin is that the user may want to "lock" the chip.
    Evel Knievel said...
    I think a pullup/down pulls any current out of a circuit so the Stamp can get a "clear or true" zero current/voltage reading on its pin?

    In this case no. The reason for the pullup is because the I2C protocol (and some serial protocols) require a line (wire) to be either "floating", or "ground". The idea is that if two devices on the same wire both send a LOW, there is no short circuit. Essentially, the micro checks it's pin as an INPUT -- if the line is HIGH, then it means no device is doing anything. If the line is LOW then it must be a device. In any case, you don't need to worry about the innards of the I2C interactions, but you *must have these pullups*. 4.7k is common; if noise is a problem you can go lower (down to 1.8k or so).
    Evel Knievel said...
    Third, you answered the question that came to my mind today as I was on my way to the supermarket.--Whether or not I would need the pause of 3mS for the READ as well as the WRITE.

    3-5 ms is fine. Any more and you're just killing unneccessary time. Between that and overhead from the Stamp instructions actually executing, you won't blow bytes. There are methods for "polling" the EEPROM to see if it's done writing, but I would keep it simple.

    You DO need a slight delay on WRITES. You need NO delay on READS, which are very fast. The Stamp doesn't even approach the limits of the I2C spec's maximum speeds, so don't worry about it.
    Evel Knievel said...
    I need a seperate reader program to keep the finished hardware size to a minimum.--ie, no switches, etc. My finished datalogger will have a 3" x 1.5" footprint and weigh 1-1.5 oz. with a battery pack. ?? What I have learned thus far is simplicity is best.

    In your final project, whatever works and is good for you is stupendous. I only meant that *for now* until you are successfully reading/writing, keep it all in one simple program so you minimize places and chances for mistakes.
    Evel Knievel said...
    I2COUT EPMio, DevAddr|i2cWrite, addrHi\addrLo, [noparse][[/noparse]ioByte]
    


    Read the documentation for I2COUT and I2CIN in the Stamp Manual! This follows the example there. Note that your address for the device will always end in an "even" number. This is because the last BIT of the address is the read/write bit -- 1 = read; 0 = write. So a device with an address of $A2 would be written at $A2 and read at $A3. Or like your setup -- written at $A0 and read at $A1. In the code above the read/write bit (as a constant) is or'ed onto the last bit. But here's a tip -- you don't need it all, actually: the I2C commands on the Stamp will automatically set BIT0 of the address to 1 for a read, and to 0 for a write. But it makes it clear what's going on. Note also the use of the address as a constant -- this way if you change addresses or have more than once device on the bus it's easy to see what's going on where.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 2/10/2009 5:28:17 AM GMT
  • Evel KnievelEvel Knievel Posts: 9
    edited 2009-02-12 01:45
    Hello Everyone,

    I have read all your feedback and I thank you for it. It seems I need to go over my connections to the 24LC256. I need to square away my address bits.--This is a relief to me because I was starting to lose faith in my trusty little stamp. I tried your prior remedies concerning a "pause" to no aval. Now I know it was a problem with my circuit more so than my code.--That is the extra kicker to working with these homemade gadgets. If I write an app for a desktop and it doesnt work, I can be pretty sure it is a problem with the code.--It is highly unlikely that there is a wire crossed or a pullup forgotten about within the desktops cabinet. Whereas in the realm of the mighty Stamp, if code of mine doesnt work it can be either the code or the circuit.--Or in my case both! Awesome!

    BUT, since I ran into trouble with the eeprom I began investigating use of the 16k of storage available in the multiple "slots" on the BS2p itself.

    I will be using the eeprom on another project, but size/weight is a concern and it is a·BIG help to·get rid of the eeprom chip + wiring from my circuit board.--Not too mention I can save $2!

    Later this evening I will post code for a hybrid state machine GPS datalogger Ive built. I am having a few problems with it thus far. I will detail them in its new posting.

    I am going to move on to that post and stop answering this one.

    Thanks for all the help. I would have applied a ball-peen hammer ever so gently to my 24LC256 without it.

    Karl
Sign In or Register to comment.