<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: SPI EEPROM 24LC640A status report (working!)</title>
		<link>http://forums.leaflabs.com/topic.php?id=694</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:13:03 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=694" rel="self" type="application/rss+xml" />

		<item>
			<title>Adam on "SPI EEPROM 24LC640A status report (working!)"</title>
			<link>http://forums.leaflabs.com/topic.php?id=694#post-3979</link>
			<pubDate>Sun, 20 Mar 2011 21:01:45 +0000</pubDate>
			<dc:creator>Adam</dc:creator>
			<guid isPermaLink="false">3979@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Folks,&#60;/p&#62;
&#60;p&#62;I got some proof-of-concept code for a Microchip 24LC640A 64kb SPI EEPROM working today.&#60;/p&#62;
&#60;p&#62;Here is a picture of it on a Sparkfun proto shield (you can't see the Maple, but it is underneath the shield):&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://imgur.com/XvrxV&#34; rel=&#34;nofollow&#34;&#62;http://imgur.com/XvrxV&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;Here is the code:&#60;/p&#62;
&#60;p&#62;&#60;code&#62;&#60;br /&#62;
// EEPROM SPI 25LC640&#60;br /&#62;
// Demonstrates reading and writing to the EEPROM.&#60;br /&#62;
// For the Leaf Labs Maple platform.&#60;br /&#62;
//&#60;br /&#62;
// Writes a single page (32 bytes),&#60;br /&#62;
// then reads it back, printing each byte.&#60;br /&#62;
//&#60;br /&#62;
// Based on code by Heather Dewey-Hagborg&#60;br /&#62;
// &#60;a href=&#34;http://arduino.cc/en/Tutorial/SPIEEPROM&#34; rel=&#34;nofollow&#34;&#62;http://arduino.cc/en/Tutorial/SPIEEPROM&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;#define PAGE_SIZE 32&#60;br /&#62;
#define SPI_MODE 0&#60;br /&#62;
#define CS 10 // chip select &#60;/p&#62;
&#60;p&#62;// EEPROM opcodes&#60;br /&#62;
#define WREN  6&#60;br /&#62;
#define WRDI  4&#60;br /&#62;
#define RDSR  5&#60;br /&#62;
#define WRSR  1&#60;br /&#62;
#define READ  3&#60;br /&#62;
#define WRITE 2&#60;/p&#62;
&#60;p&#62;// Use SPI 1:&#60;br /&#62;
//   Pin D11 MOSI&#60;br /&#62;
//   Pin D12 MISO&#60;br /&#62;
//   Pin D13 sck&#60;br /&#62;
//   Pin D10 ss&#60;/p&#62;
&#60;p&#62;HardwareSPI Spi(1);&#60;/p&#62;
&#60;p&#62;byte eeprom_output_data;&#60;br /&#62;
byte eeprom_input_data=0;&#60;br /&#62;
int address=0;&#60;/p&#62;
&#60;p&#62;// data buffer&#60;br /&#62;
uint8 buffer [PAGE_SIZE];&#60;/p&#62;
&#60;p&#62;void fill_buffer() {&#60;br /&#62;
  for (int i=0;i&#38;lt;PAGE_SIZE;i++) {&#60;br /&#62;
    buffer[i]=i;&#60;br /&#62;
  }&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void setup() {&#60;br /&#62;
  SerialUSB.begin();&#60;br /&#62;
  SerialUSB.println(&#34;starting...&#34;);&#60;/p&#62;
&#60;p&#62;  pinMode(CS, OUTPUT);&#60;br /&#62;
  digitalWrite(CS, HIGH); // disable&#60;/p&#62;
&#60;p&#62;  Spi.begin(SPI_9MHZ, MSBFIRST, SPI_MODE);&#60;/p&#62;
&#60;p&#62;  // fill buffer with data&#60;br /&#62;
  fill_buffer();&#60;/p&#62;
&#60;p&#62;  // fill eeprom w/ buffer&#60;br /&#62;
  digitalWrite(CS, LOW); // EEPROM enable&#60;br /&#62;
  Spi.send(WREN); // EEPROM write enable instruction&#60;br /&#62;
  digitalWrite(CS, HIGH); // EEPROM disable&#60;br /&#62;
  delay(10);&#60;br /&#62;
  digitalWrite(CS, LOW); // EEPROM enable&#60;br /&#62;
  Spi.send(WRITE); // EEPROM write instruction&#60;br /&#62;
  address=0;&#60;br /&#62;
  Spi.send((uint8)(address&#38;gt;&#38;gt;8));  //send MSByte address first&#60;br /&#62;
  Spi.send((uint8)(address)); //send LSByte address&#60;/p&#62;
&#60;p&#62;  // write PAGE_SIZE bytes&#60;br /&#62;
  for (int i=0;i&#38;lt;PAGE_SIZE;i++) {&#60;br /&#62;
    Spi.send(buffer[i]); // EEPROM write data byte&#60;br /&#62;
  }&#60;br /&#62;
  digitalWrite(CS, HIGH); // EEPROM disable&#60;/p&#62;
&#60;p&#62;  //wait for eeprom to finish writing&#60;br /&#62;
  delay(3000);&#60;br /&#62;
  SerialUSB.println(&#34;write finished.&#34;);&#60;br /&#62;
  delay(1000);&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;byte read_eeprom(int EEPROM_address) {&#60;br /&#62;
  //READ EEPROM&#60;br /&#62;
  int data;&#60;br /&#62;
  digitalWrite(CS, LOW); // EEPROM enable&#60;br /&#62;
  Spi.send(READ); //transmit read opcode&#60;br /&#62;
  Spi.send((uint8)(EEPROM_address&#38;gt;&#38;gt;8));   //send MSByte address first&#60;br /&#62;
  Spi.send((uint8)(EEPROM_address));      //send LSByte address&#60;br /&#62;
  data = Spi.send(0xFF); //get data byte&#60;br /&#62;
  digitalWrite(CS, HIGH); // EEPROM disable&#60;br /&#62;
  return data;&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void loop() {&#60;br /&#62;
  eeprom_output_data = read_eeprom(address);&#60;br /&#62;
  SerialUSB.print((int) eeprom_output_data);&#60;br /&#62;
  SerialUSB.println();&#60;br /&#62;
  address++;&#60;br /&#62;
  if (address == PAGE_SIZE)&#60;br /&#62;
    address = 0;&#60;br /&#62;
  delay(500); // pause for readability&#60;br /&#62;
}&#60;br /&#62;
&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;It's based off this example on the Arduino website:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://arduino.cc/en/Tutorial/SPIEEPROM&#34; rel=&#34;nofollow&#34;&#62;http://arduino.cc/en/Tutorial/SPIEEPROM&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;The circuit is exactly as described in the article.&#60;/p&#62;
&#60;p&#62;Here's a link to the Octopart page, with datasheet and pricing:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://octopart.com/25lc640a-i%2Fp-microchip-7721511&#34; rel=&#34;nofollow&#34;&#62;http://octopart.com/25lc640a-i%2Fp-microchip-7721511&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;I will eventually turn this into a library; I will let you know when I get that done.
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
