Here are a few simple subroutines for serial communications. For more information on serial communication please check this article. |
|||||||||||||
baudrate: This subroutine initializes the controllers com port, initializes Timer 1 in Mode 2 i.e. for auto reload mode which is used to set the baudrate. We have used 9600 baudrate. |
|||||||||||||
|
|||||||||||||
send: This subroutine sends serially whatever data is in the accumulator i.e. loads it into SBUF and waits for the byte to be sent i.e. we wait till the TI (Transmit Interrupt) flag is set. |
|||||||||||||
|
|||||||||||||
receive: This subroutine waits till a byte is received i.e. till RI (receive Interrupt) flag is set. Then the received byte is copied into the accumulator. |
|||||||||||||
|
|||||||||||||
check_receive: Unlike the receive subroutine which waits till a byte is received this subroutine checks if a byte is received, if no byte is received the Carry flag is cleared and return. If a byte is received then the carry flag is set and the received byte is copied into the accumulator and return. |
|||||||||||||
|
|||||||||||||
send_newline: I find this subroutine very useful especially when i have to use HyperTerminal for checking the received data. This subroutine sends the newline character and the carriage return character. So basically the next byte that we send is displayed at the start of next line. |
|||||||||||||
|
|||||||||||||
Here is a complete program for serial communication. When Power is turned ON the Controller sends ‘DNA TECHNOLOGY’ serially to the PC and then waits for a byte to be received from the PC. The controller sends back whatever byte that has been received serially. All the bytes are sent on new line. |
|||||||||||||
|
You can use Hyper Terminal for sending and receiving bytes serially. Click here to see how to setup Hyper Terminal for serial communication.