Advertisement
sn4k3

Program 2

May 19th, 2017
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.32 KB | None | 0 0
  1.  
  2. // Written by Nick Gammon
  3. // February 2011
  4.  
  5. // Note: maximum address is 0x7FFF (32 Kbytes)
  6.  
  7. #include <Wire.h>    
  8.  
  9. const byte rom = 0x50;    // Address of 24LC256 eeprom chip
  10.  
  11.     unsigned char somedata[][16] = {
  12. {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0x38, 0x36, 0x36, 0x2E, 0x32, 0x37, 0x38, 0x36, 0x36, 0x2E},
  13. {0x32, 0x37, 0x03, 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x7F, 0x4A, 0x02, 0x00, 0xFA, 0xC5},
  14. {0xFA, 0xFB, 0xF8, 0xF9, 0xFE, 0xFF, 0xF2, 0xF3, 0xF7, 0xDF, 0xFA, 0x57, 0x03, 0x93, 0x00, 0x00},
  15. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x00, 0x03, 0x93},
  16. {0x02, 0x00, 0x28, 0x00, 0x0E, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0xC4, 0xC4, 0x50, 0x00},
  17. {0x4C, 0x00, 0x00, 0x60, 0x58, 0x4D, 0x00, 0x00, 0x00, 0x2E, 0x09, 0x44, 0x00, 0x00, 0xC6, 0x47},
  18. {0x00, 0x00, 0x55, 0x22, 0x00, 0x60, 0xA1, 0x60, 0x00, 0xC4, 0xC4, 0x50, 0x00, 0x4C, 0x00, 0x00},
  19. {0x00, 0x58, 0x4D, 0x00, 0x00, 0x00, 0x2E, 0x09, 0x44, 0x00, 0x00, 0xE3, 0xA3, 0x00, 0x00, 0x55},
  20. {0x22, 0x00, 0x60, 0xA1, 0x26, 0x00, 0x02, 0x00, 0xF7, 0xDF, 0xFA, 0x60, 0xAA, 0xFF, 0xE1, 0x1E},
  21. {0x4C, 0x08, 0x08, 0x00, 0x04, 0x28, 0x02, 0xF1, 0x0A, 0x00, 0xA6, 0x7F, 0x0A, 0x08, 0x02, 0x02},
  22. {0x00, 0x00, 0x28, 0xFF, 0x71, 0xAC, 0x85, 0x89, 0x6F, 0x17, 0x08, 0x08, 0x08, 0x00, 0x00, 0x28},
  23. {0xFF, 0x2B, 0x00, 0x1C, 0xAA, 0x7D, 0x08, 0x08, 0x02, 0x02, 0x00, 0x00, 0x28, 0x17, 0x00, 0x93},
  24. {0x02, 0xB8, 0xB7, 0x22, 0x4C, 0x80, 0x80, 0x00, 0x04, 0x28, 0x7B, 0x00, 0x00, 0x00, 0x82, 0x92},
  25. {0x2E, 0x44, 0x01, 0x01, 0x00, 0x04, 0x28, 0x01, 0x74, 0x00, 0x82, 0x00, 0x99, 0x0B, 0x08, 0x10},
  26. {0x10, 0x00, 0x00, 0x28, 0x03, 0x5F, 0x61, 0x57, 0x9E, 0x6B, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00},
  27. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  28. };
  29.  
  30. void setup (void)
  31.   {
  32.   Serial.begin (9600);   // debugging
  33.  
  34.   Wire.begin ();  
  35.  
  36.   // test: write the number 123 to address 0x1234
  37.   /*writeEEPROM (rom, 0, 123);
  38.  
  39.   // read back to confirm
  40.   byte a = readEEPROM (rom, 0);
  41.  
  42.   Serial.println (a, DEC);  // display to confirm
  43.  
  44.   // test: write a string to address 0x1000
  45.   byte hello [] = "Hello, world!";
  46.  
  47.   writeEEPROM (rom, 10, hello, sizeof hello);*/
  48.   byte test;
  49.  
  50.  
  51.   for(byte i = 0; i < 16; i++)
  52.   {
  53.     writeEEPROM (rom, i, somedata[i], sizeof somedata[i]);
  54.   }
  55.  
  56.   for(short i = 0; i < 256; i++)
  57.   {
  58.     if(i % 16 == 0) Serial.println(" ");
  59.     byte err = readEEPROM(rom, i);
  60.     Serial.print ("0x");
  61.     Serial.print (err, HEX);
  62.     Serial.print ( ", ");
  63.   }
  64. }  // end of setup
  65.  
  66. void loop() {}  // no main loop
  67.  
  68.  
  69. // write len (max 32) bytes to device, returns non-zero on error
  70. //  return code: 0xFF means buffer too long
  71. //             other: other error (eg. device not present)
  72.  
  73. // Note that if writing multiple bytes the address plus
  74. //  length must not cross a 64-byte boundary or it will "wrap"
  75.  
  76. byte writeEEPROM (byte device, unsigned int addr, byte * data, byte len )
  77.   {
  78.   byte err;
  79.   byte counter;
  80.  
  81.   if (len > BUFFER_LENGTH)  // 32 (in Wire.h)
  82.     return 0xFF;  // too long
  83.  
  84.   Wire.beginTransmission(device);
  85.   Wire.write ((byte) (addr >> 8));    // high order byte
  86.   Wire.write ((byte) (addr & 0xFF));  // low-order byte
  87.   Wire.write (data, len);
  88.   err = Wire.endTransmission ();
  89.  
  90.   if (err != 0)
  91.     return err;  // cannot write to device
  92.    
  93.   // wait for write to finish by sending address again
  94.   //  ... give up after 100 attempts (1/10 of a second)
  95.   for (counter = 0; counter < 100; counter++)
  96.     {
  97.     delayMicroseconds (300);  // give it a moment
  98.     Wire.beginTransmission (device);
  99.     Wire.write ((byte) (addr >> 8));    // high order byte
  100.     Wire.write ((byte) (addr & 0xFF));  // low-order byte
  101.     err = Wire.endTransmission ();
  102.     if (err == 0)
  103.       break;
  104.     }
  105.    
  106.   return err;
  107.  
  108.   } // end of writeEEPROM
  109.  
  110. // write one byte to device, returns non-zero on error
  111. byte writeEEPROM (byte device, unsigned int addr, byte data )
  112.   {
  113.   return writeEEPROM (device, addr, &data, 1);
  114.   } // end of writeEEPROM
  115.  
  116. // read len (max 32) bytes from device, returns non-zero on error
  117. //  return code: 0xFF means buffer too long
  118. //               0xFE means device did not return all requested bytes
  119. //             other: other error (eg. device not present)
  120.  
  121. // Note that if reading multiple bytes the address plus
  122. //  length must not cross a 64-byte boundary or it will "wrap"
  123.  
  124. byte readEEPROM (byte device, unsigned int addr, byte * data, byte len )
  125.   {
  126.   byte err;
  127.   byte counter;
  128.  
  129.   if (len > BUFFER_LENGTH)  // 32 (in Wire.h)
  130.     return 0xFF;  // too long
  131.  
  132.   Wire.beginTransmission (device);
  133.   Wire.write ((byte) (addr >> 8));    // high order byte
  134.   Wire.write ((byte) (addr & 0xFF));  // low-order byte
  135.   err = Wire.endTransmission ();
  136.  
  137.   if (err != 0)
  138.     return err;  // cannot read from device
  139.  
  140.   // initiate blocking read into internal buffer
  141.   Wire.requestFrom (device, len);
  142.  
  143.   // pull data out of Wire buffer into our buffer
  144.   for (counter = 0; counter < len; counter++)
  145.     {
  146.     data [counter] = Wire.read ();
  147.     }
  148.    
  149.   return 0;  // OK
  150.   }  // end of readEEPROM
  151.  
  152. // read one byte from device, returns 0xFF on error
  153. byte readEEPROM (byte device, unsigned int addr )
  154.   {
  155.   byte temp;
  156.  
  157.   if (readEEPROM (device, addr, &temp, 1) == 0)
  158.     return temp;
  159.  
  160.   return 0xFF;
  161.  
  162.   }  // end of readEEPROM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement