Serial Communication is one of the most widely used methods of communications is microcontrollers. It provides a low cost solution for interfacing with the PC. Most of the micro controllers have one UART port, some have two UART ports but some lower end microcontrollers do not have a single UART port. Serial communication is also used for interfacing modules like GSM modems, GPS modules, RFID reader etc so what if you are using 8051 based controller and has just one UART port and want to serially interface more than one device to the same controller? | |||
You can use a method called as “BIT BANGING”. Bit Banging basically uses Software for communication instead of a dedicated hardware. In this method Transmitting is done by alternating the Transmit Pin after specific interval, this time interval depends on the Baud rate for serial communication. In same way Receive function is performed by sampling the pin at regular interval. This is quite a tedious process and puts too much load the Micro controller processing. | |||
Over here I will be explaining how to send and receive a byte using this method. This polling interval is decided by the communication Baud rate e.g. if the baud rate is 9600 bps then the polling interval will be 1/9600 seconds i.e.104µS. | |||
|
|||
Figure 1 shows the basic Frame for serial communication. You can see the basic frame consists of Start Bit which is followed by the Data and end of transmission is indicated by a Stop Bit. | |||
ALGORITHM for Sending Byte:-
|
|||
Receiving a byte is more difficult than sending it. Since it is not possible to continuously poll the Rx pin we will be using external interrupt function for detecting the START bit of serial communication. Once this START bit has been detected we poll the Rx pin at regular interval. | |||
We will be using P3.2 (INT0) as the Rx pin so that we can use the External Interrupt 0 to detect the Start Bit. Normally the Rx pin is high so when the pin becomes low i.e. Start Bit is received an interrupt is generated. The controller then deactivates the External Interrupts and starts polling the Rx pin at regular interval after getting next eight bits we confirm the receiving of the Stop bit and the External Interrupt 0 is activated again for receiving the next byte. | |||
ALGORITHM for Receiving Byte:-
|