I2C bus

1. Define the method

  a. Define the start signal

  b. Define the termination signal

  c. Define the microcontroller write function

  d. Define the readout function of the microcontroller

  e. Write to the specified component

  f. Read out from the specified component

  g. Write a header file as a relay

  h. Write a main function to implement the call to write and read

 

2. Define the start signal

  a. SDA high level delay

  b. SCL high level delay

  c. SDA low level delay

  d. SCL low level delay

  

    SDA = 1 ;
    Delay10us();
    SCL = 1;
    Delay10us();
    SDA = 0 ;
    Delay10us();
    SCL = 0;
    Delay10us();
View Code

  

 

 

3. Termination signal

  a. SDA low level delay

  b. SCL high level delay

  c. SDA high level delay

  

    SDA = 0 ;
    Delay10us();
    SCL = 1;
    Delay10us();
    SDA = 1 ;
    Delay10us();
View Code

 

  

4. MCU write function

  a. Defining a function requires a return value and requires an 8-bit formal parameter

  b. The for loop is 8 times, each time a data is sent, which is realized by shifting the data left and right. After each write to SDA, the SCL level should be pulled down

  c. After the loop is completed, pull up SDA and then pull up SCL to keep SDA unchanged

  d.while (SDA) loop waits for the component to respond. After the response, SDA will be pulled low and jump out of the loop

  e. return value

unsigned char sendaddr(unsigned char dat)
{
    unsigned char a=0,b=0;
    for(a=0;a<8;a++)
    {SDA = dat >> 7 ;
        that <<= 1 ;
        Delay10us();
        SCL = 1;
        Delay10us();
        SCL = 0;
        Delay10us();            
    }
    SDA = 1 ;
    Delay10us();
    SCL = 1;    
    while(SDA)
    { b++;
      if(b>200)
      { SCL = 0;
        Delay10us();
          return 0;
      }
    }
    SCL = 0;
    Delay10us();
    return 1;
}
View Code

5. MCU readout function  

  a. Defining a function requires a return value and no formal parameters

  b. Define variables as containers to receive data

  c. For loop 8 times, first pull the high level to keep SDA stable, num<<=1, num|=SDA pulls the low level

  e. return num

unsigned char reader()
{
 unsigned char a,num;
  num = 0;
 //SDA = 1;
  Delay10us();
  for(a=0;a<8;a++)
  {
      SCL = 1;
     Delay10us();
     num<<=1;
     num |= SDA;
     Delay10us();
     SCL=0;
     Delay10us();
  }
    return num;
}
View Code

6. Write component function

  a. Defining a function does not require a return value and requires two formal parameters

  b. Start signal >> write function (component address 0) >> write function (component first address) >> write function (write value) >> end signal

void A204write(unsigned char addr,unsigned char dat)
{
    start();
    sendaddr ( 0xa0 );
    sendaddr(addr);
    sendaddr (dat);
    stop();    

}
View Code   

7. Read function from component

  a. Defining a function with a return value does not require formal parameters

  b. Start signal >> write function (component address 0) >> write function (component first address) >> start signal >> write function (component address 1) >> receive the value of SDA> >termination signal

  c. return value  

unsigned char A204read(unsigned char addr)
{
    unsigned char num;
    start();
    sendaddr ( 0xa0 );    
    sendaddr(addr);
    start();
    sendaddr ( 0xa1 );
    num = reader();
    stop();
    return num;
}
View Code

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325227406&siteId=291194637