The Working Principle and Application Method of I2C Bus Are Analyzed in Detail

Hello everyone, through the previous study, we have understood and familiar with the use and learning methods of ICD2 simulation burner and enhanced pic experimental board, learned how to use single chip microcomputer to control light-emitting tube, relay, buzzer, key, nixie tube, RS232 serial port, stepping motor, temperature sensor and other resources, and realized the ease of use and learning of the learning board, After reading the previous examples, you must be very excited and have a sense of achievement when your experiment is successful! Now let's strike while the iron is hot and take a step up. Let's learn the working principle and use method of I2C bus, so that we can store some data we want to save in the nonvolatile memory of I2C bus to realize the function of power-off holding. For example, you set a password, but it will not be reset after the device is powered off, We can write the password data in the nonvolatile memory. For example, the reading of the range meter of the car is continuously accumulated, which can be realized by continuously accessing the I2C memory.

1、 I2C bus features

I2C bus is the master-slave structure, MCU is the master device and memory is the slave device. A bus can have multiple slave devices (or multiple master structures). SDA and SCL of I2C bus are bidirectional, open gate structure, and positive power supply is connected through pull-up resistance. During data transmission, the data on SDA line must remain stable in the high-level cycle of the clock. The high or low level state of the data line can be changed only when the clock signal of the SCL line is low, as shown in Fig. 1.

Figure 1 validity provisions of data bits

When the SCL line is a high level, switching the SDA line from high level to low level indicates the starting condition; When SCL is high, the SDA line switches from low level to high level, indicating the stop condition, as shown in Fig. 2.

Figure 2 start and stop signals

Each byte sent to the SDA line must be 8 bits.

Multiple bytes can be transferred from high to low. Each byte must be followed by a response bit (ACK). The response clock pulse is generated by the host. The master releases the SDA line, and the slave pulls the SDA line low and remains stable during the high level of the clock pulse. As shown in Figure 3. When the host receives data, it must send a signal to the slave to end the transmission after receiving the last data byte. This signal is realized by the "non response" of the host to the slave. Then, the slave releases the SDA line to allow the host to generate a termination or repeat start signal.

Figure 3 byte format and response

2、 Data frame format

(1) The master sends data to the slave, and the transmission direction of the data does not change during the transmission process, as shown in Figure 4.

Figure 4 sending data from master to slave

Note: shaded part: indicates that the host sends data to the slave; Unshaded part: indicates that the host reads data from the slave.

A: Indicates a response;: Indicates a non response. S: Start signal; P: Termination signal.

(2) The host reads data from the slave immediately after the first byte, as shown in Figure 5.

Figure 5 the master reads the slave immediately after the first byte

(3) Composite format, as shown in Figure 6. When the transmission changes direction, the starting condition and slave address will be repeated, but the R / W-bit is reversed. If the host receiver sends a stop or repeat start signal, it should have sent a non response signal ().

Figure 6 composite format

As can be seen from the above format, regardless of the transmission mode, the start signal, end signal and address are sent by the host (shaded part in the figure), the transmission direction of data bytes is specified by the direction bit in the addressing byte, and the transmission of each byte must have a response bit (a or b).

Next, the enhanced pic experimental board is programmed through the 24C02 example, and its hardware schematic diagram is shown in Figure 7. U7 is the 24C02 chip on the experimental board, SDA is connected with RB5 port of MCU, SCL is connected with RB4 port of MCU, seven segment nixie tubes D5, D7 and D8 form the display unit, and the data of font code is sent through RC port, The display chip selection signal of each nixie tube is controlled by different RA ports.

Fig. 7 Schematic diagram of reading / writing AT24C series memory

Create a new project in MPLAB ide software, add source code, select chip model and set configuration bit at the same time. The chip model used in our experiment is PIC16F877A.

The program code is as follows, and the program flow chart is shown in Figure 8.

3、 Software flow chart

Figure 8 I2C bus read / write data flow chart

4、 Software code

/**********/

/*Target device: PIC16F877A*/

/*Crystal oscillator: 4.0mhz*/

