Pages: 1 2 [3]   Go Down
Author Topic: How to know when serial has finished sending  (Read 2636 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 127
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here. It's basically a Leonardo, not an Uno.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19065
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 323
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
    
    
}
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 127
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Using flush and then waiting 1ms is fine.
Logged

Pages: 1 2 [3]   Go Up
Jump to: