Very simple test program that transmits a message at regular intervals over an RS485 datalink. Turns off the transmitter 600 microseconds after the last byte in the transmission is put into the TX buffer. This may need adjusting. A much better solution would be to read the register that tells you when the transmitter is empty but I haven't figured this out yet.

Dependencies:   mbed

Fork of RS485Test by Richard Ellingworth

main.cpp

Committer:
RichardE
Date:
2012-11-05
Revision:
0:0db8165396ae
Child:
1:0e3453d2d0cc

File content as of revision 0:0db8165396ae:

/*
 * SOURCE FILE : main.cpp
 *
 * Test program to see if a 485 communications link is working.
 * This is designed to run on the LPC11U24 which only has a single
 * serial port which uses pin 9 for TX and pin 10 for RX. Note that
 * this is the same serial port that transmits over the USB cable to the PC.
 *
 */
 
#include "mbed.h"

/****************/
/* MAIN PROGRAM */
/****************/
int main() {
    // Digital output connected to on-board LED1.
    DigitalOut myled(LED1);
    // Digital output used to enable the 485 transmitter
    // and simultaneously disable the received.
    DigitalOut txEnable( p11 );
    txEnable = 0;
    // Serial port to PC and also to pins 9 and 10.
    Serial pc( USBTX, USBRX );
    // Set baud rate and protocol.
    pc.baud( 38400 );
    pc.format( 8, Serial::None, 1 );
    // Message to send.
    char message[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
    // Do following until power goes off.
    while( true ) {
        // Toggle state of LED.
        myled = ( myled ? 0 : 1 );
        // Turn on the transmitter and disable receiver.
        txEnable = 1;
        // Transmit a message.
        char *ptr = message;
        while( *ptr != 0 ) {
            if( pc.writeable() ) {
                pc.putc( *ptr++ );
            }
        }
        // Wait 300 micro seconds to let last byte transmit.
        wait_us( 300 );
        // Turn off the transmitter and enable the receiver.
        txEnable = 0;
        // Wait 1 second.
        wait_us( 1000000 );
    }
}