From 6671a64563369d8e3540639753ba64086a75d7a3 Mon Sep 17 00:00:00 2001 From: Abel Tim Date: Sun, 2 Jun 2024 17:57:32 +0200 Subject: add files --- lower_connection_schema.pdf | Bin 0 -> 129143 bytes .../arduino_binairy_decoder.ino | 28 ++++++++++++++++ .../arduino_master_read/arduino_master_read.ino | 26 +++++++++++++++ spi_comm/arduino_slave_read/arduino_slave_read.ino | 30 +++++++++++++++++ .../arduino_slave_write/arduino_slave_write.ino | 34 +++++++++++++++++++ spi_comm/deg_to_byte.py | 5 +++ spi_comm/openmv_slave/spi_com_master.py | 36 +++++++++++++++++++++ upper_connection_schema.pdf | Bin 0 -> 168395 bytes 8 files changed, 159 insertions(+) create mode 100644 lower_connection_schema.pdf create mode 100644 spi_comm/arduino_binairy_decoder/arduino_binairy_decoder.ino create mode 100644 spi_comm/arduino_master_read/arduino_master_read.ino create mode 100644 spi_comm/arduino_slave_read/arduino_slave_read.ino create mode 100644 spi_comm/arduino_slave_write/arduino_slave_write.ino create mode 100644 spi_comm/deg_to_byte.py create mode 100644 spi_comm/openmv_slave/spi_com_master.py create mode 100644 upper_connection_schema.pdf diff --git a/lower_connection_schema.pdf b/lower_connection_schema.pdf new file mode 100644 index 0000000..704a16c Binary files /dev/null and b/lower_connection_schema.pdf differ diff --git a/spi_comm/arduino_binairy_decoder/arduino_binairy_decoder.ino b/spi_comm/arduino_binairy_decoder/arduino_binairy_decoder.ino new file mode 100644 index 0000000..eb257e5 --- /dev/null +++ b/spi_comm/arduino_binairy_decoder/arduino_binairy_decoder.ino @@ -0,0 +1,28 @@ +String inp = "01101000"; +int decimalValue = 0; + +void setup() { + Serial.begin(115200); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB + } + Serial.println(""); + + Serial.println(inp); + String binary = inp.substring(2); + Serial.println(binary); + for (int i = 0; i < binary.length(); i++) { + // Calculate the value of the current bit + int bitValue = binary.charAt(i) - '0'; + // Update the decimal value by shifting the bit to its correct position + decimalValue = (decimalValue << 1) | bitValue; + } + Serial.println(decimalValue); + Serial.println(""); +} + +void loop() { + // put your main code here, to run repeatedly: + delay(1000); + +} diff --git a/spi_comm/arduino_master_read/arduino_master_read.ino b/spi_comm/arduino_master_read/arduino_master_read.ino new file mode 100644 index 0000000..e31eac9 --- /dev/null +++ b/spi_comm/arduino_master_read/arduino_master_read.ino @@ -0,0 +1,26 @@ +#include + +int x; + +void setup() { + Serial.begin(115200); + SPI.begin(); + SPI.setClockDivider(SPI_CLOCK_DIV8); + digitalWrite(SS,HIGH); + +} + +void loop() { + byte Mastersend,Mastereceive; + digitalWrite(SS, LOW); + x = 1; + Mastersend = x; + Mastereceive=SPI.transfer(Mastersend); + if(Mastereceive == 1){ + Serial.println("Master receive 1"); + } + else{ + Serial.println("Master receive 0"); + } + +} diff --git a/spi_comm/arduino_slave_read/arduino_slave_read.ino b/spi_comm/arduino_slave_read/arduino_slave_read.ino new file mode 100644 index 0000000..8264ae3 --- /dev/null +++ b/spi_comm/arduino_slave_read/arduino_slave_read.ino @@ -0,0 +1,30 @@ +#include + +volatile boolean received; +volatile byte receivedData; + +ISR (SPI_STC_vect) //Inerrrput routine function +{ + receivedData = SPDR; // Get the received data from SPDR register + received = true; // Sets received as True +} + +void setup() +{ + Serial.begin(115200); + + pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT + SPCR |= _BV(SPE); //Turn on SPI in Slave Mode + received = false; + SPI.attachInterrupt(); //Activate SPI Interuupt +} + + +void loop() +{ + if(received) { + SPDR = receivedData; // send back the received data, this is not necessary, only for demo purpose + received = false; + Serial.print(receivedData, HEX); + } +} diff --git a/spi_comm/arduino_slave_write/arduino_slave_write.ino b/spi_comm/arduino_slave_write/arduino_slave_write.ino new file mode 100644 index 0000000..d6f838f --- /dev/null +++ b/spi_comm/arduino_slave_write/arduino_slave_write.ino @@ -0,0 +1,34 @@ +#include + +int x; +long randNumber; + + +volatile boolean received; +volatile byte Slavereceived,Slavesend; + +void setup() { + Serial.begin(115200); + randomSeed(analogRead(0)); + + pinMode(MISO,OUTPUT); + SPCR |= _BV(SPE); + received = false; + SPI.attachInterrupt(); + +} + +ISR (SPI_STC_vect){ + Slavereceived = SPDR; + received = true; +} + +void loop() { + // put your main code here, to run repeatedly: + randNumber = random(0, 1); + Serial.println(randNumber); + Slavesend=randNumber; + SPDR = Slavesend; + delay(1000); + +} diff --git a/spi_comm/deg_to_byte.py b/spi_comm/deg_to_byte.py new file mode 100644 index 0000000..196d4a5 --- /dev/null +++ b/spi_comm/deg_to_byte.py @@ -0,0 +1,5 @@ +deg = 40 +sbinary = format(deg, 'b') +print(sbinary) +binary = "".join(("01", sbinary)) +print(binary) \ No newline at end of file diff --git a/spi_comm/openmv_slave/spi_com_master.py b/spi_comm/openmv_slave/spi_com_master.py new file mode 100644 index 0000000..6e10881 --- /dev/null +++ b/spi_comm/openmv_slave/spi_com_master.py @@ -0,0 +1,36 @@ +from machine import SPI, Pin +deg = 40 +binary = format(deg, 'b') +print(binary) + +spi = SPI(0, baudrate=400000) # Create SPI peripheral 0 at frequency of 400kHz. + # Depending on the use case, extra parameters may be required + # to select the bus characteristics and/or pins to use. +cs = Pin(4, mode=Pin.OUT, value=1) # Create chip-select on pin 4. + +try: + cs(0) # Select peripheral. + spi.write(b"12345678") # Write 8 bytes, and don't care about received data. +finally: + cs(1) # Deselect peripheral. + +try: + cs(0) # Select peripheral. + rxdata = spi.read(8, 0x42) # Read 8 bytes while writing 0x42 for each byte. +finally: + cs(1) # Deselect peripheral. + +rxdata = bytearray(8) +try: + cs(0) # Select peripheral. + spi.readinto(rxdata, 0x42) # Read 8 bytes inplace while writing 0x42 for each byte. +finally: + cs(1) # Deselect peripheral. + +txdata = b"12345678" +rxdata = bytearray(len(txdata)) +try: + cs(0) # Select peripheral. + spi.write_readinto(txdata, rxdata) # Simultaneously write and read bytes. +finally: + cs(1) # Deselect peripheral. diff --git a/upper_connection_schema.pdf b/upper_connection_schema.pdf new file mode 100644 index 0000000..490a60f Binary files /dev/null and b/upper_connection_schema.pdf differ -- cgit v1.2.3