/*Compilation environment: MPLAB v7.51*/

/**********/

/**********

Include header file

**********/

#include

/**********

Data definition

**********/

#define address 0xa

#define nop() asm("nop")

#define OP_ READ 0xa1

//Device address and read operation

#define OP_ WRITE 0xa0

//Device address and write operation

/**********

Port definition

**********/

#define SCL RB4

#define SDA RB5

#define SCLIO TRISB4

#define SDAIO TRISB5

/**********

Common cathode led segment code table

**********/

const char table[]=0xC0,0xF9,0xA4,0x

B0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x

83,0xC6,0xA1,0x86,0x8E;

/**********

Function: delay subroutine

**********/

void delay()

int i;

for(i=0;i;

/**********

Function: start signal

**********/

void start()

SDA=1;

nop();

SCL=1;

nop();nop();nop();nop();nop();

SDA=0;

nop();nop();nop();nop();nop();

SCL=0;

nop();nop();

/**********

Function: stop signal

**********/

void stop()

SDA=0;

nop();

SCL=1;

nop();nop();nop();nop();nop();

SDA=1;

nop();nop();nop();nop();

/**********

Function: read data

Exit parameter: read_ data

**********/

unsigned char shin()

unsigned char i,read_data;

for(i=0;i nop();nop();nop();

SCL=1;

nop();nop();

read_data《=1;

if(SDA == 1)

read_data=read_data1;

nop();

SCL=0;

return(read_data);

/**********

Function: write data to EEPROM

Entry parameter: write_data

Exit parameter: ack_bit

**********/

bit shout(unsigned char write_data)

unsigned char i;

unsigned char ack_bit;

for(i = 0; i

if(write_data&0x80)

SDA=1;

else

SDA=0;

nop();

SCL = 1;

nop();nop();nop();nop();nop();

SCL = 0;

nop();

write_data 《= 1;

nop();nop();

SDA = 1;

nop();nop();

SCL = 1;

nop();nop();nop();

Ack_bit = SDA; / / read the response

SCL = 0;

nop();nop();

return ack_bit;

//Return at24cxx reply bit

/**********

Function function: write data to the specified address

Entry parameters: addr, write_data

**********/

void write_byte(unsigned char addr,

unsigned char write_data)

start();

shout(OP_WRITE);

shout(addr);

SDAIO = 0;

//SDA should be set to output before writing data

shout(write_data);

stop();

delay();

/**********

Function function: read data to the specified address

Entry parameter: random_addr

Exit parameters: read_data

**********/

unsigned char read_random(unsigned

char random_addr)

unsigned char read_data;

start();

shout(OP_WRITE);

shout(random_addr);

start();

shout(OP_READ);

SDAIO = 1;

//SDA should be set as input before reading data

read_data = shin();

stop();

return(read_data);

/**********

Function function: display subroutine

Entry parameters: K

**********/

void display(unsigned char k)

TRISA=0X00;

//Set port a as all outputs

PORTC=table[k/1000];

//Display thousands

PORTA=0xEF;

delay();

PORTC=table[k/100%10];

//Display hundreds

PORTA=0xDF;

delay();

PORTC = table [k/ 10%10] ;

//Display ten digits

PORTA=0xFB;

delay();

Portc = table [k%10]; / / display bits

PORTA=0xF7;

delay();

/**********

Function: main program

**********/

void main()

unsigned char eepromdata;

TRISB=0X00;

OPTION&=(1《7);

//Set the internal pull-up resistance of Rb port to be effective

TRISC=0X00;

PORTB=0X00;

PORTC=0xff;

TRISA=0X00;

eepromdata=0;

write_byte(0x01,0x55);

//Write data of 0x55 (85) to 0x01 address

delay();

write_byte(0x02,0xaa);

//Write data of 0XAA (170) to 0x02 address

delay();

eepromdata=read_random(0x02);

//Read the data in one of the addresses to verify

while(1)

display(eepromdata);

After programming, burn the compiled hex code into the single chip microcomputer chip through the ICD2 simulation burner, power on and run. In the main program, write "0x55" at 0x01 address and "0XAA" at 0x02 address, and then read out the value of 0x02 address in the while cycle, that is, the "0x55" we wrote before. After reading out, it is displayed on the nixie tube, and we can see the nixie tube display "170", that is, the corresponding decimal number of "0XAA".

As a beginner, readers must have some questions about some sentences. You can see the notes in the program, and analyze the 24C series IC data manual combined with the source program.

The Working Principle and Application Method of I2C Bus Are Analyzed in Detail 1

get in touch with us
Bellum omnium contra
This Paper Analyzes the Basic Knowledge of WiFi Module in Detail
The following is the total knowledge of WiFi modules compiled by Skylab, hoping to help you!1. What is the SDK of WiFi module?SDK is the English abbreviation of the software development kit (full name: software development kit) of WiFi module. It is generally a collection of development tools used by some software engineers to establish application software for specific software packages, software frameworks, hardware platforms, operating systems, etc.2. What is the driver of the WiFi module?A driver is a small piece of code added to the operating system that contains information about the hardware device. With this information, the computer can communicate with the device. The driver is a configuration file written by the hardware manufacturer according to the operating system. It can be said that without the driver, the hardware in the computer cannot work.For the WiFi module, this hardware can be used as the WiFi module of the slave device. At present, the WiFi module of the slave device of Skylab can provide drivers. The supported systems include window, Linux and Android.3. What is the network port of WiFi module?The network port of WiFi module should be from two aspects,(1) On hardware:Network interface refers to various interfaces of network equipment. The network interfaces we are using today are Ethernet interfaces.Common Ethernet interface types include RJ-45 interface, RJ-11 interface, SC optical fiber interface, FDDI interface, AUI interface, BNC interface and console interface.(2) Software:Network ports are divided into Wan ports and LAN portsWAN port is used to connect the external network (public network), or the equipment of broadband operators.LAN ports (1, 2, 3 and 4) are used to connect devices in the intranet (local area network), mainly to connect computers, switches, printers and other devices.4. What is the IP address of the WiFi module?IP is the abbreviation of English Internet protocol, which means "protocol for interconnection between networks", that is, a protocol designed for communication between computer networks.IP address is a 32-bit binary number, which is usually divided into four "8-bit binary numbers" (that is, 4 bytes).5. What is routing?Routing refers to the process that a router receives a packet from one interface, orients it according to the destination address of the packet and forwards it to another interface.The routing algorithm fills the routing table according to a lot of information. The destination / next hop address pair tells the router that the best way to reach the destination is to send the packet to the router representing "next hop". When the router receives a packet, it checks its destination address and tries to associate this address with its "next hop".6. What is the default gateway?The IP address of a directly reachable router. Configure the default gateway to create a default path in the IP routing table. A host can have multiple gateways. The default gateway means that if a host cannot find an available gateway, it will send the data packet to the default specified gateway, which will process the data packet.For example, the default gateway of Skylab's WiFi module is generally 10.10.10.254. Enter 10.10.10.254 in the address bar of the browser to enter the WiFi module configuration page. On the configuration page, you can set and change the working mode, language and other items of the WiFi module. When the WiFi module is in apclient working mode, there will be two default gateways.7. What is a LAN?LAN refers to a computer group interconnected by multiple computers in a certain area. LAN can realize the functions of file management, application software sharing, printer sharing, schedule within the working group, e-mail and fax communication services. LAN is a closed type, which can be composed of two computers in the office or thousands of computers in a company.8. What is DHCP?DHCP (Dynamic Host Configuration Protocol) is a LAN network protocol that works using UDP protocol. Automatically assign IP addresses to internal networks or network service providers, and give them to users or internal network administrators as a means of central management of all computers. For example, all WiFi modules of Skylab can support DHCP server.According to the DHCP address pool, each device connected to the module will be automatically assigned an IP address.9. TCP / UDP data transmission protocolTCP (transmission control protocol) is a connection oriented, reliable and byte stream based transport layer communication protocol.The process of TCP three handshakes is as follows:The client sends syn (SEQ = x) message to the server and enters syn_ Send status.The server receives a syn message, responds to a syn (SEQ = y) ack (ACK = x 1) message, and enters the syn_recv state.The client receives the syn message from the server, responds to an ACK (ACK = y 1) message, and enters the established state.TCP handshakes three times to establish a connection and four times to disconnect(1) When host a completes the data transmission, set the control bit fin to 1 and make a request to stop the TCP connection(2) Host B responds to fin after receiving it, confirms that the TCP connection in this direction will be closed, and sets ack to 1(3) The B end puts forward the closing request in the opposite direction and sets fin to 1(4) Host a confirms the request of host B, sets ack to 1, and the two-way shutdown endsFrom the three handshakes and four disconnects of TCP, it can be seen that TCP uses the connection oriented communication mode, which greatly improves the reliability of data communication and makes the sending data endIt interacts with the receiver before formal data transmission, which lays a reliable foundation for formal data transmission.The full name of UDP protocol is user datagram protocol. In the network, it is used to process packets like TCP protocol. It is a connectionless protocol.Before transmitting data, the source and terminal do not establish a connection. When it wants to transmit, it simply grabs the data from the application and throws it to the network as soon as possible. At the sender, the speed of UDP transmitting data is only limited by the speed of data generated by the application, the capacity of the computer and the transmission bandwidthDifferences between TCP and UDP:1. Based on connection and no connection;2. Requirements for system resources (more TCP and less UDP);3. UDP program structure is relatively simple;4. Stream mode and datagram mode;5. TCP guarantees data correctness, UDP may lose packets, TCP guarantees data order, and UDP does not.Well, the above is a basic theoretical knowledge of WiFi module compiled by Skylab. I hope it will be helpful to engineers engaged in WiFi module application development!About Skylab:Shenzhen Tiangong measurement and Control Technology Co., Ltd. (Skylab) , is a professional WiFi module manufacturer in China. It began to enter the field of wireless modules in 2002 with profound experience. The WiFi modules launched now include serial port WiFi module, AP / routing WiFi module, high-power UAV WiFi module, USB interface camera module, Gigabit WiFi module, 2.4/5g dual band WiFi module, etc. for more details, you can visit Tiangong measurement and control or Alibaba store
This Paper Explains the Calculation and Adjustment Method of Frequency Divider in Detail
Your Family Names and Details?
Six Points for Attention in the Installation and Maintenance of Agrometeorological Stations Are Intr
Eye for Detail Shines Through
Discuss the Differences, Applications and Suppliers Between SASE and Sd-wan in Detail
Best Cement Tiles Making Machine Suppliers and Price Details
Important Solutions to Granite Suppliers Melbourne in Easy to Follow Step by Step Detail
Migrants in Libya Detention Center Detail Dire Conditions
Could you please say sth about the details of wholesale quartz countertops?
related searches
This Paper Analyzes the Basic Knowledge of WiFi Module in Detail
This Paper Explains the Calculation and Adjustment Method of Frequency Divider in Detail
Six Points for Attention in the Installation and Maintenance of Agrometeorological Stations Are Intr
Discuss the Differences, Applications and Suppliers Between SASE and Sd-wan in Detail
Your Family Names and Details?
Eye for Detail Shines Through
Best Cement Tiles Making Machine Suppliers and Price Details
Important Solutions to Granite Suppliers Melbourne in Easy to Follow Step by Step Detail
Migrants in Libya Detention Center Detail Dire Conditions
  • E-MAIL: angelaliu@aofei-stone.com
  • PHONE : +86 13859989513
  • WHATSAPP : 13859989513
  • ADD : rm No.29e Zhongxinhuiyang Building No.55 North Hubin Road Xiamen China
AOFEI BUILDING MATERIALS
  • Xiamen Aofei Building Materials Co., Ltd. is adhering to the spirit and attitude of serving customers' needs and is willing to offer quality products and services to friends all over the world.
Copyright © 2021 Xiamen Aofei Building Materials Co.,Ltd.  | Sitemap