Outils pour utilisateurs

Outils du site


spi_documentation_2

Ceci est une ancienne révision du document !


Table des matières

Communication entre Raspberry Pi et Arduino en SPI

!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!C'est bourré d'erreur !!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Raspberry

  1. import spidev
  2. import time
  3.  
  4. spi_bus = 0
  5. spi_device = 0
  6. spi = spidev.SpiDev()
  7. spi.open(spi_bus, spi_device)
  8. spi.max_speed_hz = 1000000
  9.  
  10. # Send a null byte to check for value
  11. send_byte = 0x80
  12. rcv_byte = spi.xfer2([send_byte])
  13. # repeat to check for a response
  14. rcv_byte = spi.xfer2([send_byte])
  15. data_recv = rcv_byte[0]
  16. if (data_recv != 0x80):
  17. print ("Unable to communicate with Arduino "+str(data_recv))
  18. quit()
  19.  
  20.  
  21. # Data is sent as single bytearray
  22. # lower 4 bits = pin number
  23. # returns a value that is used to set a digital pin
  24. # pin = Arduino pin number
  25. # state = True for high, False for low
  26. # returns byte or 0xff = invalid
  27. def req_set_digital (pin, state):
  28. if (pin < 0 or pin > 10):
  29. return 0xff
  30. if (state == True):
  31. pin += 0x60
  32. else :
  33. pin += 0x20
  34. return pin
  35.  
  36. # read analog pin
  37. def req_read_analog (pin):
  38. if (pin < 0 or pin > 5):
  39. return 0xff
  40. return pin + 0x10
  41.  
  42. # read digital pin
  43. def req_read_digital (pin):
  44. if (pin < 0 or pin > 10):
  45. return 0xff
  46. return pin
  47.  
  48. while True:
  49. # Turn LEDs on
  50. for i in range (3, 10) :
  51. send_byte = req_set_digital (i, True)
  52. rcv_byte = spi.xfer2([send_byte])
  53. time.sleep (0.5)
  54. # Turn back off again
  55. for i in range (3, 10) :
  56. send_byte = req_set_digital (i, False)
  57. rcv_byte = spi.xfer2([send_byte])
  58. time.sleep (0.5)
  59. # Get status of pin 2
  60. send_byte = req_read_digital(2)
  61. rcv_byte = spi.xfer2([send_byte])
  62. # Delay to allow Arduino to get answer ready
  63. time.sleep (0.25)
  64. # Send null request
  65. send_byte = 0xf0
  66. rcv_byte = spi.xfer2([send_byte])
  67. print ("Value of digital pin 2 is "+str(rcv_byte[0]))
  68.  
  69. # Get status of analog 0
  70. send_byte = req_read_analog(0)
  71. rcv_byte = spi.xfer2([send_byte])
  72. # Delay to allow Arduino to get answer ready
  73. time.sleep (0.25)
  74. # Send null request
  75. send_byte = 0xf0
  76. rcv_byte = spi.xfer2([send_byte])
  77. print ("Value of A0 is "+str(rcv_byte[0]))

Arduino

  1. #include <SPI.h>
  2.  
  3. // track if digital pins are set as input or output (saves reading registers)
  4. // Pins 0 to 9 can be used, pin 10 can be used for SS (but can be used if that's not required)
  5. bool pin_output[11];
  6.  
  7. void setup()
  8. {
  9. // by default all pins are inputs
  10. for (int i=0; i<11; i++)
  11. {
  12. pin_output[i] = false;
  13. }
  14.  
  15. // Set the Main in Secondary Out as an output
  16. pinMode(MISO, OUTPUT);
  17. // turn on SPI as a secondary
  18. // Set appropriate bit in SPI Control Register
  19. SPCR |= _BV(SPE);
  20. }
  21.  
  22.  
  23. void loop ()
  24. {
  25. char print_text [50];
  26. byte in_byte;
  27. // SPIF indicates transmission complete (byte received)
  28. if ((SPSR & (1 << SPIF)) != 0)
  29. {
  30. in_byte = SPDR;
  31. // if no action bit sent then return same
  32. if (in_byte & 0x80) SPDR = in_byte;
  33. // if write set
  34. else if (in_byte & 0x20)
  35. {
  36. // set digital pin output - use mask to extract pin and output
  37. if (set_digital_pin (in_byte & 0x0F, in_byte & 0x40) != 0xFF) SPDR = in_byte;
  38. }
  39. // If value is only pin numbers then digital read
  40. else if (in_byte < 0x10)
  41. {
  42. byte return_val = read_digital_pin (in_byte);
  43. SPDR = return_val;
  44. }
  45. else if ((in_byte & 0x10) && !(in_byte & 0x20))
  46. {
  47. byte return_val = read_analog_pin (in_byte & 0x0F);
  48. SPDR = return_val;
  49. }
  50. else // Otherwise an error - return 0xff
  51. {
  52. SPDR = 0xFF;
  53. }
  54.  
  55. }
  56. }
  57.  
  58. byte set_digital_pin (byte address, bool high_value)
  59. {
  60. if (address < 0 || address > 10) return 0xFF;
  61. // Is it currently set as output - if not then set to output
  62. if (!pin_output[address])
  63. {
  64. pinMode(address, OUTPUT);
  65. pin_output[address] = true;
  66. }
  67. // set to low if 0, otherwise 1
  68. if (high_value == 0) digitalWrite(address, LOW);
  69. else digitalWrite (address, HIGH);
  70. }
  71.  
  72. // Returns 1 for high and 0 for low
  73. // If invalid then returns 0xff
  74. byte read_digital_pin (byte address)
  75. {
  76. if (address < 0 || address > 10) return 0xFF;
  77. // if currently output then change to input
  78. if (pin_output[address])
  79. {
  80. pinMode(address, INPUT);
  81. pin_output[address] = false;
  82. }
  83.  
  84. return digitalRead (address);
  85. }
  86.  
  87. // As full byte is used for value there is no invalid value
  88. // If invalid then returns 0x00
  89. byte read_analog_pin (byte address)
  90. {
  91. if (address < 0 || address > 5) return 0x00;
  92. int analog_val = analogRead (address);
  93. // Analog value is between 0 and 1024 - div by 4 for byte
  94. return (analog_val / 4);
  95. }
spi_documentation_2.1653924748.txt.gz · Dernière modification : de serge