Go Down

Topic: How to know when serial has finished sending (Read 6612 times) previous topic - next topic



Waiting for Serial to finish on the Leonardo is a different kettle of fish, as that goes through the USB interface. Serial1 would be similar to Serial on the Uno, although I don't know about the register names on the Leonardo as it is a different chip.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


Matt, your RS485 transceiver should detail the time it takes to clock out a bit in the datasheet.  For very fast communication, you would need to know this (it's easy to wait too long).  For doing the same thing, I have (generally):

Code: [Select]

#include <util/delay.h>


#define SER_BPS 57600.0

// serial clear time in uS (how long it takes to punch one bit
// out through the RS485 transceiver at the current bitrate).
// the transceiver adds 0.07uS delay in punching out a bit

#define SER_CLEAR_TM 1000000.0 / SER_BPS + 0.07


void write(uint8_t p_dat) {
   // TODO: Abstract for non Atmega328P platforms
   while (!(UCSR0A & _BV(UDRE0)));
// set xmit pin high
   PORTD |= _BV(PORTD5);  
   // clear tx complete flag before write, just incase serial object doesn't
   UCSR0A |= _BV(TXC0);
   UDR0 = p_dat;

   while(!(UCSR0A & _BV(TXC0)) );
   PORTD &= ~_BV(PORTD5);


I am using the HW UART, which is called Serial1.

Using flush and then waiting 1ms is fine.

Go Up