添加很多设备
This commit is contained in:
+139
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* at24cxx.c
|
||||
*
|
||||
* Created on: Mar 3, 2024
|
||||
* Author: gxms0
|
||||
*/
|
||||
#include "at24cxx.h"
|
||||
|
||||
|
||||
void at24cxx_init()
|
||||
{
|
||||
|
||||
}
|
||||
/*
|
||||
|
||||
uint8_t x24Cxx_ReadByte(uint16_t u16Addr)
|
||||
{
|
||||
uint8_t u8Data = 0;
|
||||
IIC_Start();//起始信号
|
||||
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_Start();//起始信号
|
||||
IIC_WriteByte(DEV_ADDR | READ_CMD);//器件寻址+读
|
||||
IIC_WaitAck();//等待应答
|
||||
u8Data = IIC_ReadByte();
|
||||
IIC_NoAck();
|
||||
IIC_Stop();//停止信号
|
||||
return u8Data;
|
||||
}
|
||||
|
||||
|
||||
void at24cxx_WriteByte(uint16_t u16Addr, uint8_t u8Data)
|
||||
{
|
||||
x24Cxx_WriteEnable();//使能写入
|
||||
IIC_Start();//起始信号
|
||||
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte(u8Data);
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_Stop();//停止信号
|
||||
x24Cxx_WriteDisble();//禁止写入
|
||||
}
|
||||
|
||||
void x24Cxx_ReadPage(uint16_t u16Addr, uint8_t u8Len, uint8_t *pBuff)
|
||||
{
|
||||
uint8_t i;
|
||||
IIC_Start();//起始信号
|
||||
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_Start();//起始信号
|
||||
IIC_WriteByte(DEV_ADDR | READ_CMD);//器件寻址+读
|
||||
IIC_WaitAck();//等待应答
|
||||
if (u8Len > PAGE_SIZE)//长度大于页的长度
|
||||
{
|
||||
u8Len = PAGE_SIZE;
|
||||
}
|
||||
if ((u16Addr + (uint16_t)u8Len) > CAPACITY_SIZE)//超过容量
|
||||
{
|
||||
u8Len = (uint8_t)(CAPACITY_SIZE - u16Addr);
|
||||
}
|
||||
if (((u16Addr % PAGE_SIZE) + (uint16_t)u8Len) > PAGE_SIZE)//判断是否跨页
|
||||
{
|
||||
u8Len -= (uint8_t)((u16Addr + (uint16_t)u8Len) % PAGE_SIZE);//跨页,截掉跨页的部分
|
||||
}
|
||||
for (i = 0; i < (u8Len - 1); i++)
|
||||
{
|
||||
*(pBuff + i) = IIC_ReadByte();
|
||||
IIC_Ack();//主机应答
|
||||
}
|
||||
*(pBuff + u8Len - 1) = IIC_ReadByte();
|
||||
IIC_NoAck();//最后一个不应答
|
||||
IIC_Stop();//停止信号
|
||||
}
|
||||
|
||||
|
||||
void at24cxx_WritePage(uint16_t u16Addr, uint8_t u8Len, uint8_t *pData)
|
||||
{
|
||||
uint8_t i;
|
||||
x24Cxx_WriteEnable();//使能写入
|
||||
IIC_Start();//起始信号
|
||||
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
|
||||
IIC_WaitAck();//等待应答
|
||||
if (u8Len > PAGE_SIZE)//长度大于页的长度
|
||||
{
|
||||
u8Len = PAGE_SIZE;
|
||||
}
|
||||
if ((u16Addr + (uint16_t)u8Len) > CAPACITY_SIZE)//超过容量
|
||||
{
|
||||
u8Len = (uint8_t)(CAPACITY_SIZE - u16Addr);
|
||||
}
|
||||
if (((u16Addr % PAGE_SIZE) + (uint16_t)u8Len) > PAGE_SIZE)//判断是否跨页
|
||||
{
|
||||
u8Len -= (uint8_t)((u16Addr + (uint16_t)u8Len) % PAGE_SIZE);//跨页,截掉跨页的部分
|
||||
}
|
||||
|
||||
for (i = 0; i < u8Len; i++)
|
||||
{
|
||||
IIC_WriteByte(*(pData + i));
|
||||
IIC_WaitAck();//等待应答
|
||||
}
|
||||
IIC_Stop();//停止信号
|
||||
x24Cxx_WriteDisble();//禁止写入
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user