Arduino - serial communication and IIC

1. Serial communication

The fifth function of single chip microcomputer - serial communication. It can establish a direct connection between your computer and single chip microcomputer, or the connection of other modules, such as Bluetooth, WIFI, etc. the interfaces are serial ports. The interfaces of other modules are IIC or SPI. For example, gyroscopes are generally IIC, OLED display, and the interfaces are also IIC

Physically, serial communication requires three lines TX, RX and GND. TX is transmitting, Rx is receiving and GND is grounding. See the schematic diagram


We can see that RX RT converts the serial port into a USB port through a USB chip, so the USB cable inserted into our computer is a serial port and power supply function (pay attention to whether the data cable or power cable is used, and the COM port cannot appear in the power cable). A virtual com port will appear in our computer

The serial port has two functions. The first function is to send data and the second function is to receive data from outside. The data sent in is a string of hexadecimal arrays, such as 55 77 03 05 9A B4 FF FF ff. This string of arrays means that we have a scientific name, which is called protocol, also known as communication protocol. The so-called communication protocol means the value agreed by both sides of communication, In other words, the meaning of each hexadecimal number in sending this string of data is generally the same when we formulate the agreement. For example, 5577 represents the beginning, 03 05 78 9A B4, and these five data represent five parameters. Of course, the specific parameters are determined according to the specific needs, and the last two FF represent the end, Well, if we have agreed on such an agreement, you will know the meaning of each field after receiving it. If I don't know the agreement between the two sides, it will be difficult for me to intervene in your communication. For example, I want to repair a device. The two devices communicate through serial port,

Then I can listen to the content of their communication and write out the hexadecimal numbers of their communication, but I don't know what they mean. Only the equipment manufacturer knows this. This protocol is called private protocol.

Some protocols are open. In order to facilitate the docking of equipment from various manufacturers, as long as everyone follows the same protocol, our equipment can communicate with each other. Therefore, the products of large companies generally follow a standard protocol.

When it comes to serial communication, we also need to talk about a concept, that is, the baud rate of communication. The so-called baud rate is the number of bits of data sent in one second. Communication is between two devices, so their frequency must be consistent. Generally, the baud rate we use is 9600. The lower the baud rate, the slower the transmission speed. The higher the baud rate, the faster the data will be transmitted. There may be problems

int incomedate = 0;
void setup() {
 
  Serial.begin(9600); //Set serial port baud rate 9600
}
void loop() {
 
  if (Serial.available() > 0)//Serial port received data
  {
    incomedate = Serial.read();//Get the data received by the serial port
    if (incomedate == 'a') // Determine the characters we receive
    {
      Serial.println("ko no dio da!");See what is printed in the serial monitor
    }
  }
 
  delay(1000);//Cycle delay one second printing
 
}

Let's introduce the above functions

  • Serial.begin(speed) defines our baud rate
  • int Serial.available() determines the buffer status and returns the number of bytes received
  • int Serial.read() reads the serial port and returns the received data (that is, we need to assign this value to the thing we set)
  • Serial.flush() empties the buffer
  • Serial.print(data) serial port outputs data. Data can be any data type
  • Serial.println(data) serial port outputs data and brings back the vehicle symbol (blank line)

Let's sort out the whole process. At the beginning, the single chip microcomputer does nothing and waits there. When my computer sends a data to the single chip microcomputer, the single chip microcomputer will receive and judge the data. If it is the data I want, I will choose a branch to do one thing. What we do here is to feed back a string. If not, we can let him do another thing. Using this feature, we can light our lights remotely

For example, we add lighting in our judgment statement, so that after we receive the data we want, we can turn on the light

Next is our IIC communication
The code comes from huanghaoAudio

//I2C host
#include <Wire.h>
#define LED 13
 
//initialization
void setup()
{
  Wire.begin(); //host
  pinMode(LED,OUTPUT);
  Serial.begin(115200); 
}
//main program
void loop()
{
  Wire.beginTransmission(4); //Send data to the slave with device number 4
  Wire.write("OFF");        // Send string
  Wire.endTransmission();    // Stop sending
  request();               //Reply status
  delay(1000);
 
  Wire.beginTransmission(4); 
  Wire.write("ON");        
  Wire.endTransmission();  
  request();
  delay(1000);
    
 
}
void request()
{
    delay(10);
    Wire.requestFrom(4, 2);    //Notify slave 4 to upload 2 bytes
  String c;
  while(Wire.available()>0)    // When the host receives slave data
  { 
   c += char(Wire.read());   
  }
    Serial.print(c);
    if(c=="OK"){digitalWrite(LED,HIGH);}
    else {digitalWrite(LED,LOW);}
}
//I2C slave
#include <Wire.h>
#define LED 13
bool LED_STA;//Record LED status
//initialization
void setup()
{
  Wire.begin(4);                // Join the i2c bus and set the slave address to #4
  Wire.onReceive(receiveEvent); //Register events that receive host characters
  Wire.onRequest(requestEvent); // The registration host notifies the slave of the event of uploading data
  pinMode(LED,OUTPUT);//Set digital port 13 as output
  Serial.begin(115200);           //Set serial baud rate
}
//main program
void loop()
{
  delay(100);//delayed
}
 
// This event is executed when the slave receives the host character
void receiveEvent(int a)
{
  String c;
  while( Wire.available()>0) // 
  {
    c += char(Wire.read()); // Receive bytes as characters
  }
   Serial.print(c);         // Print characters to serial port monitor
    if(c=="ON"){LED_STA = 1; digitalWrite(LED,HIGH);}//Record LED status
    if(c=="OFF"){LED_STA = 0;digitalWrite(LED,LOW);}
 
}
 
//This event is executed when the host notifies the slave to upload data
void requestEvent()
{
  if(LED_STA == 1){Wire.write("OK"); }//If the light is on, reply OK
  else {Wire.write("NO");}
}

--------
Copyright notice: This article is the original article of CSDN blogger "ArtoriaLili", which follows the CC 4.0 BY-SA copyright agreement. For reprint, please attach the original source link and this notice.
Original link: https://blog.csdn.net/ArtoriaLili/article/details/121723604

Keywords: arduino

Added by cookspyder on Fri, 17 Dec 2021 17:21:46 +0200