Advertisement
sn4k3

Program 1

May 19th, 2017
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.55 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #define device 0x50  // Address with three address pins grounded.
  4.  
  5. void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
  6.   byte err;
  7.   byte counter;
  8.     Wire.beginTransmission(deviceaddress);
  9.     Wire.write((byte)(eeaddress >> 8)); // MSB
  10.     Wire.write((byte)(eeaddress & 0xFF)); // LSB
  11.     Wire.write(&data, 1);
  12.     err = Wire.endTransmission();
  13.     if (err != 0) Serial.print("Cannot write to device");
  14.     /*while(1) {
  15.       Wire.beginTransmission(deviceaddress);
  16.       if(Wire.endTransmission() == 0)break;
  17.     }*/
  18.     // wait for write to finish by sending address again
  19.   //  ... give up after 100 attempts (1/10 of a second)
  20.   for (counter = 0; counter < 100; counter++)
  21.     {
  22.     delayMicroseconds (300);  // give it a moment
  23.     Wire.beginTransmission (device);
  24.     Wire.write ((byte) (deviceaddress >> 8));    // high order byte
  25.     Wire.write ((byte) (deviceaddress & 0xFF));  // low-order byte
  26.     err = Wire.endTransmission ();
  27.     if (err == 0)
  28.       break;
  29.     }
  30. }
  31.  
  32. // WARNING: address is a page address, 6-bit end will wrap around
  33. // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  34. void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
  35.     Wire.beginTransmission(deviceaddress);
  36.     Wire.write((byte)(eeaddresspage >> 8)); // MSB
  37.     Wire.write((byte)(eeaddresspage & 0xFF)); // LSB
  38.     byte c;
  39.     for ( c = 0; c < length; c++)
  40.         Wire.write(data[c]);
  41.     Wire.endTransmission();
  42. }
  43.  
  44. byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
  45.     byte rdata = 0xFF;
  46.     Wire.beginTransmission(deviceaddress);
  47.     Wire.write((int)(eeaddress >> 8)); // MSB
  48.     Wire.write((int)(eeaddress & 0xFF)); // LSB
  49.     Wire.endTransmission(); //RESTART OBLIGATORY
  50.     Wire.requestFrom(deviceaddress,1);
  51.     if (Wire.available()) rdata = Wire.read();
  52.     Wire.endTransmission();
  53.     return rdata;
  54. }
  55.  
  56. // maybe let's not read more than 30 or 32 bytes at a time!
  57. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  58.     Wire.beginTransmission(deviceaddress);
  59.     Wire.write((int)(eeaddress >> 8)); // MSB
  60.     Wire.write((int)(eeaddress & 0xFF)); // LSB
  61.     Wire.endTransmission();
  62.     Wire.requestFrom(deviceaddress,length);
  63.     int c = 0;
  64.     for ( c = 0; c < length; c++ )
  65.         if (Wire.available()) buffer[c] = Wire.read();
  66. }
  67.  
  68.  
  69.     unsigned char somedata[] = {
  70. 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0x38, 0x36, 0x36, 0x2E, 0x32, 0x37, 0x38, 0x36, 0x36, 0x2E,
  71. 0x32, 0x37, 0x03, 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x7F, 0x4A, 0x02, 0x00, 0xFA, 0xC5,
  72. 0xFA, 0xFB, 0xF8, 0xF9, 0xFE, 0xFF, 0xF2, 0xF3, 0xF7, 0xDF, 0xFA, 0x57, 0x03, 0x93, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x00, 0x03, 0x93,
  74. 0x02, 0x00, 0x28, 0x00, 0x0E, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0xC4, 0xC4, 0x50, 0x00,
  75. 0x4C, 0x00, 0x00, 0x60, 0x58, 0x4D, 0x00, 0x00, 0x00, 0x2E, 0x09, 0x44, 0x00, 0x00, 0xC6, 0x47,
  76. 0x00, 0x00, 0x55, 0x22, 0x00, 0x60, 0xA1, 0x60, 0x00, 0xC4, 0xC4, 0x50, 0x00, 0x4C, 0x00, 0x00,
  77. 0x00, 0x58, 0x4D, 0x00, 0x00, 0x00, 0x2E, 0x09, 0x44, 0x00, 0x00, 0xE3, 0xA3, 0x00, 0x00, 0x55,
  78. 0x22, 0x00, 0x60, 0xA1, 0x26, 0x00, 0x02, 0x00, 0xF7, 0xDF, 0xFA, 0x60, 0xAA, 0xFF, 0xE1, 0x1E,
  79. 0x4C, 0x08, 0x08, 0x00, 0x04, 0x28, 0x02, 0xF1, 0x0A, 0x00, 0xA6, 0x7F, 0x0A, 0x08, 0x02, 0x02,
  80. 0x00, 0x00, 0x28, 0xFF, 0x71, 0xAC, 0x85, 0x89, 0x6F, 0x17, 0x08, 0x08, 0x08, 0x00, 0x00, 0x28,
  81. 0xFF, 0x2B, 0x00, 0x1C, 0xAA, 0x7D, 0x08, 0x08, 0x02, 0x02, 0x00, 0x00, 0x28, 0x17, 0x00, 0x93,
  82. 0x02, 0xB8, 0xB7, 0x22, 0x4C, 0x80, 0x80, 0x00, 0x04, 0x28, 0x7B, 0x00, 0x00, 0x00, 0x82, 0x92,
  83. 0x2E, 0x44, 0x01, 0x01, 0x00, 0x04, 0x28, 0x01, 0x74, 0x00, 0x82, 0x00, 0x99, 0x0B, 0x08, 0x10,
  84. 0x10, 0x00, 0x00, 0x28, 0x03, 0x5F, 0x61, 0x57, 0x9E, 0x6B, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  86. };
  87.  
  88. char somedata2[] = "this is data from the eeprom"; // data to write
  89.  
  90.  
  91.  
  92.  
  93. void setup()
  94. {
  95.     Serial.begin(9600);
  96.     Wire.begin(); // initialise the connection
  97.  
  98.     delay(1000);
  99.     Serial.println("Wait for Wire");
  100.     Serial.println("OK!");
  101.  
  102.   // Write
  103.   Serial.print("Write: ");
  104.   for(short i = 0; i < 256; i++)
  105.   {
  106.     if(i % 16 == 0) Serial.println(" ");
  107.     Serial.print ("0x");
  108.     Serial.print(somedata[i], HEX);
  109.     Serial.print ( ", ");
  110.     i2c_eeprom_write_byte(device, i, (byte)somedata[i]);
  111.   }
  112.    
  113.    
  114.     delay(100); //add a small delay
  115.  
  116.     Serial.println(" ");
  117.     Serial.println("Memory written: ");
  118.     Serial.println(sizeof(somedata));
  119.     delay(1000); //add a small delay
  120.  
  121.     //i2c_eeprom_write_page(device, 0, (byte *)somedata2, sizeof(somedata2)); // write to EEPROM
  122.  
  123.     Serial.print("\nReady\n");
  124.  
  125.   // DUMP
  126.   Serial.print("Read: ");
  127.   for(short i = 0; i < 256; i++)
  128.   {
  129.     if(i % 16 == 0) Serial.println(" ");
  130.     Serial.print ("0x");
  131.     Serial.print (i2c_eeprom_read_byte(device, i),HEX);
  132.     Serial.print ( ", ");
  133.   }
  134.  
  135. }
  136.  
  137. void loop()
  138. {
  139.  
  140.  
  141. /*for (byte bank = 0; bank < 16; bank++)
  142.   {
  143.  
  144.     byte data[16]; // a byte array to store 16 bytes
  145.     Wire.requestFrom(0x53, 16); // request 16 bytes
  146.     for (byte i = 0; i < 16; i++)
  147.       {
  148.         data[i] = Wire.read(); // store each byte into the array
  149.         Serial.print ("0x");
  150.         Serial.print (data[i],HEX);
  151.         Serial.print ( ", ");
  152.       }
  153.  
  154.     Serial.print ("\n");
  155.     delay(500);
  156.   }*/
  157.  
  158.   delay(1000000);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement