I2C 24LC02 C reading and writing program design

Publisher:快乐舞蹈Latest update time:2011-11-24 Source: 电子产品世界Keywords:I2C Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 I2C bus features
The main advantages of the I2C bus are its simplicity and effectiveness. Since the interface is directly on the components, the I2C bus takes up very little space, reducing the space on the circuit board and the number of chip pins, reducing the cost of interconnection. The bus can be up to 25 feet long and can support 40 components at a maximum transmission rate of 10Kbps. Another advantage of the I2C bus is that it supports multimastering, in which any device capable of sending and receiving can become the master bus. A master can control the transmission and clock frequency of the signal. Of course, there can only be one master at any point in time.

2 Working Principle of I2C Bus
The data stability rule on the I2C bus is that the data on SDA remains stable when SCL is high, and SDA is allowed to change when SCL is low. If a falling edge is generated on SDA when SCL is high, it is considered a start bit, and a rising edge on SDA is considered a stop bit. The communication rate is divided into normal mode (clock frequency 100kHz) and fast mode (clock frequency 400kHz). Multiple devices with I2C interfaces can be connected to the same bus. Each device has a unique address. It can be a single-receive device or a device that can receive and send.


Each data transmission starts with a start bit and ends with a stop bit. There is no limit to the number of bytes transmitted. The most significant bit will be transmitted first, and the receiver will send an acknowledge bit after receiving the 8th bit of data. Data transmission is usually divided into two types: master device sends and slave device receives and slave device sends and master device receives. Both modes require the host to send the start bit and stop bit, and the acknowledge bit is generated by the receiver. The slave device address is generally 1 or 2 bytes, which is used to distinguish different devices connected to the same I2C.

There are three types of signals in the I2C bus during data transmission, namely: start signal, end signal and response signal.
Start signal: When SCL is at a high level, SDA jumps from a high level to a low level to start data transmission.
End signal: When SCL is at a high level, SDA jumps from a low level to a high level to end data transmission.
Response signal: After receiving 8-bit data, the IC receiving data sends a specific low-level pulse to the IC sending data, indicating that the data has been received. After the CPU sends a signal to the controlled unit, it waits for the controlled unit to send a response signal. After receiving the response signal, the CPU determines whether to continue to transmit the signal based on the actual situation. If no response signal is received, it is determined that the controlled unit has a fault.

There are only two operation modes in the I2C bus: master transmission and master reception. When the system is initialized, the CPU is controlled by instructions to send relevant data, which is sent to the I2C register through the interface. By initializing these registers, the master mode control of the I2C bus can be realized, and the slave device reading and writing on the I2C bus can be realized.

When the master device exchanges data with one of the slave devices, the master device first sends a Start signal, which is received by all the slave devices. That is, the slave device is ready to receive the CPU signal, and then the master device sends the slave device address it wants to communicate with. Next, all the slave devices compare the received address with their own addresses.

If the received address is different from their own address, they do nothing but wait for the master device to send a stop signal; if the received address is the same as their own address, they send a signal to the master device, which is called an acknowledgement signal. When the master device receives the acknowledgement signal, it starts to send data to or receive data from the slave device. When all operations are completed, the master device sends a Stop signal, the communication is completed, and the I2C bus is released; then all slave devices wait for the next Start signal to arrive.

3 Basic Bus Operations
The I2C protocol uses master/slave bidirectional communication. A device that sends data to the bus is defined as a transmitter, and a device that receives data is defined as a receiver. Both the master and slave devices can work in the receiving and sending states. The bus must be controlled by a master device (usually a microcontroller), which generates a serial clock (SCL) to control the transmission direction of the bus and generates start and stop conditions. The data state on the SDA line can only change when SCL is low. During the period when SCL is high, the change of SDA state is used to indicate the start and stop conditions.
3.1 Control Byte
After the start condition, there must be a control byte for the device, of which the upper four bits are the device type identifier (different chip types have different definitions, EEPROM should generally be 1010), followed by three bits for chip select, and the last bit is the read/write bit, which is 1 for read operation and 0 for write operation.

1. Writing process

(1) Wait for a delay (1ms) after power-on.

(2) Device addressing, giving a start signal (SDA gives a falling edge when SCL is high). Send the slave device address, the upper 5 bits are 10110, and then perform read/write control (O for read) according to A1/A0 (if the address is the same as the device address, the device will respond).

(3) Response: The device gives a low level on SDA during the 9th cycle of SCL as a response signal.

(4) There are two modes for starting writing: byte write mode and page write mode.

