Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. #define ARRAY_SIZE 5
  4. #define TRANSACTION_PERIOD_MS 4000
  5.  
  6. 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
  7. uint8_t buffer[ARRAY_SIZE] = {0}; // Buffer for holding data being sent and receiving incoming data
  8. uint32_t timer_ms = 0; // Timer for periodical transfers
  9.  
  10. void setup() {
  11. // put your setup code here, to run once:
  12. Serial.begin(9600); // Initialise serial output
  13. pinMode(SS, OUTPUT); //not really necessary as it is done by the SPI library.
  14. ssDisable(); // Make SS pin inactive
  15. SPI.setBitOrder(MSBFIRST);
  16. SPI.setDataMode(SPI_MODE0); //I believe it to be Mode3
  17. SPI.setClockDivider(SPI_CLOCK_DIV8);
  18. SPI.begin(); // Initialise SPI interface
  19. Serial.print("rnMaster started");
  20. }
  21.  
  22. void loop() {
  23. // put your main code here, to run repeatedly:
  24. if (millis() >= timer_ms)
  25. { // It is time to transfer data
  26. timer_ms = millis() + TRANSACTION_PERIOD_MS; // Set the next time for data transfer
  27. memcpy(buffer, array_to_send, ARRAY_SIZE); // Copy data to send to the buffer
  28. ssEnable(); // Set SS to active state
  29. SPI.transfer(buffer, ARRAY_SIZE); // Send data from the buffer to the slave and receive data from the slave to the same buffer
  30. ssDisable(); // Set SS to inactive state
  31. Serial.print("rnReceived:");
  32. for (uint8_t i = 0; i < ARRAY_SIZE; i++)
  33. { // Print received data
  34. Serial.print(" 0x");
  35. Serial.print(buffer[i], HEX);
  36. }
  37. }
  38. }
  39.  
  40. // Set SS to active state
  41. void ssEnable (void)
  42. {
  43. digitalWrite(SS, LOW);
  44. }
  45.  
  46. // Set SS to inactive state
  47. void ssDisable (void)
  48. {
  49. digitalWrite(SS, HIGH);
  50. }
  51.  
  52. #include <SPI.h>
  53.  
  54. #define ARRAY_SIZE 5
  55. 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
  56. uint8_t array_to_send_ndx = 0; // Index in the array for data to be sent to the master next
  57. uint8_t array_for_receive[ARRAY_SIZE] = {0}; // Array for data received from the master
  58. uint8_t array_for_receive_ndx = 0; // Index in the array for data to be received from the master next
  59. uint8_t array_for_print_ndx = 0; // Index in the received array for data to be printed
  60.  
  61. void setup() {
  62. // put your setup code here, to run once:
  63. Serial.begin(9600); // Initialise serial output
  64.  
  65. pinMode(SCK, INPUT); // Set SCK pin mode to INPUT
  66. pinMode(MOSI, INPUT); // Set MOSI pin mode to INPUT
  67. pinMode(MISO, OUTPUT); // Set MISO pin mode to OUTPUT
  68. pinMode(SS, INPUT); // Set SS pin mode to INPUT
  69. SPI.setDataMode(SPI_MODE0); //I believe it to be Mode3
  70. SPI.setClockDivider(SPI_CLOCK_DIV8);
  71. SPCR |= _BV(SPE); // Enable SPI as slave.
  72. SPI.attachInterrupt(); // Attach interrupt routine for SPI
  73. SPDR = array_to_send[array_to_send_ndx++]; // Put into SPI data register first data to send
  74. Serial.print("rnSlave started");
  75. Serial.print("rnReceived:");
  76. }
  77.  
  78. void loop() {
  79. // put your main code here, to run repeatedly:
  80. while (array_for_receive_ndx != array_for_print_ndx)
  81. { // Print received data
  82. Serial.print(" 0x");
  83. Serial.print(array_for_receive[array_for_print_ndx++], HEX);
  84. if (array_for_print_ndx >= ARRAY_SIZE)
  85. array_for_print_ndx = 0;
  86.  
  87. }
  88. }
  89.  
  90. // Interrupt service routine for SPI
  91. ISR (SPI_STC_vect)
  92. {
  93. array_for_receive[array_for_receive_ndx++] = SPDR; // Put received byte into the array
  94. SPDR = array_to_send[array_to_send_ndx++]; // Send the next byte to be sent
  95. if (array_for_receive_ndx >= ARRAY_SIZE) // Check array boundary
  96. array_for_receive_ndx = 0;
  97. if (array_to_send_ndx >= ARRAY_SIZE) // Check array boundary
  98. array_to_send_ndx = 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement