Author: Zhou Zhiqiang,Hua Qing vision embedded training center lecturer.
1) IIC initialization: reset the IIC to enable the IIC clock Unit, set the corresponding I0 port to the IIC function, clear the response bit, start bit, interrupt bit, IIC enable bit, set the IIC transmission rate. If it is a slave machine, set the slave address, enable IIC interruption, and then start IIC.
2) IIC startup
Uint32_t i2cstart (void)
{
Uint32_t timeout = 0;
Uint32_t retval = false;
Lpc_i2c-> conset = i2conset_sta // you can specify the start position.
/* Wait for the start bit to send cyclically and set the timeout time */
While (1)
{
If (i2cmasterstate = i2c_started) // The start bit is successfully sent, and is set in the interrupt
{
Retval = true;
Break;
}
If (timeout> = i2c_max_timeout) // start bit sending timeout
{
Retval = false;
Break;
}
Timeout ++;
}
Return (retval );
}
3) Stop IIC
Uint32_t i2cstop (void)
{
Lpc_i2c-> conset = i2conset_sto; // you can specify a stop bit.
Lpc_i2c-> conclr = i2conclr_sic; // clear the interrupt mark
/* Wait for the stop bit to send */
While (lpc_i2c-> conset & i2conset_sto );
Return true;
}
4) A complete IIC Communication
Uint32_t i2cengine (void)
{
I2cmasterstate = i2c_idle; // initial state
Rdindex = 0;
Wrindex = 0;
If (i2cstart ()! = True) // send the startup signal
{
I2cstop ();
Return (false );
}
While (1)
{
If (i2cmasterstate = data_nack) // determine whether data transmission is complete
{
I2cstop (); // stop IIC
Break;
}
}
Return (true );
}
5) main sending code
Void i2cwrite (uint8_t ADDR, uint8_t * Buf, uint32_t Len)
{
I2caddr = ADDR; // set the slave address
I2cmasterbuffer = Buf; // The master sends the buffer to the data to be transmitted.
I2cwritelength = Len; // The length of the sent data
I2creadlength = 0;
I2cengine (); // starts the entire IIC communication process
I2cwritelength = i2cwritelength;
}
6) interrupt processing functions
Each time the status changes, the SI bit is set to a bit. In the interrupt processing function, operations are performed based on different status codes. In these operations, it is necessary to clear the SI bit, this section mainly lists the processing functions of the master sending interruption. The other parts can be operated according to the operation procedure in the Data Manual.
Void i2c_irqhandler (void)
{
Uint8_t statvalue;
/* Only lists the processing of the master sending Method */
Statvalue = maid-> Stat; // read status code
Witch (statvalue) // perform different operations based on the status code
{
Case 0x08: // start condition of the sent message
Wrindex = 0;
Lpc_i2c-> dat = i2caddr; // SLA + W load data register
Lpc_i2c-> conclr = (i2conclr_sic | i2conclr_stac); // use SiC and STAC bits.
I2cmasterstate = i2c_started;
Break;
Case 0x10: // start condition of the sent repetition
Rdindex = 0;
/* Send SLA + R status bit */
Lpc_i2c-> dat = i2caddr;
Lpc_i2c-> conclr = (i2conclr_sic | i2conclr_stac );
I2cmasterstate = i2c_restarted;
Break;
Case 0x18: // send SLA + W and accept ACK
If (i2cmasterstate = i2c_started)
{
Lpc_i2c-> dat = i2cmasterbuffer [wrindex ++]; // write data to the data register.
I2cmasterstate = data_ack;
}
Lpc_i2c-> conclr = i2conclr_sic;
Break;
Case 0x28:/* The data has been sent, and ACK or non-Ack is returned */
Case 0x30:
If (wrindex <i2cwritelength) // determines whether the message is sent completely.
{
Lpc_i2c-> dat = i2cmasterbuffer [wrindex ++];
I2cmasterstate = data_ack;
}
Else // data of the specified length has been sent
{
If (i2creadlength! = 0)
{
Lpc_i2c-> conset = i2conset_sta; // you can specify a duplicate start condition.
I2cmasterstate = i2c_repeated_start;
}
Else
{
I2cmasterstate = data_nack;
Lpc_i2c-> conset = i2conset_sto; // sets the stop bit.
}
}
Lpc_i2c-> conclr = i2conclr_sic;
Break;
......
For embedded and 3G related resources and learning, click: Embedded Development Video Android training 3G training QT Development Video Iot training IoT technology video embedded learning