pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C pastebin - collaborative debugging tool View Help


Posted by kans on Thu 13 Mar 22:20 (modification of post by view diff)
report abuse | View followups from l, Anonymous, OmarDiaz, wep, wep, msn, Anonymous and edu | download | new post

  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. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post