Parma/Salento
Offline
Jr. Member
Karma: 0
Posts: 83
Arduino rocks
|
 |
« on: June 23, 2010, 03:12:59 pm » |
Ciao a tutti, vorrei iniziare ad utilizzare una EEPROM 24WC16P (recuperata da vecchi progetti) con Arduino, utilizzando la libreria Wire.h.
Ho cercato un pò in rete (e anche nel forum) ma non ho trovato molto (soprattutto in italiano). In particolare si trovano solo esempi riguardanti EEPROM più grandi (256).
C'è qualcuno che ci ha già provato?
|
|
|
|
|
Logged
|
|
|
|
|
cuneo
Offline
God Member
Karma: 3
Posts: 777
|
 |
« Reply #1 on: June 23, 2010, 03:39:39 pm » |
funziona allo stesso modo solo ricorda che hai meno indirizzi... lo sketch scritto sul playground funziona ma ti ripeto che devi ridurre gli indirizzi calcoli quanta memoria ha, e converti in byte ottenedo il numero di indirizzi che contiene ps ricorda che il primo indirizzo è 0 non 1 quindi non fiirà ad esempio in 16(memoria da 16byte) ma con 15  ciao
|
|
|
|
|
Logged
|
|
|
|
|
BZ (I)
Offline
Brattain Member
Karma: 162
Posts: 15752
+39 349 2158303
|
 |
« Reply #2 on: June 23, 2010, 05:18:18 pm » |
|
|
|
|
« Last Edit: June 23, 2010, 05:18:49 pm by uwefed »
|
Logged
|
|
|
|
|
Parma/Salento
Offline
Jr. Member
Karma: 0
Posts: 83
Arduino rocks
|
 |
« Reply #3 on: June 24, 2010, 04:19:43 am » |
Ok, intanto ringrazio entrambi per le celeri risposte!  Ho connesso la EEPROM secondo il seguente schema: Pin 1 -> GND (Arduino) Pin 2 -> GND (Arduino) Pin 3 -> GND (Arduino) Pin 4 -> GND (Arduino) Pin 8 -> 5 V (Arduino) Pin 7 -> GND (Arduino) Pin 6 -> Analog Pin 5 (Arduino) Pin 5 -> Analog Pin 4 (Arduino)Tuttavia, inserendo il seguente codice: #include <Wire.h> #include <LiquidCrystal.h>
#define EEPROM_ADDRESS 0x50 LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) { int rdata = data; Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddress >> 8)); // MSB Wire.send((int)(eeaddress & 0xFF)); // LSB Wire.send(rdata); Wire.endTransmission(); }
// WARNING: address is a page address, 6-bit end will wrap around // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) { Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddresspage >> 8)); // MSB Wire.send((int)(eeaddresspage & 0xFF)); // LSB byte c; for ( c = 0; c < length; c++) Wire.send(data[c]); Wire.endTransmission(); }
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) { byte rdata = 0xFF; Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddress >> 8)); // MSB Wire.send((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission(); Wire.requestFrom(deviceaddress,1); if (Wire.available()) rdata = Wire.receive(); return rdata; }
// maybe let's not read more than 30 or 32 bytes at a time! void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) { Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddress >> 8)); // MSB Wire.send((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission(); Wire.requestFrom(deviceaddress,length); int c = 0; for ( c = 0; c < length; c++ ) if (Wire.available()) buffer[c] = Wire.receive(); }
int temp = 0;
void setup(void) { lcd.begin(20, 4); lcd.clear(); lcd.print("EEPROM Test"); Wire.begin(); i2c_eeprom_write_byte( EEPROM_ADDRESS, 0, 200 ); delay(20); }
void loop(void) { temp = i2c_eeprom_read_byte( EEPROM_ADDRESS, 0 ); lcd.setCursor(0,1); lcd.print(temp); delay(8000); }
sul display appare sempre: EEPROM TEST 255
Cosa sbaglio?
|
|
|
|
« Last Edit: June 24, 2010, 11:36:57 am by 83darking83 »
|
Logged
|
|
|
|
|
BZ (I)
Offline
Brattain Member
Karma: 162
Posts: 15752
+39 349 2158303
|
 |
