Hi
Now I'm creating a program where Razpai generates 4 bytes, sends them 1 byte each, connects them when Arduino receives 4 bytes, and restores the information. I wanted to send you a float. (To be honest, I couldn't find a unified site, including overseas sites. I've been looking for about 5 days.)
Well done, but in a fast loop, the receiver? can't reconstruct the number properly and spews out messy numbers. I think this is probably because Arduino missed the timing to reconstruct the number with the wrong combination. Arduino simply spews out +1 to counter == 4 every time you receive it. While there may be other problems, such as Razpai's not reporting bytes in the first place (Arduino is receiving 3.3V rectangular waves because he doesn't use a level shifter, isn't it good?), I'd like you to tell me what you think is the most off-timing problem.
//Arduino slave code
# include <SPI.h>
volatile uint32_t data[4];
volatile uint8_t count = 0;
union my_receive_data_converter_type{
uint8_bytes[4]; // Payload Bytes Included in One SPI Receive
float value;// float value established on 4 SPI receptions
};
void setup() {
Serial.begin (115200);
SPCR|=_BV(SPE);
SPI.attachInterrupt(); //(3)
}
void loop() {
}
US>ISR(SPI_STC_vect) {//(4)
data [count] = SPDR;
count+=1;
if(count==4){
count = 0;
my_receive_data_converter_typef;
for(inti=0;i<4;++i){f.bytes[i]=(uint8_t)(data[i]&0xFF);}
Serial.println((f.value));
}
}
Next, the master.pi will succeed only once, but it will crash if you send continuously like While loop.
#Raspi master code
import spreadv
import RPi.GPIO as GPIO
import structure
spi=spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 250000
spi.lsbfirst=False
channel = 2
TIME = 0
buff=structure.pack("<f", 3.141592)
bytelist=list(buff)
spi.xfer2 (bytelist)
# spi.xfer2 ([1])
spi.close()
"""
# ------- RUN main -----------------
while True:
Timer=time.perf_counter()
TIME+ = dt
buff=structure.pack("<f", TIME)
bytelist=list(buff)
spi.xfer2 (bytelist)
dt=time.perf_counter() - Timer
"""
Read carefully the datasheet of the device you are using.
SPI simply sends consecutive bytes of data, so if something happens along the way and you miss or recognize extra bits, subsequent data transmission/receipt will fail.
To prevent this from happening, the SPI device will synchronize with a CS (ChipSelect) signal.
I can't see this kind of synchronization operation in the code provided
It's not good to repeat the SPI device Open/Close every time you send one signal.Closing a device means opening the port, so you don't want to know what happens to the port in the meantime
# As with questions and answers to other questions, I don't think I have the skills to handle them yet.
© 2024 OneMinuteCode. All rights reserved.