Arduino Asked by Martin.G on September 25, 2021
I have to reproduce a signal (of 6 UART frames) which have 950 µs delay between each of the frames. Than, a delay of 37ms at the end of those frames.
Everything going right except the delay between each frame…The baud rate is at 9600.
Here is my current code :
Serial1.write(sw_binary_train_1);
delayMicroseconds(950);
Serial1.write(sw_binary_train_2);
delayMicroseconds(950);
Serial1.write(sw_binary_train_3);
delayMicroseconds(950);
Serial1.write(sw_binary_train_4);
delayMicroseconds(950);
Serial1.write(sw_binary_train_5);
delayMicroseconds(950);
Serial1.write(sw_binary_train_6);
delay(37);
It's not strange at all. Serial doesn't block while it sends. When you call write that pushes the byte into the send buffer and that's all. Then the code moves on to the next instruction, the delay(950) and during that time the serial data is actually being sent. It takes about 1 millisecond for that to happen.
This solution works as long as the send buffer doesn't have anything else in it that's slowing you down and as long as you don't change the baud rate. If either of those things happen then you need to recalculate that time.
A better solution would be:
Serial1.write(sw_binary_train_1);
Serial1.flush();
delayMicroseconds(950);
Serial1.write(sw_binary_train_2);
Serial1.flush();
delayMicroseconds(950);
Serial1.write(sw_binary_train_3);
Serial1.flush();
delayMicroseconds(950);
Serial1.write(sw_binary_train_4);
Serial1.flush();
delayMicroseconds(950);
Serial1.write(sw_binary_train_5);
Serial1.flush();
delayMicroseconds(950);
Serial1.write(sw_binary_train_6);
Serial1.flush();
delay(37);
The flush blocks until the send buffer is empty again. That way you don't have to account for that time and all you need the delay for is the time between transmissions.
Even better would be if those things were in an array so you didn't have to keep repeating yourself. Anytime you catch yourself putting numbers on variable names what you really want is an array so that you can use those numbers in the program:
for (int i=0; i<6; i++){
Serial1.write(sw_binary_train[i]);
Serial1.flush();
delayMicroseconds(950);
}
delay(37)
looks much better.
Answered by Delta_G on September 25, 2021
set delayMicroseconds(1900) do a delay of 950µs between each frames...Strange but works !
Answered by Martin.G on September 25, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP