Advertisement
Guest User

kans

a guest
Mar 13th, 2008
6,683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. /* Ejemplo EEPROM
  2.  * Autor: kans
  3.  * Fecha: 05/03/2008
  4.  */
  5.  
  6. #include <Wire.h> //libreria I2C
  7.  
  8. //Las siguientes funciones para lectura y escritura en una EEPROM se encuentran en el wiki de Arduino: http://www.arduino.cc/playground/Code/I2CEEPROM
  9.  
  10. void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
  11.   int rdata = data;
  12.   Wire.beginTransmission(deviceaddress);
  13.   Wire.send((int)(eeaddress >> 8)); // MSB
  14.   Wire.send((int)(eeaddress & 0xFF)); // LSB
  15.   Wire.send(rdata);
  16.   Wire.endTransmission();
  17. }
  18.  
  19. // WARNING: address is a page address, 6-bit end will wrap around
  20. // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  21. void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
  22.   Wire.beginTransmission(deviceaddress);
  23.   Wire.send((int)(eeaddresspage >> 8)); // MSB
  24.   Wire.send((int)(eeaddresspage & 0xFF)); // LSB
  25.   byte c;
  26.   for ( c = 0; c < length; c++)
  27.     Wire.send(data[c]);
  28.   Wire.endTransmission();
  29. }
  30.  
  31. byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
  32.   byte rdata = 0xFF;
  33.   Wire.beginTransmission(deviceaddress);
  34.   Wire.send((int)(eeaddress >> 8)); // MSB
  35.   Wire.send((int)(eeaddress & 0xFF)); // LSB
  36.   Wire.endTransmission();
  37.   Wire.requestFrom(deviceaddress,1);
  38.   if (Wire.available()) rdata = Wire.receive();
  39.   return rdata;
  40. }
  41.  
  42. // maybe let's not read more than 30 or 32 bytes at a time!
  43. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  44.   Wire.beginTransmission(deviceaddress);
  45.   Wire.send((int)(eeaddress >> 8)); // MSB
  46.   Wire.send((int)(eeaddress & 0xFF)); // LSB
  47.   Wire.endTransmission();
  48.   Wire.requestFrom(deviceaddress,length);
  49.   int c = 0;
  50.   for ( c = 0; c < length; c++ )
  51.     if (Wire.available()) buffer[c] = Wire.receive();
  52. }
  53.  
  54.  
  55. void setup() {
  56.   char cadena[] = "hola mundo desde una eeprom"; //cadena a escribir
  57.   Wire.begin(); //es obligatorio inicializar la conexion
  58.   Serial.begin(9600);
  59.   i2c_eeprom_write_page(0x50, 0, (byte *)cadena, sizeof(cadena)); //escribir la cadena al principio de la EEPROM; comentar esta linea para probar que la memoria es no volatil
  60.   delay(10); //pequeña pausa despues de escribir en la memoria
  61. }
  62.  
  63. void loop() {
  64.   int addr=0; //direccion a leer
  65.   byte b = i2c_eeprom_read_byte(0x50, 0); //acceso a la primera posicion de memoria
  66.  
  67.   while (b!=0) {
  68.     Serial.print((char)b); //enviar al ordenador
  69.     addr++; //siguiente direccion
  70.     b = i2c_eeprom_read_byte(0x50, addr); //acceso a posicion de memoria
  71.   }
  72.   Serial.println();
  73.   delay(2000);
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement