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

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. void setup()
  4. {
  5. // Set the Main in Secondary Out as an output
  6. pinMode(MISO, OUTPUT);
  7. // turn on SPI as a secondary
  8. // Set appropriate bit in SPI Control Register
  9. SPCR |= _BV(SPE);
  10. }
  11.  
  12. // The following code is then used to receive a byte and then set the data register to return a byte to the master controller device.
  13. void loop ()
  14. {
  15. byte in_byte;
  16. // SPIF indicates transmission complete (byte received)
  17. if ((SPSR & (1 << SPIF)) != 0)
  18. {
  19. in_byte = SPDR;
  20. // Handle the input code here
  21. // Set return_val to the value you want to return
  22. SPDR = return_val;
  23. }
spi_documentation_2.1653918321.txt.gz · Dernière modification : de serge