[STC MCU learning] Lesson 15: I2C communication-EEPROM

[Mr. Zhu's course summary intrusion]

In this lesson, we will learn how to use the 51 single-chip IO port to simulate I2C timing, and to achieve two-way communication with AT24C02 (EEPROM).

The second part, chapter introduction

1.15.1. EEPROM and its background knowledge
This section focuses on EEPROM and its related concepts, focusing on the memory and I2C interface in the microcontroller system .
1.15.2. Schematic diagram and data manual 1
This section first analyzes the schematic diagram and wiring briefly, and then focuses on reading the data manual of AT24C02 .
1.15.3. Schematics and data Manual 2
This section describes the I2C protocol key concepts, as well as data sheets and I2C protocols related parts.
1.15.4. I2C low-level timing diagram and program 1
This section mainly analyzes the low-level timing part of the code in the official routine , and
compares and analyzes the timing diagram in the data manual 1.15.5.
EEPROM read and write test 1 This section describes the EEPROM The upper-layer read and write sequence focuses on the principle of determining the I2C slave address and register settings.
1.15.6
. EEPROM read and write test 2 This section then analyzes the upper read and write timing of the EEPROM, focusing on the upper timing diagram in the data manual.

Part Three, Classroom Record

1.15.1. EEPROM and its background knowledge

1.15.1.1, EEPROM
(1) Some concepts:

  • ROM (Read Only Memory), RAM (Random Access Memory), PROM (Programmable ROM), EPROM (Erasable ROM), EEPROM (Electrically Erasable ROM)
    Step on the link to understand it!

(2) Why do we need EEPROM?

  • The ROM inside the microcontroller can only be erased and rewritten when the program is downloaded, but the program itself cannot be rewritten. The data program in RAM inside the microcontroller can be changed while it is running, but it will be lost when power is off. Sometimes we have some data that needs to be stored in the system, which requires that it is not lost after power failure, and that the program can be modified. So internal ROM and RAM are not good. At this time, an EEPROM is needed in the system

(3) The difference and connection between EEPROM and flash

  • Flash belongs to EEPROM in a broad sense, because it is also an electrically erasable rom. But in order to distinguish it from the general erasable EEPROM in bytes, we call it flash.
    The improvement made by flash is that erasing is no longer in bytes, but in blocks, which simplifies the circuit at a time, increases the data density, and reduces the cost. The ROM on M is generally flash.

(4) There are two forms of EEPROM in the system: built-in inside the microcontroller, and externally expanded
EEPROMEEPROM schematic diagram

1.15.1.2 How to program EEPROM

  • I2C interface bottom timing
  • Device-defined register read and write timing

1.15.2. Schematic and data sheet 1

1.15.2.1. Schematic diagram and wiring determination
(1) Definition of key pins and their connections

SCL clock line, transmitting CLK signal.
SDA data line, transmit communication data
WE write protection enable signal

(2) Wiring

P2.0 connect to SCL
P2.1 connect to SDA
WE connect to GND

1.15.2.2, AT24C02 data manual browsing
(1) chip information

  • description
  • Features
  • block diagram
  • Pin description

(2) I2C basic informationInsert picture description here

  • Detailed instructions
  • I2C Handbook-Highlighted Features
  • Master device, slave device, transmitter, receiver
  • Device addressing (to distinguish slave devices )

(3) I2C low-level timing
Insert picture description here

  • Start signal: SCL high level, SDA falling edge
  • Stop signal: SCL high level, SDA rising edge
  • Send byte: After the start, the master device broadcasts, first sends the slave address, then sends the valid data, and finally stops
  • Read byte: After starting, the slave device sends its own address first, then puts the data on the bus, and finally stops
  • Repeated start condition: Similar to the start condition, the repeated start condition occurs before the stop condition. When the master wants to continue sending messages to the slave, it can send a repeated start condition after one byte transmission is completed, instead of generating a stop condition.

(4) I2C summary

  • The most commonly used interface between the main CPU and its auxiliary chips, especially various sensors, is very important in the Internet of Things era;
  • Three wires: GND, SCL, SDA , serial, level type;
  • Bus structure, can be one-to-many, hundreds of devices can be hung on the bus, distinguished by slave address ;
  • Master-slave mode, the master device initiates communication and bus arbitration, and the slave device responds passively;
  • At the same time, only one slave device can communicate with the master device, and the other slave devices are in a "hibernation" state;
  • The communication rate is general (kbps level), not suitable for information types such as voice and video;
  • The LSB of the device address information is the read/write operation selection bit, the high is the read operation, and the low is the write operation ;
  • Data transmission is big-endian transmission, that is, the high bit is transmitted first ;
  • Data is transmitted in bytes , and can transmit a plurality of consecutive bytes;
  • The start and stop conditions are controlled by the master device, and the reception and response are executed by the receiver and transmitter respectively.

1.15.4. I2C low-level sequence diagram and program

Official sample program: \Step 3 51 routine\16, EEPROM (24C02)\digital tube display EEPROM\
Program download link
1.15.4.1 , start signal and end signal
(1) Start signal: When SCL remains high, SDA There is a high-to-low ( falling edge )
(2) End signal: When SCL remains high, SDA has a low-to-high ( rising edge )
(3) Bus is idle: If a device's SCL and SDA remain high, It means that the device is idle
-see code
Insert picture description here
1.15.4.2, bit transmission
Each clock pulse transmits one bit of data. SDA must remain stable when SCL is high , because the change of SDA at this time is considered a start/end signal . As follows:
Insert picture description here
Reference
Insert picture description here

1.15.4.3, I2C sends a byte
(1) Response bit processing (receiver -> sender )

  • Each byte must be followed by an acknowledge bit (ACK) .

(2) When I2C sends a byte, it starts from the high bit ( microcontroller -> receiver )

  • Taking transmission Byte: 1010 1010 (0xAAh) as an example, the SDA SCL transmission sequence is as follows:
    Insert picture description here
    As can be seen from the above figure, the slave device pulls down SDA to indicate a response , and maintains a stable low level during the response pulse!
    -Look at the code

1.15.4.4, I2C receives a byte ( receiver -> MCU )
(1) Concept: release the bus.

  • In 51 single-chip microcomputer, SDA=1 is to release the bus; in other more advanced single-chip microcomputers (such as STM32, etc.), the processing here is a bit different.
  • Why SDA=1 is to release the bus, because when the 51 single-chip microcomputer pulls the pin high, the slave device can choose to pull this pin high or low; but when the 51 single-chip microcomputer pulls this pin low (grounded), The slave device can no longer pull this pin high.

1.15.5. EEPROM read and write test 1

1.15.5.1, 24C02 read and write high-level sequence ( STC51-->AT24C02 )

The high-level timing is to use the low-level timing to complete the communication with the chip, accurate to the 24C02 manual

(1) Write operation
Insert picture description here

  • Word address is write memory address
    Insert picture description here

-Look at the code

(2) Read operation

  • Random read
    Random read

  • Look at the manual, look at the code

(3) The general read and write operations are understood, but how did the address come from?

  • The device address
    is defined by the slave device itself . Different slave devices have different address definition methods. For hardware definition, please check the specific chip data manual to determine.
    Insert picture description here

Insert picture description here
By analyzing the schematic diagram and the address definition of 24C02 , we can get:

Read address: 1010 0001 (0xA1)
Write address: 1010 0000 (0xA0)

How many AT24C02 pieces can be connected at most?

  • The byte address (EEPROM memory address)
    Insert picture description here
    is ugly. For ease of understanding, let’s look at the blackboard!

1.15.6. EEPROM read and write test 2

Send a byte of data to the EEPROM and write them in the EEPROM specific memory;
then read the EEPROM data and print it out through the serial port.

1.15.6.1 Project establishment and file import

  • Directly use i2c.h and i2c.c of the official sample program

1.15.6.2, add serial output code

  • Directly use the serial port files written before: serial.c and serial.h

1.15.6.3, test EEPROM

  • Send data-receive data-print to the serial port

1.15.6.4. The program problem is solved. The
read content is wrong, because the EEPROM cannot withstand fast continuous read and write, so add a 20ms delay between read and write. After testing, it is found that the read and write are correct.

Attach the final program code:

//main.c
#include "serial.h"
#include "i2c.h"
/*******************************************************************************
一、接线
P2.0--SCL、P2.1--SDA  
设置在i2c.h

二、程序功能
给EEPROM发送一个字节的数据,把它们写在EEPROM特定内存下;
然后读取EEPROM的数据之后,通过串口打印出来。
*******************************************************************************/

void At24c02Write(unsigned char ,unsigned char );
unsigned char At24c02Read(unsigned char );
void delay20ms(void)   //误差 -0.000000000005us
{
    unsigned char a,b,c;
    for(c=1;c>0;c--)
        for(b=222;b>0;b--)
            for(a=40;a>0;a--);
}

void main(void)
{
	//变量声明
	u8 src_data[] = "12345678"; //要发送的数据
	u8 buf[8];//接收数据的buf
	u8 addr = 0;	//特定的地址
	u8 i = 0;
	//串口初始化
	uart_init();
	
	//发送数据
	for(i = 0;i<8;i++)
	{
		At24c02Write(addr,src_data[i]);
		delay20ms();
		addr++;
	}
	
	//接收数据
	addr = 0;
	for(i = 0;i<8;i++)
	{
		buf[i] = At24c02Read(addr);
		delay20ms();
		addr++;
	}
	
	//向串口打印
	for(i = 0;i<8;i++)
	{
		uart_send_byte(buf[i]);
	}
	while(1);
}

void At24c02Write(unsigned char addr,unsigned char dat)
{
	I2C_Start();
	I2C_SendByte(0xa0, 1);//发送写器件地址
	I2C_SendByte(addr, 1);//发送要写入内存地址
	I2C_SendByte(dat, 0);	//发送数据
	I2C_Stop();
}
/*******************************************************************************
* 函 数 名         : unsigned char At24c02Read(unsigned char addr)
* 函数功能		   : 读取24c02的一个地址的一个数据
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

unsigned char At24c02Read(unsigned char addr)
{
	unsigned char num;
	I2C_Start();
	I2C_SendByte(0xa0, 1); //发送写器件地址
	I2C_SendByte(addr, 1); //发送要读取的字地址
	I2C_Start();
	I2C_SendByte(0xa1, 1); //发送读器件地址
	num = I2C_ReadByte(); //读取数据
	I2C_Stop();
	return num;	
}

Program download link for this lesson : I2C
This lesson is over!

Guess you like

Origin blog.csdn.net/qq_27148893/article/details/110168332