Using a microcontroller is all about using and connecting to peripheral devices for the purpose of reading analog signal or sending commands. Examples of such works includes interfacing and controlling DAC(Digital to Analog Converters), interfacing and communication with external memory devices such as FLASH EEPROM, interfacing and communication with Bluetooth modules or computer etc.
In order for microcontroller to communicate with external devices several communication protocols have been developed. Most popular ones are the UART, SPI(Serial Peripheral Interface), I2C(Inter Integrated Circuits). The UART or USART can be asynchronous or synchronous while SPI and I2C are synchronous communication protocols. Protocols means a standard rule to follow for communication to take place.All of these protocols are serial communication protocols. Serial communication protocols happens using few wires whereas parallel communication requires many wires joining the microcontroller and the external device. Hence serial communication reduces the interconnecting number of wires.
Asynchronous is communication between devices where there is no mutual clocking. Synchronous means there is a clock setting between the communicating devices. In asynchronous communication, the devices know the state of the information flow because they send control signals once in a while. Whereas in synchronous communication, a common clock is first set up and both devices know the state of the information flow.
Most modern microcontrollers such as PIC18, PIC24, PIC32MX, AVR 32 etc provides all these kinds of communication interfaces to allow users to connect to different types of external devices.
Let’s start by looking into UART communication interface. Shown below is a block diagram that shows how and what wires are necessary for UART communication.
As you can a microcontroller like PIC32 is connected to a asynchronous peripheral device via UART communication lines. The microcontroller has UART build in inside it. It comprises of mainly of parallel to serial converter/serial to parallel converter made of shift registers, circuit to insert start, stop, parity bits etc.
The RTS(Ready To Send) and CTS(Clear To Send) are control lines that controls the communication between the devices. They are optional for general purpose less critical applications. The data is transferred using the two wires connecting the devices(Tx and Rx). You might ask how the receiving device knows what is start of the data and end of the data. This is made possible but sending start and stop bits and putting the data in between these bits. So when the receiver sees the start bit, it knows that the next 8 bit is data bits and the stop bit also indicates the end of the 8 bit data. To ensure that there is no error in the transmitted data, parity bits may be added.
Synchronous SPI(Serial Peripheral Interface)
Next type of communication interface you will find in microcontroller application is the SPI. In this type of communication, a common clock is set up between the two communicating devices and then data is transferred. Below shows a block diagram of SPI communication.
As you can see 3 wires are required for SPI communication between microcontroller and peripheral device. These wires includes wire for clock and two wires for data. One data wire is for receiving and the other is for transmitting data. The two data wire used allows one to send and receive data simultaneously which means a faster communication happens between the devices. In SPI, the clock is set up such that the clock is provided by one of the two communicating device. The device from which clock is extracted is called the Master and the other is called Slave.
Synchronous I2C(Inter Integrate Circuits)
Another popular mechanism for communication between devices is the I2C. In this protocol, only two wires are used. One is send/receive data and the other is for clock. Following shows the block diagram of I2C communication.
As in case of SPI, the data are transferred in synchronous with common clock. Again like SPI there is a master device and slave device depending upon who is setting up the clock. But what is different from SPI is that there are now only two wires required in I2C. The data is transmitted and received using the same data wire. While this technique reduces the number of wires required it also increases the time for data transfer or speed of communication.