#ifndef AT24C64_H
#define AT24C64_H
//
#include <Driver\DrvI2C.H>
#include "MYFUN\MYFUN.H"
//写数值到24C64
void Write_24LC64(uint32_t address, uint8_t data)
{
uint32_t u32HCLK, m;
u32HCLK = DrvSYS_GetHCLK() * 1000;
//Open I2C0 and set clock = 50Kbps
DrvI2C_Open(I2C_PORT0, u32HCLK, 20000);
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 0, 0); //set start
while (I2C0->CON.SI == 0)
; //poll si flag
//send writer command
I2C0->DATA = 0XA0; //send writer command
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si flag
while (I2C0->CON.SI == 0)
; //poll si flag
//send address high
I2C0->DATA = (address >> 8) & 0XFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1); //clr si and set ack
while (I2C0->CON.SI == 0)
; //poll si flag
//send address low
I2C0->DATA = address & 0XFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1); //clr si and set ack
while (I2C0->CON.SI == 0)
; //poll si flag
//send data
I2C0->DATA = data; //write data to
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1); //clr si and set ack
while (I2C0->CON.SI == 0)
; //poll si flag
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //send stop
//while( I2C0->CON.SI == 0 );
for (m = 0; m < 60; m++)
;
DrvI2C_Close(I2C_PORT0);
for (m = 0; m < 6000; m++)
;
}
//读数值到24C64
uint8_t Read_24LC64(uint32_t address)
{
uint8_t TEMP;
uint32_t u32HCLK;
//Open I2C0 and set clock = 50Kbps
u32HCLK = DrvSYS_GetHCLK() * 1000;
DrvI2C_Open(I2C_PORT0, u32HCLK,10000);
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0); //set start
while (I2C0->CON.SI == 0)
; //poll si flag
//send writer command
I2C0->DATA = 0XA0;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si
while (I2C0->CON.SI == 0)
; //poll si flag
//send address high
I2C0->DATA = (address >> 8) & 0XFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1); //clr si and set ack
while (I2C0->CON.SI == 0)
; //poll si flag
//send address low
I2C0->DATA = address & 0XFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si and set ack
while (I2C0->CON.SI == 0)
; //poll si flag
//send start flag
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0); //clr si and send start
while (I2C0->CON.SI == 0)
; //poll si flag
//send read command
I2C0->DATA = 0XA1;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1); //clr si
while (I2C0->CON.SI == 0)
; //poll si flag
//resive data
I2C0->DATA = 0XFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si
while (I2C0->CON.SI == 0)
; //poll si flag
TEMP = I2C0->DATA;
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //clr si and set stop
DrvI2C_Close(I2C_PORT0);
return TEMP;
}
//写连续地址uint32型数值
void Write_24LC64L(uint32_t address, int32_t lng)
{
uint8_t chr[3];
uint32_t Lng;
if (lng < 0)
{
Lng = ~lng + 1;
Write_24LC64(address + 0, 111);
}
if (lng >= 0)
{
Lng = lng;
Write_24LC64(address + 0, 222);
}
chr[1] = Lng / 10000 % 100;
chr[2] = Lng / 100 % 100;
chr[3] = Lng / 1 % 100;
Write_24LC64(address + 1, chr[1]);
Write_24LC64(address + 2, chr[2]);
Write_24LC64(address + 3, chr[3]);
}
//读连续地址uint32型数值
int32_t Read_24LC64L(uint32_t address)
{
int32_t lng;
uint8_t chr[4];
chr[1] = Read_24LC64(address + 0);
chr[2] = Read_24LC64(address + 1);
chr[3] = Read_24LC64(address + 2);
chr[4] = Read_24LC64(address + 3);
if (chr[1] == 222)
{
lng = chr[2] * 10000 + chr[3] * 100 + chr[4];
}
if (chr[1] == 111)
{
lng = ~(chr[2] * 10000 + chr[3] * 100 + chr[4]) + 1;
}
return lng;
}
//写连续地址浮点型数值
void Write_24LC64F(uint32_t address, float a)
{
uint8_t i, *px;
uint8_t x[4];
void *pf;
px = x;
pf = &a;
for (i = 0; i < 4; i++)
{
*(px + i) = *((char *) pf + i);
}
Write_24LC64(address + 0, x[0]);
Write_24LC64(address + 1, x[1]);
Write_24LC64(address + 2, x[2]);
Write_24LC64(address + 3, x[3]);
}
//读连续地址浮点型数值
float Read_24LC64F(uint32_t address)
{
float a;
uint8_t i, *px;
uint8_t x[4];
x[0] = Read_24LC64(address + 0);
x[1] = Read_24LC64(address + 1);
x[2] = Read_24LC64(address + 2);
x[3] = Read_24LC64(address + 3);
void *pf;
px = x;
pf = &a;
for (i = 0; i < 4; i++)
{
*((char *) pf + i) = *(px + i);
}
return a;
}
//
#endif
|