spi_documentation_2
Ceci est une ancienne révision du document !
Communication entre Raspberry Pi et Arduino en SPI
Cette page est inspirée de penguintutor.com Using Raspberry Pi and Arduino together through SPI serial communications
Raspberry
import spidev spi_bus = 0 spi_device = 0 spi = spidev.SpiDev() spi.open(spi_bus, spi_device) spi.max_speed_hz = 1000000 # 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. # Send a null byte to check for value send_byte = 0x80 rcv_byte = spi.xfer2([send_byte]) # repeat to check for a response rcv_byte = spi.xfer2([send_byte]) data_recv = rcv_byte[0] if (data_recv != 0x80): print ("Unable to communicate with Arduino "+str(data_recv)) quit()
Arduino
# include SPI.h void setup() { // Set the Main in Secondary Out as an output pinMode(MISO, OUTPUT); // turn on SPI as a secondary // Set appropriate bit in SPI Control Register SPCR |= _BV(SPE); } // 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. void loop () { byte in_byte; // SPIF indicates transmission complete (byte received) if ((SPSR & (1 << SPIF)) != 0) { in_byte = SPDR; // Handle the input code here // Set return_val to the value you want to return SPDR = return_val; }
spi_documentation_2.1653916887.txt.gz · Dernière modification : de serge
