In our previous tutorial learned about SPI communication in Arduino. Today we will learn about another Serial Communication Protocol: I2C (Inter Integrated Circuits). Comparing I2C with SPI, I2C has only two wires while SPI uses four and I2C can have Multiple Master and Slave, while SPI can have only one master and multiple slaves. So there are more than one microcontroller in a project that need to be masters then I2C is used. I2C communication is generally used to communicate with Gyroscope, accelerometer, barometric pressure sensors, LED displays etc.
- Serial Communication Programs Arduino Free
- Serial Communication Programs Arduino Software
- Serial Communication Programs Arduino Download
- Serial Communication Programs Arduino Pdf
Code in a sketch can use the same USB / serial port to communicate with the PC by using the Arduino IDE Serial Monitor window, or a Processing application for example. The USB port appears as a virtual COM port on the PC. This article shows how to use Arduino serial ports when additional serial ports are needed for a project. In today's Arduino tutorial, we go over the Uno's built in serial communication. We look at talking to the Arduino from the computer and vice versa, the difference between print and write, and my.
Simple Arduino Project - Serial Communication Demonstration Project. Hello world Program in Arduino: In our previous lesson, we have understand the Arduino based LED blinking Program. Now we will learn how to communicate Arduino board through serial communication. Don't underestimate this type of simple basic arduino programs. This tutorial will focus on Arduino-Arduino communication through the serial ports (RX and TX). The schematic below shows how to connect the two Arduinos together. This shows two Unos, but if a Mega is used, it can be connected to any of the Serial ports on the Mega as long as that is accounted for in the code.
In this Arduino I2C tutorial we will use I2C communication between two arduino boards and send (0 to 127) values to each other by using potentiometer. Values will be displayed on the 16x2 LCD connected to each of the Arduino. Here one Arduino will act as Master and another one will act as Slave. So let’s start with the introduction about I2C communication.
What is I2C Communication Protocol?
The term IIC stands for “Inter Integrated Circuits”. It is normally denoted as I2C or I squared C or even as 2-wire interface protocol (TWI) at some places but it all means the same. I2C is a synchronous communication protocol meaning, both the devices that are sharing the information must share a common clock signal. It has only two wires to share information out of which one is used for the cock signal and the other is used for sending and receiving data.
How I2C Communication works?
I2C communication was first introduced by Phillips. As said earlier it has two wires, these two wires will be connected across two devices. Here one device is called a master and the other device is called as slave. Communication should and will always occur between two a Master and a Slave. The advantage of I2C communication is that more than one slave can be connected to a Master.
The complete communication takes place through these two wires namely, Serial Clock (SCL) and Serial Data (SDA).
Serial Clock (SCL): Shares the clock signal generated by the master with the slave
Serial Data (SDA): Sends the data to and from between the Master and slave.
At any given time only the master will be able to initiate the communication. Since there is more than one slave in the bus, the master has to refer to each slave using a different address. When addressed only the slave with that particular address will reply back with the information while the others keep quit. This way we can use the same bus to communicate with multiple devices.
The voltage levels of I2C are not predefined. I2C communication is flexible, means the device which is powered by 5v volt, can use 5v for I2C and the 3.3v devices can use 3v for I2C communication. But what if two devices which are running on different voltages, need to communicate using I2C? A 5V I2C bus can’t be connected with 3.3V device. In this case voltage shifters are used to match the voltage levels between two I2C buses.
There are some set of conditions which frame a transaction. Initialization of transmission begins with a falling edge of SDA, which is defined as ‘START’ condition in below diagram where master leaves SCL high while setting SDA low.
As shown in the above diagram below,
The falling edge of SDA is the hardware trigger for the START condition. After this all devices on the same bus go into listening mode.
In the same manner, rising edge of SDA stops the transmission which is shown as ‘STOP’ condition in above diagram, where the master leaves SCL high and also releases SDA to go HIGH. So rising edge of SDA stops the transmission.
R/W bit indicates the direction of transmission of following bytes, if it is HIGH means the slave will transmit and if it is low means the master will transmit.
Each bit is transmitted on each clock cycle, so it takes 8 clock cycles to transmit a byte. After each byte either sent or received, ninth clock cycle is held for the ACK/NACK (acknowledged/not acknowledged). This ACK bit is generated by either slave or master depending upon the situation. For ACK bit, SDA is set to low by master or slave at 9th clock cycle. So it is low it considered as ACK otherwise NACK.
Where to use I2C communication?
I2C communication is used only for short distance communication. It is certainly reliable to an extent since it has a synchronised clock pulse to make it smart. This protocol is mainly used to communicate with sensor or other devices which has to send information to a master. It is very handy when a microcontroller has to communicate with many other slave modules using a minimum of only wires. If you are looking for a long range communication you should try RS232 and if you are looking for more reliable communication you should try the SPI protocol.
I2C in Arduino
The image below shows the I2C pins present in Arduino UNO.
I2C Line | Pin in Arduino |
SDA | A4 |
SCL | A5 |
Before we get start into programming I2C using two Arduino. We need to learn about the Wire library used in Arduino IDE.
The library <Wire.h> is included in the program for using the following functions for I2C communication.
1. Wire.begin(address):
Use: This library is used for making communication with I2C devices. This Initiate the Wire library and join the I2C bus as a master or slave.
Address: The 7-bit slave address is optional and if the address is not specified, it joins the bus as a master like this [Wire.begin()].
2. Wire.read():
Use: This function is used to read a byte that was received from master or slave device, either that was transmitted from a slave device to a master device after a call to requestFrom() or was transmitted from a master to a slave.
3. Wire.write():
Use: This function is used to write data to a slave or master device.
Slave to Master: Slave writes data to a master when Wire.RequestFrom() is used in master.
Master to Slave: For transmission from a master to slave device Wire.write() is used in-between calls to Wire.beginTransmission() and Wire.endTransmission().
Wire.write() can be written as:
- Wire.write(value)
value: a value to send as a single byte.
- Wire.write(string) :
string: a string to send as a series of bytes.
- Wire.write(data, length):
data: an array of data to send as bytes
length: the number of bytes to transmit.
4. Wire.beginTransmission(address):
Use: This function is used to begin a transmission to the I2C device with the given slave address. Subsequently, build queue of bytes for transmission with the write() function and then transmit them by calling endTransmission() function. 7-bit address of the device is transmitted.
5. Wire.endTransmission();
Use: This function is used to end a transmission to a slave device that was begun by beginTransmission() and transmits the bytes that were queued by Wire.write().
6. Wire.onRequest();
Use: This function gets called when a master requests data using Wire.requestFrom() from the slave device. Here we can include Wire.write() function to send data to the master.
Use: This function gets called when a slave device receives a data from a master. Here we can include Wire.read(); function to read the data sent from master.
8. Wire.requestFrom(address,quantity);
Use: This function is used in the master to request bytes from a slave device. The function Wire.read() is used to read the data sent from the slave device.
address: the 7-bit address of the device to request bytes from
quantity: the number of bytes to request
Components Required
- Arduino Uno (2-Nos)
- 16X2 LCD display module
- 10K Potentiometer (4-Nos)
- Breadboard
- Connecting Wires
Circuit Diagram
Working Explanation
Here for demonstrating I2C communication in Arduino, we use Two Arduino UNO with Two 16X2 LCD display attached to each other and use two potentiometers at both arduino to determine the sending values (0 to 127) from master to slave and slave to master by varying the potentiometer.
We take input analog value at arduino pin A0 from (0 to 5V) by using potentiometer and convert them into Analog to Digital value (0 to 1023). Then these ADC values are further converted into (0 to 127) as we can send only 7-bit data through I2C communication. The I2C communication takes place through two wires at pin A4 & A5 of both arduino.
Values at Slave Arduino’s LCD will be changed by varying the POT at master side and vice versa.
I2C Programming in Arduino
This tutorial has two programs one for master Arduino and other for slave Arduino. Complete programs for both the sides are given at the end of this project with a demonstration Video.
Master Arduino Programming Explanation
1. First of all we need to include the Wire library for using I2C communication functions and LCD library for using LCD functions. Also define LCD pins for 16x2 LCD. Learn more about interfacing LCD with Arduino here.
2. In void setup()
- We Start Serial Communication at Baud Rate 9600.
- Next we start the I2C communication at pin (A4,A5)
- Next we initialize LCD display module in 16X2 mode and display the welcome message and clear after five seconds.
3. In void loop()
- First we need to get data from the Slave so we use requestFrom() with the slave address 8 and we request one byte
The received value is read using Wire.read()
- Next we need to read the analog value from the master arduino POT attached to pin A0
We convert that value in terms of one byte as 0 to 127.
- Next we need to send those converted values so we begin the transmission with slave arduino with 8 address
- Next we display those received values from the slave arduino with a delay of 500 microseconds and we continuously receive and display those value.
Slave Arduino Programming Explanation
1. Same as master, first of all we need to include the Wire library for using I2C communication functions and LCD library for using LCD functions. Also define LCD pins for 16x2 LCD.
2. In void setup()
- We Start Serial Communication at Baud Rate 9600.
- Next we start the I2C communication at pin (A4, A5) with slave address as 8. Here it is important to specify the slave address.
Next we need to call the function when Slave receives value from master and when Master request value from Slave
- Next we initialize LCD display module in 16X2 mode and display the welcome message and clear after five seconds.
3. Next we have two functions one for request event and one for receive event
For request Event
When Master request value from slave this function will be executed. This function does take input value from the Slave POT and convert it in terms of 7-bit and send that value to master.
Samsung rv511 drivers. For Receive Event
When Master sends data to slave with slave address (8) this function will be executed. This function reads the received value from master and store in a variable of type byte.
4. In Void loop():
We display the received value from master continuously in the LCD display module.
Watch out, because here comes Bookworm Adventures Deluxe, a spelling sensation of epical proportions! Download bookworm deluxe. Help Lex the Bookworm confront the most terrible threat the Great Library has ever known by spelling words to vanquish villains, whomp foes and annihilate enemies. Discover gem tiles, magic potions and fabulous treasures as you battle through three enchanting storybooks, lots of engrossing mini-games and the frenzied Arena mode!
By rotating the Potentiometer at one side, you can see the varying values on LCD on another side:
So this is how I2C communication takes place in Arduino, here we have use two Arduinos to demonstrate not only sending of data but also receiving the data using I2C communication. So now you can interface any I2C sensor to Arduino.
The complete coding for Master and Slave Arduino is given below with a demonstration video
Master Arduino Programming
//I2C MASTER CODE
//I2C Communication between Two Arduino
//Circuit Digest
//Pramoth.T
#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print('Circuit Digest'); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print('I2C 2 ARDUINO'); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(); //Begins I2C communication at pin (A4,A5)
}
void loop()
{
Wire.requestFrom(8,1); // request 1 byte from slave arduino (8)
byte MasterReceive = Wire.read(); // receive a byte from the slave arduino and store in MasterReceive
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte MasterSend = map(potvalue,0,1023,0,127); //Convert digital value (0 to 1023) to (0 to 127)
Wire.beginTransmission(8); // start transmit to slave arduino (8)
Wire.write(MasterSend); // sends one byte converted POT value to slave
Wire.endTransmission(); // stop transmitting
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print('>> Master <<'); //Prints >> Master << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print('SlaveVal:'); //Prints SlaveVal: in LCD
lcd.print(MasterReceive); //Prints MasterReceive in LCD received from Slave
Serial.println('Master Received From Slave'); //Prints in Serial Monitor
Serial.println(MasterReceive);
delay(500);
lcd.clear();
}
Slave Arduino Programming
//I2C SLAVE CODE
//I2C Communication between Two Arduino
//CircuitDigest
//Pramoth.T
#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
byte SlaveReceived = 0;
void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print('Circuit Digest'); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print('I2C 2 ARDUINO'); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(8); //Begins I2C communication with Slave Address as 8 at pin (A4,A5)
Wire.onReceive(receiveEvent); //Function call when Slave receives value from master
Wire.onRequest(requestEvent); //Function call when Master request value from Slave
}
void loop(void)
{
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print('>> Slave <<'); //Prints >> Slave << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print('MasterVal:'); //Prints MasterVal: in LCD
lcd.print(SlaveReceived); //Prints SlaveReceived value in LCD received from Master
Serial.println('Slave Received From Master:'); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent (int howMany) //This Function is called when Slave receives value from master
{
SlaveReceived = Wire.read(); //Used to read value received from master and store in variable SlaveReceived
}
void requestEvent() //This Function is called when Master wants value from slave
{
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte SlaveSend = map(potvalue,0,1023,0,127); // Convert potvalue digital value (0 to 1023) to (0 to 127)
Wire.write(SlaveSend); // sends one byte converted POT value to master
}
In this tutorial , we will see a serial communication programming of 8051 microcontroller. when electronic devices communicate with each other, They can transfer data in two different ways.One is serial and other one is parallel, When digital data is transferred serially, it is transmitted bit by bit, whereas in parallel transfer, many bits are transmitted at same time. Though parallel transfer of data is much faster but requires many wires. while serial transfer is slower as compared to parallel transfer but requires few wires. Serial communication maybe synchronous or asynchronous. In synchronous communication, transmitter also transmits a clock along with data. This clock is used for synchronization between transmitter and receiver device In asynchronous transfer of data, There is no clock.
Install Fresco Logic USB3.0 VGA Display driver for Windows 10 x64, or download DriverPack Solution software for automatic driver installation and update. The FL2000DX is Fresco Logic’s USB 3.0 Display device controller. It integrates Fresco Logic’s display transfer engine, USB 3.0 device controller, USB 3.0 transceiver, and a VGA (D-Sub) DAC. Fresco Logic’s display transfer engine is designed with Fresco Logic’s proprietary architecture and processes the video stream optimally for USB 3.0 bandwidth. Fresco logic usb display driver linux. Jun 24, 2017 This package contains the files needed for installing the Fresco Logic FL2000 USB 3.0 Display Controller Driver. If it has been installed, updating (overwrite-installing) may fix problems, add new functions, or expand existing ones. The package provides the installation files for Fresco Logic FL2000 USB Display Adapter Driver version 1.1.325.0. If the driver is already installed on your system, updating (overwrite-installing) may fix various issues, add new functions, or just upgrade to the available version.
Page Contents
- 3 8051 SERIAL COMMUNICATION programming registers
- 5 Example of serial communication programming using Mikro C for 8051
Introduction to SERIAL COMMUNICATION with 8051 microcontroller
serial communication maybe simplex, half-duplex or full duplex. simplex communication means that data will be transmitted only in one direction while half duplex means data will be transmitted in both directions but at one time, only one device can transmit, whereas full duplex means data may be transmitted in both directions at one time, while one device is transmitting, it can also receive data transmitted from other device at same time. As I have mentioned before, transmitter and receiver are configured to communicate at some data transfer rate before communication starts. This data transfer rate or number of bits transmitted per second is called baud rate for handling serial communication.
Parallel communication is fast but it is not applicable for long distances (for printers). Moreover it is also expensive. Serial is not much fast as parallel communication but it can deal with transmission of data over longer distances (for telephone line, ADC, DAC). It is also cheaper and requires less physical wires, that’s why we use serial communication. This article also deals with how to serially communicate in 8051 microcontroller.
METHODS OF SERIAL COMMUNICATION
There are two methods of Serial Communication:
SYNCHRONOUS: Transfer the block of data (characters) between sender and receiver spaced by fixed time interval. This transmission is synchronized by an external clock.
ASYNCHRONOUS: There is no clock involved here and transmission is synchronized by special signals along the transmission medium. It transfers a single byte at a time between sender and receiver along with inserting a start bit before each data character and a stop bit at its termination so that to inform the receiver where the data begins and ends. An example is the interface between a keyboard and a computer. Keyboard is the transmitter and the computer is the receiver. We use USART and UART for serial communications. USART or UART is a microcontroller peripheral which converts incoming and outgoing bytes of data into a serial bit stream. Both have same work but with different methods which is explained below.
USART:
USART stands for Universal Synchronous/Asynchronous Receiver-Transmitter. USART uses external clock so it needs separate line to carry the clock signal. Sending peripheral generates a clock and the receiving peripheral recover from the data stream without knowing the baud rate ahead of time. By use of external clock, USART’s data rate can be much higher (up to rates of 4 Mbps) than that of a standard UART.
UART:
It stands for Universal Asynchronous Receiver-Transmitter. A UART generates its internal data clock to the microcontroller. It synchronizes that clock with the data stream by using the start bit transition. The receiver needs the baud rate to know ahead of time to properly receive the data stream.
8051 SERIAL COMMUNICATION programming registers
8051 microcontroller has a built-in serial port called UART. We can easily read and write values to the serial port. For using serial port we simply need to configure the serial port:
- Operation mode (how many data bits we want)
- Baud rate
There are 21 Special function registers (SFR) in 8051 microcontroller and 21 unique locations are for these 21 SFR. Each of these register is of 1 byte size. The “Serial Control” (SCON) is the SFR which is used to configure serial port.
SCON SFR:
SM0: Serial port mode
Bit 0 of serial port mode.
SM1: Serial port mode
Bit 1 of serial port mode.
SM2: Enable multiprocessor
Enable multiprocessor communication in modes 2 and 3 (for 9 bit UART).
REN: Receiver Enable
Set/clear by software to enable/disable receive operation
TB8: Transmit bit 8
Set or clear by software. The 9 bits will be transmitted in mode 2 and 3.
TB8 = 1, a value is written to the serial port, 9th bit of data = 1.
TB8 = 0, 9th bit of data = 0, RI will not set.
RB8: Receive bit 8
Set or clear by software. The 9 bits will be received in mode 2 and 3. First eight bits are the data received and 9th bit received will be placed in RB8.
TI: Transmit Interrupt flag
Set by hardware when a byte is transmitted completely. Now the port is free and ready to send the next byte. This bit must be cleared by software.
RI: Receive Interrupt flag
Set by hardware when a byte has been completely received. This lets the program to know that it needs to read the value quickly before another byte is read. This bit must be cleared by software.
CONFIGURATION SETTINGS
- Setting Operation Mode of Serial Port:
The Upper four bits are configuration bits. SM0 and SM1 set the serial mode between 0 and 3. The four modes along with the baud rates are in the table given below. In modes 0 and 2 the baud rate is fixed which is based on the oscillator frequency. In modes 1 and 3 the baud rate is variable which depends on how often Timer 1 overflows.
SM0 | SM1 | MODE | Description | Baud rate |
---|---|---|---|---|
0 | 0 | 0 | shift register | (Fosc./12) |
0 | 1 | 1 | 8 bit UART | Variable (set by Timer 1) |
1 | 0 | 2 | 9 bit UART | (Fosc./64) |
1 | 1 | 3 | 9 bit UART | Variable (set by Timer 1) |
SM2 is a flag bit for Multiprocessor communication. When SM2 is set, the 8051 microcontroller will set the RI (Receive Interrupt) flag whenever a byte is received (if the 9th bit received is “1”). By this way, program knows that a byte has been received and it needs to be processed. If 9th bit is clear, the RI flag will never be set. We will clear this RI bit so that the flag is set upon reception of any character.
If SM2=1, RI=1 (only if 9th bit of received byte is 1)
REN is Receiver Enable bit. Set this bit if we want to receive data through serial port.
REN=1 (for serial communication)
- Setting the Baud Rate of Serial Port:
Baud Rate:
It is defined as number of bits transmitted or received per second and is usually expressed in Bits per second bps. After setting the operation mode of serial port, the program must configure the serial ports baud rate which is only applicable to Serial Port modes 1 and 3. For mode 0 and 2 oscillator frequency determines the Baud Rate.
Csi 2130 analyzer manual. The CSI 2130 setup must match the 'Comm Setup' selection in the CSI 2130. The analyzer can only communicate using one method at a time. Therefore, the analyzer will need to be reconfigured in order to switch from. USB to Ethernet communication. Please see the CSI 2130 manual for information. Concerning configuring communication setup. This manual is provided for informational purposes. COMPUTATIONAL SYSTEMS, INCORPORATED MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-NESS FOR A PARTICULAR PURPOSE. View and Download Emerson CSI 2140 user manual online. Machinery Health Analyzer. Note You can use the CSI 2130 A646 headphones with the Bluetooth functionality in the CSI 2140. You need the adapter cable from Emerson and an A2DP Bluetooth receiver.
For mode 0:
Baud rate is always the oscillator frequency divided by 12. For crystal of frequency 11.059MHz, Baud rate will be 921,583 baud.
Baud rate = (11.0592MHz/12) = 921,583 baud
For mode 2:
Baud rate is always the oscillator frequency divided by 64. For crystal of frequency 11.059MHz, Baud rate will be 172,797 baud.
Baud rate (11.0592MHz/64) = 172,797 baud
For mode 1 and 3:
For them, the baud rate is determined by how frequently timer 1 overflows. The more frequent timer 1 overflows, the higher the baud rate. There are many ways due to which timer 1 overflows at a rate that determines a baud rate. The most common method is:
- We put timer 1 in 8-bit auto-reload mode (timer mode 2).
- This 8 bit timer that allows only values of 00 to FFH loaded in timer register TH1.
- Set a reload value in TH1 that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate.
- If PCON.7 is clear (SMOD=0), then to generate a given baud rate, the value to be put in TH1 can be determined from an equation:
TH1 = 256 – ((Crystal / 384) / Baud)
- If the value to be load in TH1 by above equation is not in whole number then we can’t get accurate baud rate. For that case, set PCON.7 (SMOD=1). The baud rate gets double and the equation becomes:
TH1 = 256 – ((Crystal / 192) / Baud)
How to calculate baud rate for serial communication in 8051 microcontroller
Let us take an example. If we have crystal of frequency 11.059MHz and we want to configure the serial port to 19,200 baud rate, firstly we try equation 1. By using equation 1:
TH1 = 256 – ((Crystal / 384) / Baud)
TH1 = 256 – ((11.059MHz/ 384) / 19200 )
TH1 = 256 – 1.5 = 254.5
By setting 254 and 255, we don’t achieve accurate baud rate so we need to set PCON.7 (SMOD=1). This doubles the baud rate and we will use equation 2 for determining TH1 value. By using equation 2:
Serial Communication Programs Arduino Free
TH1 = 256 – ((Crystal / 192) / Baud)
TH1 = 256 – ((11.059MHz/ 192) / 19200)
TH1 = 256 – 3 = 253
Thus by setting TH1 to 253 we get correct frequency for 19,200 baud rate.
Example of serial communication programming using Mikro C for 8051
If you do not want to do complicated programming using serial communications registers, you can simply use Mikro c compiler which has built in libraries for the UART communication. Now I will explain about UART programming through mikro c and after that I will provide an example of UART in keil compiler. We can use built-in UART library for handling 8051 serial communication, there are several functions. In this library.
- UART_init() is used for initialization of 8051 UART module
- UART_init() is used for initialization of 8051 UART module
- UART_data_read() function checks if some data received on serial Port
- UART_data_read() function checks if some data received on serial Port, UART_read() reads one byte from serial port
- whereas UART_Read_Text() can read multiple bytes
- UART_Write() writes one byte to serial port. UART_Write() writes one byte to serial port
- while UART_Write_Text() can write multiple bytes.
- UART_Set_Active() function is only used in micro-controllers that support more than one UART ports.
In above we are transmitting data through UART port and also displaying same data on LCD. If you don’t know how to display data on LCD, you can check tutorial here. Now lets see the explanation of code.
- First we initialized serial port at 9600 baud rate using UART_Init() function.
- Next we need to Clear SMOD bit of PCON register
- Actually there is some bug in uart_init() function of UART library, it writes HIGH to SMOD bit that causes erroneous baud rate.
- we can send some string to serial port with uart_write_text() function.
- Now in while(1) loop, we are sending “P0+P1” string to serial port with uart_write_text function.
- now we can send same text array to serial port that is being display on character LCD.
- At the end, we are sending new line character, OK code is now ready we can compile and test it.
- It is recommended to select 11.0592MHz crystal frequency for using serial port 11.0592MHz crystal frequency can give exact clock rates for most of the common baud rates for the UART.
- we can use virtual terminal in Proteus for testing serial communication.
- TX pin of 8051 will be connected to RX of virtual terminal.
- In virtual terminal of proteus, you have to enter same baud rate as configured in your code.
- When you run the code, You will see, sum of P0 & P1 is appearing on virtual terminal,
Example of serial communication 8051 microcontroller in Keil
8051 microcontroller is used in the schematic. Crystal frequency is set to 11.059MHz. Baud rate used is 9600. A virtual terminal is used. The data coming from transmitter pin of controller will be displayed on virtual terminal. The send to the microcontroller is also received on receiver pin of controller which is sent through virtual terminal by pressing specific key. The program is such that the baud rate is set to 9600.
Serial Communication Programs Arduino Software
For 9600 baud rate:
TH1 = 256 – ((Crystal / 384) / Baud)
TH1 = 256 – ((11.59MHz/ 384) / 9600 )
TH1 = 256 – 3 = 253
The Hex value of 253 is FD which should be loaded to TH1
- Port 3 pin 0 receiver pin is connected to TX of virtual terminal.
- Port 3 pin 1 transmitter pin is connected to RX of virtual terminal.
- Port 1 lower four pins are connected with 4 LEDs.
PROTEUS SCHEMATIC:
WORKING of serial communication 8051 microcontroller
By pressing key 1, 1st LED will glow and when key “a” is pressed, it goes off.
By pressing key 2, 2nd LED will glow and when key “b” is pressed, it goes off.
By pressing key 3, 3rd LED will glow and when key “c” is pressed, it goes off.
By pressing key 4, 4th LED will glow and when key “d” is pressed, it goes off.
Serial Communication Programs Arduino Download
The virtual terminal will also show the ON and OFF of LEDs.