« Reply #4 on: June 24, 2010, 06:47:26 am » |
Se mi chiedi cosi... :-? leggi bene http://arduino.cc/en/Reference/Wiredove é scritto: This library allows you to communicate with I2C / TWI devices. On most Arduino boards, SDA (data line) is on analog input pin 4, and SCL (clock line) is on analog input pin 5. Ciao Uwe
|
|
|
|
|
Logged
|
|
|
|
|
Parma/Salento
Offline
Jr. Member
Karma: 0
Posts: 83
Arduino rocks
|
 |
« Reply #5 on: June 24, 2010, 07:32:26 am » |
Avevo dato per scontato che 4 e 5 erano analog pin... :o
Comunque modifico il mio post precedente, onde evitare confusioni...
|
|
|
|
« Last Edit: June 24, 2010, 07:34:11 am by 83darking83 »
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 181
Arduino rocks
|
 |
« Reply #6 on: June 24, 2010, 07:37:55 am » |
Quindi i pin 4 e 5 analogici funzionano anche in uscita con una comunicazione I2C
|
|
|
|
|
Logged
|
|
|
|
|
BZ (I)
Offline
Brattain Member
Karma: 162
Posts: 15752
+39 349 2158303
|
 |
« Reply #7 on: June 24, 2010, 08:36:34 am » |
ciao 83darking83 "Avevo dato per scontato che 4 e 5 erano analog pin... " allora é giusto. Servono resistenze di pullup sui SDA e SCL. La libreria WIRE.H mette quelle interne e per cavetti corti non serve aggiungere altre sesistenze (Wire library Wire library will enable ATMEL internal Pullup's. So using additional pullup's with a short bus may not be required, for example interface only a single nearby EEPROM. ) c'é anche la libreria http://www.arduino.cc/en/Reference/EEPROMo http://www.arduino.cc/playground/Main/InterfacingWithHardware#StorageRicontrollando mi sembra che hai collegato la EEPROM sbagliato. EEprom Pin 1 (A0) -> GND (Arduino) Pin 2 (A1) -> GND (Arduino) Pin 3 (A2) -> GND (Arduino) Pin 4 (GND) -> GND (Arduino) Pin 5 (SDA) -> Analog Pin 4 (Arduino)Pin 6 (SCL) -> Analog Pin 5 (Arduino)Pin 7 (WP) -> GND (Arduino)Pin 8 (Vpp) -> 5 V (Arduino)http://pdf1.alldatasheet.com/datasheet-pdf/view/57845/CATALYST/CAT24WC16P-1.8TE13D.htmlCiao Uwe
|
|
|
|
« Last Edit: June 24, 2010, 08:37:25 am by uwefed »
|
Logged
|
|
|
|
|
Parma/Salento
Offline
Jr. Member
Karma: 0
Posts: 83
Arduino rocks
|
 |
« Reply #8 on: June 24, 2010, 10:52:49 am » |
Hai ragione, ho sbagliato a scrivere la lista dei pin, in realtà ho collegato l'EEPROM correttamente. Non mi resta che provare con le resistenze di Pull-up....
|
|
|
|
|
Logged
|
|
|
|
|
Parma/Salento
Offline
Jr. Member
Karma: 0
Posts: 83
Arduino rocks
|
 |
« Reply #9 on: June 24, 2010, 11:23:43 am » |
Niente, non va neanche con le resistenze di pull up (da 10KOHM) e neanche sostituendo la EEPROM!
Non so più che fare...
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 181
Arduino rocks
|
 |
« Reply #10 on: June 24, 2010, 12:59:58 pm » |
allora per aiutarti ci provo anch'io ho trovato in mezzo alla roba una 20WC16 però non ho capito una cosa: l tuo EEprom é di 16k bit e percui 2048 Byte cosa centrano i 2024 byte
|
|
|
|
|
Logged
|
|
|
|
|
Parma/Salento
Offline
Jr. Member
Karma: 0
Posts: 83
Arduino rocks
|
 |
« Reply #11 on: June 24, 2010, 01:27:27 pm » |
2024?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 181
Arduino rocks
|
 |
« Reply #12 on: June 24, 2010, 01:38:02 pm » |
scusa 2048 byte
|
|
|
|
|
Logged
|
|
|
|
|
Parma/Salento
Offline
Jr. Member
Karma: 0
Posts: 83
Arduino rocks
|
 |
