Go Down

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

mattallen37


Nick Gammon

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.

drone

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)) );
   
   _delay_us(SER_CLEAR_TM);
   
   PORTD &= ~_BV(PORTD5);
   
   
}

mattallen37

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

Using flush and then waiting 1ms is fine.

Go Up