malidaOBD/SYSTEM/24cxx.c

290 lines
5.1 KiB
C
Raw Normal View History

2024-08-30 07:20:57 +00:00
#include "24cxx.h"
#include "led.h"
static void IIC1_Start(void); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
static void IIC1_Stop(void); //<2F><><EFBFBD><EFBFBD>ֹͣ<CDA3>ź<EFBFBD>
static void IIC1_Write_Byte(u8 dat);//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
static u8 IIC1_Read_Byte(unsigned char ack);//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
static u8 IIC1_Wait_Ack(void);//<2F>ȴ<EFBFBD>ACK<43>ź<EFBFBD>
static void IIC1_Ack(void);//<2F><><EFBFBD><EFBFBD>ACK<43>ź<EFBFBD>
static void IIC1_NAck(void);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK<43>ź<EFBFBD>
void I2C_delay(uint16_t n)
{
u8 i = 5*n; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD><C5BB>ٶ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>5<EFBFBD><35><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
while(i)
{
i--;
}
}
//<2F><>ʼ<EFBFBD><CABC>IIC
void AT24cxx_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* <20><><EFBFBD><EFBFBD>GPIOʱ<4F><CAB1> */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_11;//SDA,SCL
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
IIC_SDA1;
IIC_SCL1;
}
//<2F><>AT24CXXָ<58><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//ReadAddr :<3A><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
//<2F><><EFBFBD><EFBFBD>ֵ <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
u8 AT24cxx_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC1_Start();
if(EE_TYPE>AT24C16)
{
IIC1_Write_Byte(0xa0);//<2F><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
IIC1_Wait_Ack();
IIC1_Write_Byte(ReadAddr/256);//<2F><><EFBFBD>͸ߵ<CDB8>ַ
}
else IIC1_Write_Byte(0xa0+((ReadAddr/256)<<1));//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ0xA0,д<><D0B4><EFBFBD><EFBFBD>
IIC1_Wait_Ack();
IIC1_Write_Byte(ReadAddr%256);//<2F><><EFBFBD>͵͵<CDB5>ַ
IIC1_Wait_Ack();
IIC1_Start();
if(EE_TYPE>AT24C16)IIC1_Write_Byte(0xa1); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
else IIC1_Write_Byte(0xa1+((ReadAddr/256)<<1));//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
IIC1_Wait_Ack();
temp=IIC1_Read_Byte(1);
IIC1_Stop();//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
return temp;
}
//<2F><>AT24CXXָ<58><D6B8><EFBFBD><EFBFBD>ַдһ<D0B4><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//WriteAddr:д<><D0B4><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
//data :Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void AT24cxx_WriteOneByte(u16 WriteAddr,u8 data)
{
IIC1_Start();
if(EE_TYPE>AT24C16)
{
IIC1_Write_Byte(0xa0);
IIC1_Wait_Ack();
IIC1_Write_Byte(WriteAddr/256);//<2F><><EFBFBD>͸ߵ<CDB8>ַ
}
else IIC1_Write_Byte(0xa0+((WriteAddr/256)<<1));
IIC1_Wait_Ack();
IIC1_Write_Byte(WriteAddr%256);//<2F><><EFBFBD>͵͵<CDB5>ַ
IIC1_Wait_Ack();
IIC1_Write_Byte(data);
IIC1_Wait_Ack();
IIC1_Stop();//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
I2C_delay(2);
}
//<2F><>AT24CXXָ<58><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ʼд<CABC><D0B4>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
//<2F>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>16λ<36><CEBB>32λ<32><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//WriteAddr:<3A><>ʼд<CABC><D0B4><EFBFBD>ĵ<EFBFBD>ַ
//data :Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//len :Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD>ݵij<DDB5><C4B3><EFBFBD>2<EFBFBD><32>4
void AT24cxx_WriteLenByte(u16 WriteAddr,u32 data,u8 len)
{
u8 i;
for(i=0;i<len;i++)
{
AT24cxx_WriteOneByte(WriteAddr+i,(data>>(8*i))&0xff);
}
}
//<2F><>AT24CXXָ<58><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
//<2F>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>16bit<69><74><EFBFBD><EFBFBD>32bit<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
//ReadAddr :<3A><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
//<2F><><EFBFBD><EFBFBD>ֵ :<3A><><EFBFBD><EFBFBD>
//len :Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<DDB5><C4B3><EFBFBD>2,4
u32 AT24cxx_ReadLenByte(u16 ReadAddr,u8 len)
{
u8 i;
u32 temp=0;
for(i=0;i<len;i++)
{
temp<<=8;
temp+=AT24cxx_ReadOneByte(ReadAddr+len-i-1);
}
return temp;
}
//<2F><>AT24CXXָ<58><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ʼд<CABC><D0B4>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
//<2F>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
//WriteAddr:<3A><>ʼд<CABC><D0B4><EFBFBD>ĵ<EFBFBD>ַ
//buf :<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
//len :Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD>ݵĸ<DDB5><C4B8><EFBFBD>
void AT24cxx_Write(u16 WriteAddr,u8 *buf,u16 len)
{
u16 i;
for(i=0;i<len;i++)
{
IWDG_Feed();
AT24cxx_WriteOneByte(WriteAddr,*buf);
WriteAddr++;
buf++;
}
}
//<2F><>AT24CXXָ<58><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
//<2F>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
//ReadAddr:<3A><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĵ<DDB5>ַ
//len :Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD>ݵĸ<DDB5><C4B8><EFBFBD>
//buf :<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
void AT24cxx_Read(u16 ReadAddr,u8 *buf,u16 len)
{
while(len--)
{
IWDG_Feed();
*buf++=AT24cxx_ReadOneByte(ReadAddr++);
}
}
//<2F><><EFBFBD><EFBFBD>AT24CXX<58>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>24XX<58>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ַ(0)<29><><EFBFBD><EFBFBD><E6B4A2>־<EFBFBD><D6BE>.
//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
//<2F><><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
u8 AT24cxx_Check(void)
{
u8 temp1,temp2;
AT24cxx_WriteOneByte(0x010,0x55);
temp1=AT24cxx_ReadOneByte(0x010);//<2F><><EFBFBD><EFBFBD>ÿ<EFBFBD>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>дAT24CXX
temp2=temp1;
if(temp2==0x55)return 0;
else
{
AT24cxx_WriteOneByte(0x010,0x55);
temp1=AT24cxx_ReadOneByte(0x010);
temp2=temp1;
if(temp2==0x55)return 0;
}
return 1;
}
//<2F><><EFBFBD><EFBFBD>IIC<49><43>ʼ<EFBFBD>ź<EFBFBD>
static void IIC1_Start(void)
{
IIC_SDA1;
IIC_SCL1;
I2C_delay(4);
IIC_SDA0;
I2C_delay(4);
IIC_SCL0;
I2C_delay(4);
}
//<2F><><EFBFBD><EFBFBD>IICֹͣ<CDA3>ź<EFBFBD>
static void IIC1_Stop(void)
{
IIC_SDA0;
IIC_SCL1;
I2C_delay(2);
IIC_SDA1;
I2C_delay(4);
// IIC_SCL0;
}
//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ɹ<EFBFBD>
static u8 IIC1_Wait_Ack(void)
{
u8 i;
SDA_IN();
IIC_SCL0;
I2C_delay(2);
IIC_SCL1;
I2C_delay(2);
while(Read_IIC_SDA())
{
i++;
if(i>250)
{
IIC1_Stop();
return 1;
}
}
IIC_SCL0;
I2C_delay(2);
return 0;
}
//<2F><><EFBFBD><EFBFBD>ACKӦ<4B><D3A6>
static void IIC1_Ack(void)
{
IIC_SCL0;
SDA_OUT();
IIC_SDA0;
I2C_delay(2);
IIC_SCL1;
I2C_delay(2);
IIC_SCL0;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<4B><D3A6>
static void IIC1_NAck(void)
{
IIC_SCL0;
SDA_OUT();
IIC_SDA1;
I2C_delay(2);
IIC_SCL1;
I2C_delay(2);
IIC_SCL0;
}
//IIC<49><43><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
//<2F><><EFBFBD>شӻ<D8B4><D3BB><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
//0<><30><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
//1<><31><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
void IIC1_Write_Byte(u8 dat)
{
u8 t;
SDA_OUT();
I2C_delay(2);
for(t=0;t<8;t++)
{
IIC_SCL0;
if(dat&0x80)
{
IIC_SDA1;
}
else
{
IIC_SDA0;
}
I2C_delay(2);
IIC_SCL1;
dat<<=1;
I2C_delay(2);
}
IIC_SCL0;
I2C_delay(2);
}
//<2F><>1<EFBFBD><31><EFBFBD>ֽڣ<D6BD>ack=0ʱ<30><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK,ack=1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>NACK
u8 IIC1_Read_Byte(unsigned char ack)
{
u8 i,receive;
SDA_IN();//SDA<44><41>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
IIC_SDA0;
I2C_delay(2);
for(i=0;i<8;i++)
{
IIC_SCL0;
I2C_delay(2);
IIC_SCL1;
receive<<=1;
I2C_delay(2);
if(Read_IIC_SDA())receive++;
I2C_delay(2);
}
IIC_SDA1;
I2C_delay(2);
IIC_SCL0;
I2C_delay(2);
if(!ack)
IIC1_Ack();
else
IIC1_NAck();
return receive;
}