Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #define ARRAY_SIZE 5
- #define TRANSACTION_PERIOD_MS 4000
- const uint8_t array_to_send[ARRAY_SIZE] = {0x10, 0x11, 0x12, 0x13, 0x14};//, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}; // Array of data to be sent to the slave
- uint8_t buffer[ARRAY_SIZE] = {0}; // Buffer for holding data being sent and receiving incoming data
- uint32_t timer_ms = 0; // Timer for periodical transfers
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600); // Initialise serial output
- pinMode(SS, OUTPUT); //not really necessary as it is done by the SPI library.
- ssDisable(); // Make SS pin inactive
- SPI.setBitOrder(MSBFIRST);
- SPI.setDataMode(SPI_MODE0); //I believe it to be Mode3
- SPI.setClockDivider(SPI_CLOCK_DIV8);
- SPI.begin(); // Initialise SPI interface
- Serial.print("rnMaster started");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if (millis() >= timer_ms)
- { // It is time to transfer data
- timer_ms = millis() + TRANSACTION_PERIOD_MS; // Set the next time for data transfer
- memcpy(buffer, array_to_send, ARRAY_SIZE); // Copy data to send to the buffer
- ssEnable(); // Set SS to active state
- SPI.transfer(buffer, ARRAY_SIZE); // Send data from the buffer to the slave and receive data from the slave to the same buffer
- ssDisable(); // Set SS to inactive state
- Serial.print("rnReceived:");
- for (uint8_t i = 0; i < ARRAY_SIZE; i++)
- { // Print received data
- Serial.print(" 0x");
- Serial.print(buffer[i], HEX);
- }
- }
- }
- // Set SS to active state
- void ssEnable (void)
- {
- digitalWrite(SS, LOW);
- }
- // Set SS to inactive state
- void ssDisable (void)
- {
- digitalWrite(SS, HIGH);
- }
- #include <SPI.h>
- #define ARRAY_SIZE 5
- const uint8_t array_to_send[ARRAY_SIZE] = {0x20, 0x21, 0x22, 0x23, 0x24};//, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F}; // Array of data to be sent to the slave
- uint8_t array_to_send_ndx = 0; // Index in the array for data to be sent to the master next
- uint8_t array_for_receive[ARRAY_SIZE] = {0}; // Array for data received from the master
- uint8_t array_for_receive_ndx = 0; // Index in the array for data to be received from the master next
- uint8_t array_for_print_ndx = 0; // Index in the received array for data to be printed
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600); // Initialise serial output
- pinMode(SCK, INPUT); // Set SCK pin mode to INPUT
- pinMode(MOSI, INPUT); // Set MOSI pin mode to INPUT
- pinMode(MISO, OUTPUT); // Set MISO pin mode to OUTPUT
- pinMode(SS, INPUT); // Set SS pin mode to INPUT
- SPI.setDataMode(SPI_MODE0); //I believe it to be Mode3
- SPI.setClockDivider(SPI_CLOCK_DIV8);
- SPCR |= _BV(SPE); // Enable SPI as slave.
- SPI.attachInterrupt(); // Attach interrupt routine for SPI
- SPDR = array_to_send[array_to_send_ndx++]; // Put into SPI data register first data to send
- Serial.print("rnSlave started");
- Serial.print("rnReceived:");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- while (array_for_receive_ndx != array_for_print_ndx)
- { // Print received data
- Serial.print(" 0x");
- Serial.print(array_for_receive[array_for_print_ndx++], HEX);
- if (array_for_print_ndx >= ARRAY_SIZE)
- array_for_print_ndx = 0;
- }
- }
- // Interrupt service routine for SPI
- ISR (SPI_STC_vect)
- {
- array_for_receive[array_for_receive_ndx++] = SPDR; // Put received byte into the array
- SPDR = array_to_send[array_to_send_ndx++]; // Send the next byte to be sent
- if (array_for_receive_ndx >= ARRAY_SIZE) // Check array boundary
- array_for_receive_ndx = 0;
- if (array_to_send_ndx >= ARRAY_SIZE) // Check array boundary
- array_to_send_ndx = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement