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

Committer:
RichardE
Date:
Mon Nov 05 20:57:14 2012 +0000
Revision:
1:0e3453d2d0cc
Parent:
0:0db8165396ae
Transmits message at roughly 1 second intervals and other end receives OK.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RichardE 0:0db8165396ae 1 /*
RichardE 0:0db8165396ae 2 * SOURCE FILE : main.cpp
RichardE 0:0db8165396ae 3 *
RichardE 0:0db8165396ae 4 * Test program to see if a 485 communications link is working.
RichardE 0:0db8165396ae 5 * This is designed to run on the LPC11U24 which only has a single
RichardE 1:0e3453d2d0cc 6 * serial port. This program uses pin 9 for TX and pin 10 for RX.
RichardE 1:0e3453d2d0cc 7 * There is only one serial port on the LPC11U24 so you cannot use
RichardE 1:0e3453d2d0cc 8 * the USB serial port to the development PC at the same time.
RichardE 0:0db8165396ae 9 *
RichardE 0:0db8165396ae 10 */
RichardE 0:0db8165396ae 11
RichardE 0:0db8165396ae 12 #include "mbed.h"
RichardE 0:0db8165396ae 13
RichardE 0:0db8165396ae 14 /****************/
RichardE 0:0db8165396ae 15 /* MAIN PROGRAM */
RichardE 0:0db8165396ae 16 /****************/
RichardE 0:0db8165396ae 17 int main() {
RichardE 0:0db8165396ae 18 // Digital output connected to on-board LED1.
RichardE 0:0db8165396ae 19 DigitalOut myled(LED1);
RichardE 0:0db8165396ae 20 // Digital output used to enable the 485 transmitter
RichardE 0:0db8165396ae 21 // and simultaneously disable the received.
RichardE 0:0db8165396ae 22 DigitalOut txEnable( p11 );
RichardE 0:0db8165396ae 23 txEnable = 0;
RichardE 1:0e3453d2d0cc 24 // Serial port on pins 9 (TX) and 10 (RX).
RichardE 1:0e3453d2d0cc 25 Serial port485( p9, p10 );
RichardE 0:0db8165396ae 26 // Set baud rate and protocol.
RichardE 1:0e3453d2d0cc 27 port485.baud( 38400 );
RichardE 1:0e3453d2d0cc 28 port485.format( 8, Serial::None, 1 );
RichardE 0:0db8165396ae 29 // Message to send.
RichardE 1:0e3453d2d0cc 30 char message[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
RichardE 0:0db8165396ae 31 // Do following until power goes off.
RichardE 0:0db8165396ae 32 while( true ) {
RichardE 0:0db8165396ae 33 // Toggle state of LED.
RichardE 0:0db8165396ae 34 myled = ( myled ? 0 : 1 );
RichardE 0:0db8165396ae 35 // Turn on the transmitter and disable receiver.
RichardE 0:0db8165396ae 36 txEnable = 1;
RichardE 0:0db8165396ae 37 // Transmit a message.
RichardE 0:0db8165396ae 38 char *ptr = message;
RichardE 0:0db8165396ae 39 while( *ptr != 0 ) {
RichardE 1:0e3453d2d0cc 40 if( port485.writeable() ) {
RichardE 1:0e3453d2d0cc 41 port485.putc( *ptr++ );
RichardE 0:0db8165396ae 42 }
RichardE 0:0db8165396ae 43 }
RichardE 1:0e3453d2d0cc 44 // Wait a bit to let last byte transmit.
RichardE 1:0e3453d2d0cc 45 wait_us( 600 );
RichardE 0:0db8165396ae 46 // Turn off the transmitter and enable the receiver.
RichardE 0:0db8165396ae 47 txEnable = 0;
RichardE 0:0db8165396ae 48 // Wait 1 second.
RichardE 0:0db8165396ae 49 wait_us( 1000000 );
RichardE 0:0db8165396ae 50 }
RichardE 0:0db8165396ae 51 }