Serial Port Complete - Latest Microcontroller projects

(lily) #1
Inside RS-485

In the PIC18F4520 and other PIC® microcontrollers, the TXSTA register’s
TRMT bit indicates whether the transmit shift register is empty or full. Chap-
ter 11 has more about PIC18F4520 programming.
In PCs, .NET’s SerialPort class includes the BytesToWrite property, which
returns the number of bytes in the transmit buffer. However, BytesToWrite
returns zero when the UART’s hardware buffer still contains bytes to transmit
and when the final byte is transmitting. If you use BytesToWrite to find out if
the data has transmitted, you’ll need to add a delay after BytresToWrite = 0 to
allow the UART’s buffer to empty. Chapter 9 and Chapter 10 have more about
.NET programming.




  
 
A transmitting computer can ensure that all data has transmitted by reading the
data back after it transmits. This method also detects problems such as a dis-
connected transceiver or multiple drivers enabled at the same time. To enable
reading back the data, the RS-485 receiver must be enabled when transmitting.
To use this method, the program code sends the data and read back the data
just transmitted. To verify a match, the transmitting computer can just count
the received bytes or compare the received data with the bytes transmitted. On
receiving the expected data or number of bytes, the transmitting computer can
disable its driver. If the data doesn’t appear or the values don’t match, the trans-
mitting computer can retry or give up.





 





A calculated delay is another approach to determining when to disable the driv-
ers. Program code uses the bit rate and number of bytes to calculate the time
required to send the data. The code enables the driver, sends the data and waits
the calculated time before disabling the driver. If using flow control, the time
required to transmit a block of data might be impossible to calculate. A solution
is to calculate and wait a 1-byte delay time just after the final byte of data trans-
mits.

9   


Hardware methods of controlling the driver-enable line can be efficient and
eliminate the need for dedicating a port bit and program code to control the
bit.
Free download pdf