Guest User

Untitled

a guest
Apr 20th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <SPI.h>
  2. #define READ 0x03 //read command
  3. #define WRITE 0x02 //write command
  4. #define WREN 0x06 // write enable
  5.  
  6. const int csPin = 10;
  7. const int wpPin = 9; // For this sketch you could also leave the wpPin unconnected
  8. const int writeCycle = 10;
  9.  
  10. void setup(){
  11. pinMode(csPin, OUTPUT);
  12. digitalWrite(csPin, HIGH);
  13. pinMode(wpPin, OUTPUT);
  14. digitalWrite(wpPin,HIGH);
  15. SPI.begin();
  16. Serial.begin(9600);
  17.  
  18. eepromWriteEnable();// enable write enable latch bit
  19. digitalWrite(csPin, LOW);
  20. SPI.transfer(0x60); //chip erase command
  21. digitalWrite(csPin, HIGH);
  22. delay(8000); //max time taken for chip erase to process
  23.  
  24. int intToWrite = 42;
  25. eepromWriteInt(4, intToWrite);
  26. intToWrite = -1111;
  27. eepromWriteInt(6, intToWrite);
  28.  
  29. int intToRead = 0;
  30. intToRead = eepromReadInt(4);
  31. Serial.print("intToRead (Address = 4): ");
  32. Serial.println(intToRead);
  33. intToRead = 0;
  34. intToRead = eepromReadInt(6);
  35. Serial.print("intToRead (Address = 6): ");
  36. Serial.println(intToRead);
  37. }
  38.  
  39. void loop(){}
  40.  
  41. void eepromWriteEnable(){
  42. digitalWrite(csPin, LOW);
  43. SPI.transfer(WREN); //set write enable latch
  44. digitalWrite(csPin, HIGH);
  45. }
  46.  
  47. void eepromWriteInt(int addr, int value){
  48. eepromWriteEnable();
  49. delay(100);
  50. // If you want to change the SPI clock speed, uncomment the following line and adjust
  51. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  52. digitalWrite(csPin, LOW);
  53. SPI.transfer(WRITE); //write command
  54. SPI.transfer((byte)(addr>>16)); //1st byte of 2 byte address
  55. SPI.transfer((byte)(addr>>8));
  56. SPI.transfer((byte)(addr&0xFF)); //1nd byte of 3 byte address
  57. SPI.transfer((byte)(value>>16)); //2st byte of 3 byte data
  58. SPI.transfer((byte)(value>>8));
  59. SPI.transfer((byte)(value&0xFF));//3nd byte of 3 byte data
  60. digitalWrite(csPin, HIGH);
  61. SPI.endTransaction(); // Uncomment if you have uncommented SPI.beginTransaction()
  62. delay(writeCycle);
  63. }
  64.  
  65. int eepromReadInt(int addr){
  66. byte MSB = 0;
  67. byte LSB = 0;
  68. int value = 0;
  69. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  70. digitalWrite(csPin, LOW);
  71. SPI.transfer(READ);
  72. SPI.transfer((byte)(addr>>16)); //1st byte of 3 byte address
  73. SPI.transfer((byte)(addr>>8));
  74. SPI.transfer((byte)(addr&0xFF));
  75. MSB = SPI.transfer(0x00); //store the first byte in MSB buffer
  76. LSB = SPI.transfer(0x00); //store the first byte in LSB buffer
  77. digitalWrite(csPin, HIGH);
  78. SPI.endTransaction();
  79. value = (int)((MSB<<8)|LSB);
  80. return value;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment