/**
* @brief AT24C02初始化
*
*/
void AT24C02_Init(void)
{
IIC_Init(); // 初始化IIC总线
}
/**
* @brief AT24C02字节写入
*
* @param Address: 字节地址
* @param Data: 待写入的数据
*/
void AT24C02_ByteWrite(uint8_t Address, uint8_t Data)
{
IIC_StartSignal(); // 发送开始信号
// 发送设备地址,写操作
IIC_SendBytes(0xA0);
if (IIC_WaitACK() == 1) // AT24C02没应答
{
printf("[AT24C02] Answered the device address: Error\n");
IIC_StopSignal(); // 发送停止信号
}
else
{
printf("[AT24C02] Answered the device address: OK\n");
}
// 发送字地址
IIC_SendBytes(Address);
if (IIC_WaitACK() == 1) // AT24C02没应答
{
printf("[AT24C02] Answered the word address: Error\n");
IIC_StopSignal(); // 发送停止信号
}
else
{
printf("[AT24C02] Answered the word address: OK\n");
}
// 发送待写入的数据
IIC_SendBytes(Data);
if (IIC_WaitACK() == 1) // AT24C02没应答
{
printf("[AT24C02] Answered the data: Error\n");
IIC_StopSignal(); // 发送停止信号
}
else
{
printf("[AT24C02] Answered the data: OK\n");
}
IIC_StopSignal(); // 发送停止信号
}
/**
* @brief AT24C02页编程(8字节/页)
*
* @param Address: 页地址
* @param buf: 待写入的数据
* @param DataLen: 待写入的数据长度
*/
void AT24C02_PageWrite(uint32_t Address, uint8_t *buf, uint8_t DataLen)
{
IIC_StartSignal(); // 发送开始信号
// 发送设备地址,写操作
IIC_SendBytes(0xA0);
if (IIC_WaitACK() == 1) // AT24C02没应答
{
printf("[AT24C02] Answered the device address: Error\n");
IIC_StopSignal(); // 发送停止信号
}
else
{
printf("[AT24C02] Answered the device address: OK\n");
}
// 发送页地址
IIC_SendBytes(Address);
if (IIC_WaitACK() == 1) // AT24C02没应答
{
printf("[AT24C02] Answered the page address: Error\n");
IIC_StopSignal(); // 发送停止信号
}
else
{
printf("[AT24C02] Answered the page address: OK\n");
}
// 循环发送数据
while (DataLen--)
{
IIC_SendBytes(*buf++); // 发送数据
if (IIC_WaitACK() == 1) // AT24C02没应答
{
printf("[AT24C02] Answered the data: Error\n");
IIC_StopSignal(); // 发送停止信号
}
else
{
printf("[AT24C02] Answered the data: OK\n");
}
}
IIC_StopSignal(); // 发送停止信号
}
/**
* @brief AT24C02当前地址读
*
* @return uint8_t 当前地址的数据
*/
uint8_t AT24C02_CurrentAddressRead(void)
{
uint8_t Data;
IIC_StartSignal(); // 发送开始信号
// 发送设备地址,读操作
IIC_SendBytes(0xA1);
if (IIC_WaitACK() == 1) // AT24C02没应答
{
printf("[AT24C02] Answered the device address: Error\n");
IIC_StopSignal(); // 发送停止信号
}
else
{
printf("[AT24C02] Answered the device address: OK\n");
}
// 读取1字节数据
Data = IIC_ReadBytes();
// 发送应答信号
IIC_MasterACK(1); // 不应答
IIC_StopSignal(); // 发送停止信号
return Data; // 返回接收到的数据
}
uint8_t data;
AT24C02_Init()
AT24C02_PageWrite(0x00, "0123456", 6);
HAL_Delay(100);
AT24C02_ByteWrite(0x07, '7');
HAL_Delay(100);
data = AT24C02_CurrentAddressRead();
printf("AT24C02_CurrentAddressRead: %c", data);
|