« Reply #13 on: June 24, 2010, 01:51:02 pm » |
16 Kbit / 8 = 2 KByte * 1024 = 2048 byte  Ogni aiuto è ben accetto! 
|
|
|
|
« Last Edit: June 24, 2010, 02:01:05 pm by 83darking83 »
|
Logged
|
|
|
|
|
cuneo
Offline
God Member
Karma: 3
Posts: 777
|
 |
« Reply #14 on: June 24, 2010, 03:40:58 pm » |
io ti posto il codice che ho usato(ai tempi) per capirne il funzionamento non so a chi rendere grazie in quanto non mi ricordo da che pagina l'ho preso, spero che l'autore non si arrabbi: #include <Wire.h>
/* Simple read & write to a 24LC256 256K EEPROM using the Wire library. Addresses are ints - 0000-7FFF (32767) Data is bytes (8 bits x 32767 = 256K) Functions for R/W of single byte or a page of bytes. Max page is 28 bytes. Arduino GND- A0-|oU |-Vcc to Arduino Vcc Arduino GND- A1-| |-WP to GND for now. Set to Vcc for write protection. Arduino GND- A2-| |-SCL to Arduino 5 Arduino GND-Vss-| |-SDA to Arduino 4 --- (A2, A1, A0 to GND for 1010000 (0x50) address.) If set to Vcc adds to address (1010,A2,A1,A0) */
#define EEPROM_ADDR 0x50 // I2C Buss address of 24LC256 256K EEPROM void setup(){ Wire.begin(); // join I2C bus (address optional for master) Serial.begin(9600); Serial.println("\n----------------\n"); // TESTS FOR EACH FUNCTION BEGIN HERE Serial.println("Writing Test:");
for (int i=0; i<32; i++){ // loop for first 20 slots i2c_eeprom_write_byte(EEPROM_ADDR,i,77-i);// write address + 65 A or 97 a Serial.print(". "); delay(5); // NEED THIS DELAY! (tests suggest it can be as // small as delay(3) -- BBR } Serial.println(""); delay(500); Serial.println("Reading Test:"); for (int i=0; i<32; i++){ // loop for first 20 slots Serial.print(i2c_eeprom_read_byte(EEPROM_ADDR, i),BYTE); Serial.print(" "); } // setup for page tests . . . byte PageData[30]; // array that will hold test data for a page byte PageRead[30]; // array that will hold result of data for a page for (int i=0; i<30; i++){ // zero both arrays for next test PageData[i] = 0; PageRead[i] = 0; } Serial.println(""); for (int i=0; i<30; i++) PageData[i] = i+33; // fill up array for next test char 33 = ! Serial.println("Writing Page Test:"); Serial.println(millis()); i2c_eeprom_write_page(EEPROM_ADDR, 100, PageData, 28 ); // 28 bytes/page is max Serial.println(millis()); Serial.println("Reading Page Test:"); i2c_eeprom_read_buffer( EEPROM_ADDR, 100, PageRead, 28); for (int i=0; i<28; i++){ Serial.print(PageRead[i],BYTE); // display the array read Serial.print(" "); } }
void loop(){} void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) { int rdata = data; Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddress >> 8)); // Address High Byte Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte Wire.send(rdata); Wire.endTransmission(); } // Address is a page address, 6-bit (63). More and end will wrap around // But data can be maximum of 28 bytes, because the Wire library has a buffer of 32 bytes void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) { Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddresspage >> 8)); // Address High Byte Wire.send((int)(eeaddresspage & 0xFF)); // Address Low Byte byte c; for ( c = 0; c < length; c++) Wire.send(data[c]); Wire.endTransmission(); delay(10); // need some delay } byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) { byte rdata = 0xFF; Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddress >> 8)); // Address High Byte Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte Wire.endTransmission(); Wire.requestFrom(deviceaddress,1); if (Wire.available()) rdata = Wire.receive(); return rdata; } // should not read more than 28 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) { Wire.beginTransmission(deviceaddress); Wire.send((int)(eeaddress >> 8)); // Address High Byte Wire.send((int)(eeaddress & 0xFF)); // Address Low Byte Wire.endTransmission(); Wire.requestFrom(deviceaddress,length); // int c = 0; for ( int c = 0; c < length; c++ ) if (Wire.available()) buffer[c] = Wire.receive(); }
|
|
|
|
|
Logged
|
|
|
|
|
|