Byte mode: Give A15~A8 response, give A7~A0 response; then give DATA and stop signals (when SCL is high level, SDA gives a rising edge), and then wait for an erase time.

Page write mode: After the address is given, 64 data are given continuously. If there are more than 64 data, the address counter will automatically roll over. (If there are less than 64 data, it is estimated that there is no problem, but it needs to be verified by experiment.)

(5) A method for determining whether the erase operation is completed (response query). If the device is still in the erase state, it will not respond to the device addressing; if there is a response, it means that the erase operation is completed.

2. Reading process

(1) Wait for a delay (lms) after power-on.

(2) Device addressing.

(3)Response.

(4) There are three modes for starting to read: immediate current address read, selective/random read, and continuous read.

Immediate current address read: If the last read/write operation address is N, now it is N+1. No ACK is required, but a Stop signal is required.

Selective/random read: First dummy write (to give an address), then start again to read the data.

·Continuous read: After reading one, give a response, so that the device will give the data content of the next address.

(5) After the data transmission starts and before the data transmission stops, during the period when SCL is high, SDA contains valid data.

/***********************************************************
1. Program description:
1. The device address of 24LC02 is 1010000R/W.
2. The array is written to 24LC02 in page write mode.
3. The array code is read from 24LC02 in free read mode.
4. 4.00M crystal is used.
5. Software I2C is used.

2. Hardware connection:
1. SDA------->23 pin. (Of course you can choose any pin)
2. SCL------->18 Pin. (Of course you can choose any pin)
3. PORTD----->8 external LEDs to display the read data. Here, the read data is a flashing running light state.

************************************************************/
#i nclude "pic.h"

#define uchar unsigned char
#define nop() asm("nop"
#define SCL TRISC3
#define SDA TRISC4

void start_i2c();
void stop_i2c();
void send_byte(uchar c);
uchar receive_byte();
void I_send_str(uchar sla,uchar suba,uchar *s,uchar no);
void delay_250ms();
void i2c_error ();

