A brief introduction to the i2c bus

2018/4/16

51 single chip microcomputer

1. Say before:

Today, my friend took an ADDA module, but when I saw the SDA and SCL above, I was instantly confused. I have to remember and review what I have learned, and learn while using it.

2.i2c

1. Introduction : It is a serial bus with a relatively simple structure, only SDA (data line) and SCL (time line)

2. Preliminary knowledge:

2-1. The bus uses a pull-up resistor to connect the positive power supply, the bus is idle (no data exchange), both lines are high and level, but if either is low, the bus level will be pulled down

2-2: When i2c performs data transmission, the data on the data line is in a stable state only when the clock signal is at a high level , and can only be modified on the data line when the clock signal is at a low level

2-3: When SCL is high, SDA changes from high to low as the starting state, SCL is high, and SDA changes from low to high to terminate

3. Mode configuration code

1. Timing diagram


Start bit configuration (I2cStart())

void Delay10us(void)
{
    unsigned char a,b;
    for(b=1,b>0;b++)
    {
        for(a=2;a>0;a--);
    }
}
//simulate start signal
void I2cStart()
{
    SDA=1;
    Delay10us();//10us
    SCL=1;
    Delay10us();//10us
    SDA=0;
    Delay10us();//10us
    SCL=0;
    Delay10us();//10us
}

Stop Bit Configuration (I2cStop())

void I2cStop()
{

    SDA=0;
    Delay10us();//10us
    SCL=1;
    Delay10us();//10us
    SDA=1;
    Delay10us();//10us
}

Judging whether the sent signal is correct or not (1/0)

unsigned char I2cSendByte(unsigned char dat)//Get the return value to judge whether it is sent correctly
{
   unsigned char a=0;
   for(a=0;a<8;a++)
   {
        SDA=dat>>7;//SDA to detect high bits
        dat=dat<<1; the loop pushes back
        Delay10us();//The bus timing is up to 4.7us
        SCL=1;//Keep the clock line stable
        Delay10us();
        SCL=0;//Change data
        Delay10us();
   }
   int b=0;
   while(SDA)//Set the case of no response, return 1
   {
        b++;//Set the variable
        if(b>200)//More than 200ms
        {
            SCL=0;
             Delay10us();

        }

   }
   SCL=0;
   Delay10us();
   return 1;//Successful sending, return 1

}

Read data (data stored by SDA)

//read data
unsigned char I2cReadByte()
{
    unsigned char a=0,dat;
    for(a=0;a<8;a++)
    {
        SCL=1;
        Delay10us();//Stable data
        dat<<=1;
        dat|=SDA;
        Delay10us();//Stable data
        SCL=0;
        Delay10us();




    }
    return dat;


}



Write data (write initial address and data)

void At24c02Write(unsigned char addr,unsigned char dat)
{
    I2cStart();
    I2cSendByte(0xa0);//Register operation, write
     //send data
     I2cSendByte(addr);//Write data
     I2cSendByte(dat);//Write data,
     I2cStop();//Stop

}

Read data (note the state inversion)

unsigned char At24c02Read(unsigned char addr)
{
    unsigned char num;
    I2cStart();
    I2cSendByte(0xa0);
    I2cSendByte(addr);//Send the first address
    I2cStart();
    I2cSendByte(0xa1);//Read data
    I2cstop();



Guess you like

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