|
|
|
|
#include "24cxx.h"
|
|
|
|
|
#include "delay.h"
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>ѧϰʹ<EFBFBD>ã<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD>;
|
|
|
|
|
//ALIENTEK<EFBFBD><EFBFBD>ӢSTM32<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//24CXX<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD>ʺ<EFBFBD>24C01~24C16,24C32~256δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!<EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>֤!)
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>@ALIENTEK
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳:www.openedv.com
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:2012/9/9
|
|
|
|
|
//<EFBFBD>汾<EFBFBD><EFBFBD>V1.0
|
|
|
|
|
//<EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؾ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//Copyright(C) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾ 2009-2019
|
|
|
|
|
//All rights reserved
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>IIC<EFBFBD>ӿ<EFBFBD>
|
|
|
|
|
void AT24CXX_Init(void)
|
|
|
|
|
{
|
|
|
|
|
MYIIC_Init();
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>AT24CXXָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//ReadAddr:<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ :<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
|
|
|
|
|
{
|
|
|
|
|
u8 temp=0;
|
|
|
|
|
MYIIC_Start();
|
|
|
|
|
if(EE_TYPE>AT24C16)
|
|
|
|
|
{
|
|
|
|
|
IIC_Send_Byte(0XA0); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
IIC_Send_Byte(ReadAddr>>8);//<EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ַ
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
}else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ0XA0,д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
IIC_Send_Byte(ReadAddr%256); //<EFBFBD><EFBFBD><EFBFBD>͵͵<EFBFBD>ַ
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
MYIIC_Start();
|
|
|
|
|
IIC_Send_Byte(0XA1); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
temp=IIC_Read_Byte(0);
|
|
|
|
|
MYIIC_Stop();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ֹͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>AT24CXXָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַд<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//WriteAddr :д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
|
|
|
|
|
//DataToWrite:Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
|
|
|
|
|
{
|
|
|
|
|
MYIIC_Start();
|
|
|
|
|
if(EE_TYPE>AT24C16)
|
|
|
|
|
{
|
|
|
|
|
IIC_Send_Byte(0XA0); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
IIC_Send_Byte(WriteAddr>>8);//<EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ַ
|
|
|
|
|
}else
|
|
|
|
|
{
|
|
|
|
|
IIC_Send_Byte(0XA0+((WriteAddr/256)<<1)); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ0XA0,д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
IIC_Send_Byte(WriteAddr%256); //<EFBFBD><EFBFBD><EFBFBD>͵͵<EFBFBD>ַ
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
IIC_Send_Byte(DataToWrite); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
|
|
MYIIC_Wait_Ack();
|
|
|
|
|
MYIIC_Stop();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ֹͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
delay_ms(10);
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>AT24CXX<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼд<EFBFBD>볤<EFBFBD><EFBFBD>ΪLen<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>16bit<EFBFBD><EFBFBD><EFBFBD><EFBFBD>32bit<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
//WriteAddr :<EFBFBD><EFBFBD>ʼд<EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
|
|
|
|
|
//DataToWrite:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
//Len :Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>2,4
|
|
|
|
|
void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len)
|
|
|
|
|
{
|
|
|
|
|
u8 t;
|
|
|
|
|
for(t=0;t<Len;t++)
|
|
|
|
|
{
|
|
|
|
|
AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD>AT24CXX<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪLen<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>16bit<EFBFBD><EFBFBD><EFBFBD><EFBFBD>32bit<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
//ReadAddr :<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ :<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//Len :Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>2,4
|
|
|
|
|
u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len)
|
|
|
|
|
{
|
|
|
|
|
u8 t;
|
|
|
|
|
u32 temp=0;
|
|
|
|
|
for(t=0;t<Len;t++)
|
|
|
|
|
{
|
|
|
|
|
temp<<=8;
|
|
|
|
|
temp+=AT24CXX_ReadOneByte(ReadAddr+Len-t-1);
|
|
|
|
|
}
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>AT24CXX<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>24XX<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ(255)<EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD><EFBFBD>־<EFBFBD><EFBFBD>.
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>24Cϵ<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַҪ<EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
u8 AT24CXX_Check(void)
|
|
|
|
|
{
|
|
|
|
|
u8 temp;
|
|
|
|
|
temp=AT24CXX_ReadOneByte(255);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>дAT24CXX
|
|
|
|
|
if(temp==0X55)return 0;
|
|
|
|
|
else//<EFBFBD>ų<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>γ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
AT24CXX_WriteOneByte(255,0X55);
|
|
|
|
|
temp=AT24CXX_ReadOneByte(255);
|
|
|
|
|
if(temp==0X55)return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD>AT24CXX<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//ReadAddr :<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ <EFBFBD><EFBFBD>24c02Ϊ0~255
|
|
|
|
|
//pBuffer :<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
//NumToRead:Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĸ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead)
|
|
|
|
|
{
|
|
|
|
|
while(NumToRead)
|
|
|
|
|
{
|
|
|
|
|
*pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);
|
|
|
|
|
NumToRead--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>AT24CXX<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ʼд<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//WriteAddr :<EFBFBD><EFBFBD>ʼд<EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ <EFBFBD><EFBFBD>24c02Ϊ0~255
|
|
|
|
|
//pBuffer :<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
//NumToWrite:Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĸ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)
|
|
|
|
|
{
|
|
|
|
|
while(NumToWrite--)
|
|
|
|
|
{
|
|
|
|
|
AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
|
|
|
|
|
WriteAddr++;
|
|
|
|
|
pBuffer++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|