uchar code[]={0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
uchar no,ack,c,data;


void main(void)
{
uchar i;
TRISC=0Xff; //C port is set as input RC3 is the SCL line, RC4 is SDA line.
PORTC=0X00;
TRISD=0X00; //Port D is output, displaying the contents read from IC24LC02
PORTD=0X00; //Initial display is all bright

I_send_str(0xa0,0x00,code,9); //Page writes code array to 24LC02, device address is 0Xa0, sub-address is 0X00, total 9 numbers.

delay_250ms();


///////////Start reading out to port D for display, according to the Random read timing diagram.
while (1)
{
for (i=0x00;i<0x09;i++)
{
start_i2c();
send_byte(0xa0); //Send device address, i.e. DEVICE ADDRESS.
if (ack==0) i2c_error(); //If 24LC02 does not respond. Then enter I2C ERROR error indication.
send_byte(i); //Send word address, i.e. WORD ADDRESS. D port displays array.
if (ack==0) i2c_error();
start_i2c(); //Restart the bus.
send_byte(0xa1); //Send read command and device address DEVICE ADDRESS.
if (ack==0) i2c_error();
data=receive_byte();
stop_i2c();
PORTD=data;
delay_250ms();
}
}
}
/********************************************************************
Start bus function
Function prototype: void start_i2c();
Function: start on the I2C bus
*************************************************************/
void start_i2c()
{
SDA=1; //Send the data signal of the start condition
nop();
SCL=1;
nop();nop();nop();nop();nop();nop(); //24LC02 requires the establishment time to be greater than 4,7S
SDA=0; //Send start signal
nop();nop();nop();nop();nop();
SCL=0; //Clamp the I2C bus and prepare to send or receive data
nop();nop();
}


/****************************************************************
Stop bus function
Function prototype: void stop_i2c();
Function: stop the I2C bus
**************************************************************/
void stop_i2c()
{

SDA=0; //Send the data signal of the end condition
nop();
SCL=1;
nop();nop();nop();nop();nop();nop();
SDA=1;
nop();nop();nop();nop();
}

/*=====================================================
Byte data transmission function
Function prototype: void send_byte(uchar c);
Function: Send data C, which can be an address or data. After sending, wait for a response and
operate on this status bit (make ack=0 for no response or non-response). Sending data is normal. ack=1; ack=0
means that the controlled device has no response or is damaged.
==================================================*/
void send_byte(uchar c)
{
uchar bit_count;
for (bit_count=0;bit_count<8;bit_count++)
{
if ((c< else {SDA=0;}
nop();
SCL=1;
nop();nop();nop();nop ();nop();
SCL=0;
}
nop();nop();
SDA=1;
nop();nop();
SCL=1;
nop();nop();nop();
if (RC4==1) ack=0;
else ack=1; //Use ASK=1 for response signal
SCL=0;
nop();nop();
}

/*===============================================
Byte data receiving function
Function prototype: uchar receive_byte();
FUNCTION: Used to receive data from the device and judge the bus error (no response signal is sent).
Please use the response function after sending.
==================================================*/
uchar receive_byte()
{
uchar retc,bit_count;
retc=0;
SDA=1;
for (bit_count=0;bit_count<8;bit_count++)
{
nop();
SCL=0;
nop();nop();nop();nop();nop();
SCL=1;
nop(); nop();
retc=retc<<1;
if (RC4==1) retc=retc+1;
nop();nop();
}
SCL=0;
nop();nop();
return (retc);
}


/ * =
... ​ ==============================================*/ void I_send_str(uchar sla,uchar suba,uchar *s,uchar no) { uchar i; start_i2c(); send_byte(sla); if (ack==0) i2c_error(); send_byte(suba); if (ack==0) i2c_error(); for (i=0;i












{
send_byte(*s);
if (ack==0) i2c_error();
s++;
}
stop_i2c();
// return(1);
}

/********************************************************************
Delay function
Function prototype: void delay_250ms();
FUNCTION: Delay 250ms
*****************************************************************/
void delay_250ms()
{
unsigned int d=24999;
while (--d);
}

/*********************************************************************
Bus error function
Function prototype: void i2c_error();
Function: RD7 flashes 8 times to indicate a bus operation failure and an alarm.
*****************************************************************/
void i2c_error ()
{
uchar i;
for (i=0;i<8;i++)
{
RD7=0;
delay_250ms();
RD7=1;
delay_250ms();
}

}
/**********END******************/

Keywords:I2C Reference address:I2C 24LC02 C reading and writing program design

Previous article:Design and Application of Clock Frequency Synchronization Based on FPGA
Next article:Design of multiple timers based on VxWorks

Recommended ReadingLatest update time:2024-11-16 19:39

PIC microcontroller I2C communication (slave mode)
There are many explanations on the Internet about how to implement I2C master mode with microcontrollers, but few about slave mode. I will now talk about how to implement I2C slave mode with PIC microcontrollers using the MSSP module.     For a detailed introduction to the I2C protocol, please refer to "PIC Microcontr
[Microcontroller]
PIC microcontroller I2C communication (slave mode)
MCU I2C driver subroutine
Crystal oscillator 11.0592M/********************************************************************/ /***********************I2C analog communication and AT24C02 bottom-level driver program****************/ /************************************************************************/ //AT24C02 //delayus: delay about 5us
[Microcontroller]
Analysis of I2C driver of linuxok6410 --- user mode driver
3 i2c-dev 3.1 Overview When introducing the I2C subsystem, we mentioned using the i2c-dev.c file to implement our I2C slave device driver in the application. However, it implements a virtual, temporary i2c_client, which is generated when the device file is opened and revoked when the device file is c
[Microcontroller]
avr--TWI(I2C)
TWI: It is a full-duplex serial communication protocol that works in the same way as I2C. It consists of a data transmission line SDL and a clock line SCL, corresponding to the external pins PC1 and PC0 of the microcontroller.              Since there are only two buses, the system design is simplified. Features: • Si
[Microcontroller]
Design of Data Writer Based on I2C Bus
1 Introduction The Inter-Integrated Circuit (IIC) bus is a bus used to connect microcontrollers and their peripheral devices. The main advantages of the bus are its simplicity and effectiveness. Since the interface is directly on the component, it occupies a small space, reduces the space of the circui
[Microcontroller]
Design of Data Writer Based on I2C Bus
Multi-point temperature acquisition system based on I2C bus
With the progress of society and the development of industrial technology, many products have stricter requirements on temperature range, especially in industrial development, such as metallurgy, steel, petrochemical, cement, glass and other industries. However, most of the temperature detection instruments on the m
[Test Measurement]
Multi-point temperature acquisition system based on I2C bus
24c08 I2C bus protocol operation program assembly and C language 2 versions detailed explanation
;24c08 operation timing is: when the start signal sclk is pulled high, sda changes from high to low, indicating the start ; When the end signal sclk is pulled high, sda changes from low to high, indicating the end ; Bytes are sent from high to low ; The first byte is the address and read/write control, the second is
[Microcontroller]
Latest Industrial Control Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号