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.  
  3. spi_bus = 0
  4. spi_device = 0
  5.  
  6. spi = spidev.SpiDev()
  7. spi.open(spi_bus, spi_device)
  8. spi.max_speed_hz = 1000000
  9.  
  10. # This is based on using SPI Bus 0 (the pin numbers mentioned previously) and device 0 (CE0). The following code shows the transfer of two bytes from the Raspberry Pi to the Arduino.
  11. # Send a null byte to check for value
  12.  
  13. send_byte = 0x80
  14. rcv_byte = spi.xfer2([send_byte])
  15.  
  16. # repeat to check for a response
  17. rcv_byte = spi.xfer2([send_byte])
  18. data_recv = rcv_byte[0]
  19.  
  20. if (data_recv != 0x80):
  21. print ("Unable to communicate with Arduino "+str(data_recv))
  22. quit()

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.1653916919.txt.gz · Dernière modification : de serge