I2C Code for the BS2, BS2e, and BS2sx
Jon Williams
Posts: 6,491
As there seems to be growing interest in using I2C parts with non-BS2p/BS2pe modules, I spent the day updating my core I2C routines for the BS2, BS2e, and the BS2sx.· Above the low-level routines I have a Write_Byte subroutine and a Read_Byte subroutine that should work with any I2C device you throw at it -- but I can't guarantee that (if you have a problem with a particular part, please let me know).· These subroutines only write or read one byte at a time; if you want to do block writes or reads you'll have to add your own code (it shouldn't be too tough).
Now,·if you're not familiar with I2C please do a bit of research --·there is plenty of information on the Net.· I wrote an article for Nuts & Volts that might help you:
http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv85.pdf
Understand, though, that the code attached here is updated using PBASIC 2.5 syntax.· It's also more flexible in that you can specify the slave ID as a variable, the device address as a variable (in case you have more than one of the same device on the line), as well as the number of address bytes for the register you want to read or write.· In addition to the core program, I have three demos, each demonstrating different address byte values:
PCF8574A - no address byte
MCP23016 - 1 address byte
24LC32 - 2 address bytes
And yes, I connected and ran all three demos on my trusty NX-1000 board.
I find this code helpful for my own projects, I hope it's helpful for yours as well.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Post Edited (Jon Williams) : 8/19/2004 2:54:06 AM GMT
Now,·if you're not familiar with I2C please do a bit of research --·there is plenty of information on the Net.· I wrote an article for Nuts & Volts that might help you:
http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv85.pdf
Understand, though, that the code attached here is updated using PBASIC 2.5 syntax.· It's also more flexible in that you can specify the slave ID as a variable, the device address as a variable (in case you have more than one of the same device on the line), as well as the number of address bytes for the register you want to read or write.· In addition to the core program, I have three demos, each demonstrating different address byte values:
PCF8574A - no address byte
MCP23016 - 1 address byte
24LC32 - 2 address bytes
And yes, I connected and ran all three demos on my trusty NX-1000 board.
I find this code helpful for my own projects, I hope it's helpful for yours as well.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Post Edited (Jon Williams) : 8/19/2004 2:54:06 AM GMT
Comments
Please note that I wrote this code to help a customer and don't have a 24LC515 to test with.· If you find a problem, please let me know.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
The info is just in time because i do have problems with I2C. Mostly with eeproms as you know.
For my previous post about eeprom i understood that is a two bytes address for the 16k . The addressing is still the same for the 4 k memory?
Acording with Ramtron data sheet for the 4k memory, the address is made by 8 bits for the loction on the page (they are only two pages), and one bit for the page , which make 9 bits== 512 , locations to write or read. Do I have to use the same sample comand you posted before , or just a plain command. I know the question is silly and maybe obvious for you, but for me is still an unsolved mistery. Maybe a sugestion for Parallax to change the code for the comand in a such way that the user just put the phisical address number , with no wory about high and low bit , let say address 1097 and the code does all the house keeping in behind the curtains.
I will go now to read your article on NV and maybe i· can go to sleep if i find my answer.
Regards
Ion
In terms of data , how much is a 4KByte eeprom capable of storing ???
I mean for example Date,Time,and 2 numerical values for each write ??
Thank you
i am completly new at that but reading the data sheet for a 4k memory i found out that:
The FM24C04A is a serial FRAM memory. The
memory array is logically organized as 512 x 8 and is
accessed using an industry standard two-wire
interface. Functional operation of the FRAM is
similar to serial EEPROMs.
When accessing the FM24C04A, the user addresses
512 locations each with 8 data bits. These data bits
are shifted serially. The 512 addresses are accessed
using the two-wire protocol, which includes a slave
address (to distinguish other devices), a page address,
and a word address. The word address consists of 8-
bits that specify one of 256 addresses. The page
address is 1-bit and so there are 2 pages each of 256
locations. The complete address of 9-bits specifies
each byte address uniquely.
Assuming that you want to store one byte for days,one for month,one for year,one for seconds,one for minutes ,and one for hours in a 24 hours format will use 6 bytes of the 512 on the chip. So you can store up to 85 logs until you run out of memory and have to rolover. Dependig how often you log data will give you an ideea how much you can store.
I hope that this is the answer for your question. Here is the link for the data sheet of this memory. I use it because they claim that you can write for life with no limitation of write cycle like a conventional eeprom. I do a write every 5 seconds and i need it to work for 5 years 24/7 so this is my first choice for eeprom over nvram or standard eeprom.
http://www.ramtron.com/doc/Products/serial.asp
Ion
ion
24LC04 - 4 kBits (512 bytes, organized as 2 blocks of 256 bytes) --·the block is specified in the Slave ID
24LC16 - 16 kBits (2 kBytes, organized as 8 blocks of 256 bytes) --·the block is specified in the Slave ID
24LC32 - 32 kBits (4 kBytes)
24LC256 - 256 kBits (32 kBytes)
24LC515 - 512 kBits (64 kBytes, organized as 2 blocks of 32 kBytes) --·the block is specified in the Slave ID
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Post Edited (Jon Williams) : 8/19/2004 2:44:38 PM GMT
Thank you ion , a nice and clean explanation.
Thank you Jon ,..... I was suspicious of that NOT beeing 4 KBytes but 4K Bits. A small difference , huhhh !!!
nice guys you both .... man there is SO MUCH to learn ....
Francisco
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
· P.S.· I do want to program my stamp II.· I just wanted to learn how to braoden it as well.· That is why I have asked so many questions about the "outside" components.· I am sorry if I have annoyed anyone here.· [noparse]/noparse][noparse]:([/noparse
In just two seconds I found the spec sheet on the Internet for the 25LC640 -- it is NOT an I2C device; it is an SPI device which means that you can use SHIFTOUT and SHIFTIN to deal with it. First, though, you should probably spend some time learning the basics of the BASIC Stamp. Start with "What's A Microcontroller?" and then give "StampWorks" (written by me) a scan. If you'll just give yourself a little training time you will be an old pro in a matter of a few weeks.
PS: Do you actually speak like Elmer Fudd too, or is it just in the writing?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Post Edited (Jon Williams) : 8/19/2004 5:46:51 AM GMT
Post Edited (Velvet Leopard) : 8/19/2004 7:20:08 AM GMT
ALL books publshed by Parallax are available as PDF downloads (no charge).· Here are links to books that will help:
http://www.parallax.com/html_pages/edu/curriculum/sic_curriculum.asp·(Stamps In Class)
http://www.parallax.com/detail.asp?product_id=27220·(StampWorks)
And you can get to our Nuts & Volts articles (over 100) here:
http://www.parallax.com/html_pages/downloads/nvcolumns/Nuts_Volts_Downloads.asp
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
I did post·yesterday a question about 4 k memory and maybe it did passed unread. I will put it again and ask you kindly to help me on this mather. Here it is:
"For my previous post about eeprom i understood that is a two bytes address for the 16k . The addressing is still the same for the 4 k memory?
Acording with Ramtron data sheet for the 4k memory, the address is made by 8 bits for the loction on the page (they are only two pages), and one bit for the page , which make 9 bits== 512 , locations to write or read. Do I have to use the same sample command you posted before , or just a plain command like direct addresing ?
Here is the info posted by Ramtron about their memory:
The FM24C04A is a serial FRAM memory. The
memory array is logically organized as 512 x 8 and is
accessed using an industry standard two-wire
interface. Functional operation of the FRAM is
similar to serial EEPROMs.
When accessing the FM24C04A, the user addresses
512 locations each with 8 data bits. These data bits
are shifted serially. The 512 addresses are accessed
using the two-wire protocol, which includes a slave
address (to distinguish other devices), a page address,
and a word address. The word address consists of 8-
bits that specify one of 256 addresses. The page
address is 1-bit and so there are 2 pages each of 256
locations. The complete address of 9-bits specifies
each byte address uniquely.
9 bits is biger then one byte, so how i can write and read to 10, and what is the diference to write and read to 410?
Thank you
ion
With the BS2p or BS2pe, I would tend to do something like this (written on-the-fly -- not tested):
If this works the way I intend, the string "BASIC Stamp" will be written to the EEPROM and then read back one character at a time.· When using this device, though, you must be careful·with the 16-byte page boundaries.· If you're writing/reading a single byte to/from the device you won't have any troubles with the page boundaries or block bit using the addressing demonstrated above.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Post Edited (Jon Williams) : 8/19/2004 3:47:10 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
It did worked as expected. Like you said many times, READ THE MANUAL is a gold rule. After reading the addressing for 4k chip I found out that for page 0 we must use $A0
and for page 1 is $A2. After all this troubles I will not forget it so easy. I am sorry for all the nagging to get the answer, but I am new to this and I have a lot to learn.
The slave ID for chip is as follow:
1010·· A2A1 for device select,· A0 for page select, and the last is R/W bit
Thank you
Ion
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Ion
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Here're the deal: We can't do the housekeeping hehind the scenes because of the way different I2C devices are addressed.· Take the 24LC256 for example.· It uses two address bytes.· What if we told it to write to address 0?· How would the compiler know to send one byte or two?· It's not possible unless we create specialized commands for the various addressing schemes of I2C devices -- and remember that some devices (like your 24LC04) want address bit(s) included in the Slave ID byte.
If you study the I2C bit timing diagrams that are included in most I2C component data sheets this will make sense.
Here's the syntax for I2COUT (identical for I2CIN):
· I2COUT pin, slaveID, {address {\lowAddress},} [noparse][[/noparse]output_data]
You see the curly braces?· These mean the elements enclosed are optional.· So, valid I2COUT commands could look like this:
I hope this additional information helps.· Keep studying the examples, the help file, and the product data sheets and it will all come together!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Post Edited (Jon Williams) : 8/19/2004 4:47:05 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
You are right. I believe that after more then 24 hours around the memory subject it will be time to move on.
I hope that all the readers of this thread ( and they are a lot) had a lot to get as info and demo programs.
Thanks again and see you soon with some other questions.
By the way , i read all the posting in this forum and some of my questions are already answered.
Ion
What the hell is a fur? And where would we be if everybody just changed the spelling on words as a way to manifest their personality?
www.atmel.com/dyn/resources/prod_documents/doc3409.pdf
Any help would be greatly appreciated.
Pete
slvAddr = %11111111 ? by the way I am trying to use the I2C essentials posted earlier on this form.
Thanks a bunch for the help.