[Lanqiao Cup Embedded Expansion Board]—I2C read and write EEPROM detailed

Regarding the I2C of the blue bridge cup embedded board, if you have not been exposed to I2C communication before.
Focus:

  1. Know how to determine the address of the I2C device through the circuit diagram
  2. Will look at the I2C read and write timing diagram.
  3. Low-level driver, the exam will give you

Of course, if you are really zero-based, there is still a rescue!

"[STM32-I2C learning summary] STM32: hardware-IIC detailed explanation, firmware library programming, teach you how to implement IIC" here is an article I wrote before, hardware implementation of I2C, at the end of the article there is a method of software implementation of I2C , hardware will , The software is very simple

Portal: https://blog.csdn.net/qq_45689790/article/details/113725196?spm=1001.2014.3001.5501

I have to say that the official gave you the I2C low-level driver, writing and reading functions, referencing the manual, and looking at the timing diagram, it is really too simple.

1. Hardware schematic diagram

Insert picture description here
Through the schematic diagram, it is judged that [SCL->PB6] and [PB7->SDA]
Insert picture description here
onboard EEPROM are 2k, and E0~E2 are all grounded to 0, so the device address is 0xA0, of course, the read-write flag bit also follows the device address

  1. Read command: device address 0xA0 + 1 = 0xA1
  2. Write command: device address 0xA0 + 0 = 0xA0

This will be reflected in the programming. If you want to know the specific details, turn to the top and look for the portal.

Second, realize the read and write function
1. void Byte_Write(uint8_t Write_addr,uint8_t data)
Insert picture description here

void Byte_Write(uint8_t Write_addr,uint8_t data)
{
    
    
	I2CStart(); 				//1、发送开始信号
	I2CSendByte(0xA0);			//2、发送设备地址  写命令 0xA0 + 0 = 0xA0
	I2CWaitAck();				//3、等待响应
	I2CSendByte(Write_addr);	//4、等待响应 发送要写入EEPROM的地址
	I2CWaitAck();				//5、等待响应
	I2CSendByte(data);			//6、发送要写入的地址
	I2CWaitAck();				//7、等待响应
	I2CStop();					//8、发送停止信号
}

2. uint8_t Byte_Read(uint8_t Read_Addr)
Insert picture description here
seems to be marked wrong, please refer to the code remarks for details

uint8_t Byte_Read(uint8_t Read_Addr)
{
    
    
	uint8_t data = 0;
	I2CStart();						//1、发送开始信号		
	I2CSendByte(0xA0);				//2、发送设备地址  先写操作,0xA0	+ 0 = 0xA0
	I2CWaitAck();					//3、等待响应	
	I2CSendByte(Read_Addr);			//4、写入要读的地址,告诉EEPROM等下需要返回哪个值
	I2CWaitAck();					//5、等待响应
	
	I2CStart();						//6、发送开始信号,接受EEPROM返回的数据
	I2CSendByte(0xa1);				//7、发送设备地址,读操作 0xA0 + 1 =0xA1
	I2CWaitAck();					//8、等待响应
	data = I2CReceiveByte();		//9、读出数据
	I2CSendNotAck();				//10、发送非应答信号,准备停止
	I2CStop();						//11、停止信号
	
	return data;
}

3Buffer_Write(uint8_t *_pWriteBuf ,uint8_t Write_Addr,uint8_t Write_Num)

void Buffer_Write(uint8_t *_pWriteBuf,uint8_t Write_Addr,uint8_t Write_Num)
{
    
    
//最多连续写8个  注意地址对齐
	uint8_t i ;
	
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(Write_Addr);
	I2CWaitAck();
	
	for(i = 0;i < Write_Num;i++)
	{
    
    
		I2CSendByte(_pWriteBuf[i]);
		I2CWaitAck();
	}
	I2CStop();
}

4、void Buffer_Read(uint8_t *_pReadBuf,uint8_t Read_Addr,uint8_t Read_Num)

自己写把,尝试一下

Insert picture description here
If you have any questions, you can leave a message

Welcome to exchange and discuss

Guess you like

Origin blog.csdn.net/qq_45689790/article/details/114177991