📄 eerom.c
字号:
/****************************************Copyright (c)**************************************************
** 上海新时达电气有限公司
** 研 发 中 心
** 研发一部
**
** http://www.stepelevatar.com
**
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: EEROM.c
**创 建 人: 吕海安
**最后修改日期: 2007年01月10日
**描 述: EEROM (AT24C08)的底层函数
**
**--------------历史版本信息----------------------------------------------------------------------------
** 创建人: 吕海安
** 版 本: v1.0
** 日 期: 2007年01月10日
** 描 述: 原始版本
**
**--------------当前版本修订------------------------------------------------------------------------------
** 修改人: 吕海安
** 日 期: 2007年07月10日
** 描 述: For MP3 语音报站器
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#include "config.h"
#define AT24C08 0xa8
#define PAGE_SIZE 16
#define MAX_uint16_ADDR 0x7ff
void eeprom_write_byte(INT16U addr,INT8U ch)
{
INT16U address;
address = ((addr >> 7) & 0x6) + AT24C08;
InitI2c(0);
StartI2C();
SendByte(address);
SendByte(addr & 0xff);
SendByte(ch);
StopI2C();
}
INT8U eeprom_read_byte(INT16U addr)
{
INT8U result;
INT16U address;
address = ((addr >> 7) & 0x6) + AT24C08;
InitI2c(0);
StartI2C();
SendByte(address);
SendByte(addr & 0xff);
StartI2C();
SendByte(address + 1);
result = RcvByteNA();
StopI2C();
return (result);
}
/*********************************************************************
函数功能:连续读eerom
输入 :INT16U addr 读取数据的初始地址
INT8U *ch 指向读取数据的指针
INT16U len 读取数据的长度
返回 :无
**********************************************************************/
void eeprom_read_block(INT8U *ch,INT16U addr,INT16U len)
{
INT16U address,i;
if (len == 0) return;
if(addr > MAX_uint16_ADDR) //防止地址溢出
{
return;
}
address = ((addr >> 7) & 0x6) + AT24C08;
InitI2c(0);
StartI2C();
SendByte(address);
SendByte(addr & 0xff);
StartI2C();
SendByte(address + 1);
for (i=len; i!=0; i--,ch++)
{
if (i == 1)
{
*ch = RcvByteNA();
}
else
{
*ch = RcvByteA();
}
}
StopI2C();
}
/**********************************************************************************************************************
函数功能:连续写eerom
输入 :INT16U addr 写入数据的初始地址
INT8U *ch 指向写入数据的指针
INT16U len 写入数据的长度
返回 :无
其他说明: 原程序中有BUG,如果初始地址不是一页的起始地址,即xxxx0000,那么写入的时候有错误;
假设初始地址为xxxx0001,则前面15位数据分别写入xxxx0001~xxxx1111,第16位数据写入xxxx0000
***********************************************************************************************************************/
void eeprom_write_block(INT8U *ch,INT16U addr,INT16U len)
{
INT16U address,i,j,addr_low;
if (len == 0) return;
if(addr > MAX_uint16_ADDR) //防止地址溢出
{
return;
}
while (len > 0)
{
address = ((addr >> 7) & 0x6) + AT24C08;
InitI2c(0);
StartI2C();
SendByte(address);
SendByte(addr & 0xff);
address = addr & (PAGE_SIZE - 1); //如果初始地址不是一页的起始地址,则计算这页剩余可写入的字节数,即PAGE_SIZE - i
if(len > (PAGE_SIZE - address))
{
len = len - (PAGE_SIZE - address);
j = PAGE_SIZE - address;
}
else
{
j = len;
len = 0;
}
for (i = 0; i < j; i++,ch++)
{
SendByte(*ch);
}
StopI2C();
addr = (addr & (~(((INT16U)PAGE_SIZE) - 1))) + PAGE_SIZE;
if(addr > MAX_uint16_ADDR) //防止地址溢出
{
addr = 0;
}
wait(); //Twr : Write Cycle Time, The write cycle time is the time from a valid stop condition of a write sequence to the end of the internal clear/write cycle
}
}
/*********************************************************************************************************
** End Of File
*********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -