// Pull CS pin high to disable communication digitalWrite(csPin, HIGH); }
// Read the status register of the EEPROM chip and return a byte byte readStatus() { // Declare a variable to store the status byte byte status;
// Pull CS pin low to enable communication digitalWrite(csPin, LOW);
// Send read status instruction SPI.transfer(READ_STATUS);
// Receive the status byte status = SPI.transfer(0x00);
// Pull CS pin high to disable communication digitalWrite(csPin, HIGH);
// Return the status byte return status; }
// Write a byte to the status register of the EEPROM chip void writeStatus(byte status) { // Pull CS pin low to enable communication digitalWrite(csPin, LOW);
// Send write status instruction SPI.transfer(WRITE_STATUS);
// Send the status byte SPI.transfer(status);
// Pull CS pin high to disable communication digitalWrite(csPin, HIGH); }
// Read a certain amount of data from a certain address of the EEPROM chip and store it in the buffer array void readData(int address, int amount) { // Pull CS pin low to enable communication digitalWrite(csPin, LOW);
// Send read data instruction SPI.transfer(READ_DATA);
// Send the high byte of the address SPI.transfer(highByte(address));
// Send the low byte of the address SPI.transfer(lowByte(address));
// Receive the data bytes and store them in the buffer array for (int i = 0; i < amount; i++) { buffer[i] = SPI.transfer(0x00); }
// Pull CS pin high to disable communication digitalWrite(csPin, HIGH); }
// Write a certain amount of data from the buffer array to a certain address of the EEPROM chip void writeData(int address, int amount) { // Pull CS pin low to enable communication digitalWrite(csPin, LOW);
// Send write data instruction SPI.transfer(WRITE_DATA);
// Send the high byte of the address SPI.transfer(highByte(address));
// Send the low byte of the address SPI.transfer(lowByte(address));
// Send the data bytes from the buffer array for (int i = 0; i < amount; i++) { SPI.transfer(buffer[i]); }
// Pull CS pin high to disable communication digitalWrite(csPin, HIGH); }
// Print a certain amount of data from the buffer array to the serial monitor void printBuffer(int amount) { // Print an opening bracket Serial.print("[");
// Print each data byte in hexadecimal format for (int i = 0; i < amount; i++) { Serial.print("0x"); if (buffer[i] < 16) { Serial.print("0"); } Serial.print(buffer[i], HEX); if (i < amount -1) { Serial.print(", "); } }
// Print a closing bracket and a new line Serial.println("]"); }
void loop() { // Send the temperature data type to the peripheral device SPI.transfer(TEMPERATURE);
// Receive the temperature data value from the peripheral device dataValue = SPI.transfer(0x00);
// Print the temperature data to the serial monitor Serial.print("Temperature: "); Serial.print(dataValue); Serial.println(" C");
// Wait for a second delay(1000);
// Send the humidity data type to the peripheral device SPI.transfer(HUMIDITY);
// Receive the humidity data value from the peripheral device dataValue = SPI.transfer(0x00);
// Print the humidity data to the serial monitor Serial.print("Humidity: "); Serial.print(dataValue); Serial.println(" %");
// Wait for a second delay(1000); }
对于外围设备,我们需要在setup()函数中初始化SPI通信,并设置为从机模式:
1 2 3 4 5 6 7 8 9 10 11
void setup() { // Initialize SPI communication as slave SPI.begin(); SPI.setSCK(13); // Set SCK pin to pin SPI.setMISO(12); // Set MISO pin to pin SPI.setMOSI(11); // Set MOSI pin to pin SPI.setSS(10); // Set SS pin to pin SPI.onData(receiveData); // Register a callback function to handle incoming data }
// Define a callback function to handle incoming data void receiveData(int byteCount) { // Declare a variable to store the incoming data type byte dataType;
// Read the incoming data type from the COPI line dataType = SPI.transfer(0x00);
// Check the data type and send back the corresponding data value on the CIPO line switch (dataType) { case TEMPERATURE: SPI.transfer(25); // Send a dummy temperature value of break; case HUMIDITY: SPI.transfer(50); // Send a dummy humidity value of break; default: SPI.transfer(0x00); // Send a zero value for unknown data type break; } }
最后,我们可以在loop()函数中什么都不做,因为所有的通信都是由中断驱动的:
1 2 3 4 5
void loop() { // Do nothing, communication is handled by interrupt }