#include<p30f6015.h>
#include<ckdy.h>
#include<ACVFD.H>
#include<i2c.h>
#define SDAA_AS_OUT() {TRISGbits.TRISG3=0;}
#define SDAA_AS_IN() {TRISGbits.TRISG3=1;}
#define SDAA_SET() {LATGbits.LATG3=1;}
#define SDAA_CLR() {LATGbits.LATG3=0;}
#define SDAA PORTGbits.RG3
#define SCLA_AS_OUT() {TRISGbits.TRISG2=0;}
#define SCLA_SET() {LATGbits.LATG2=1;}
#define SCLA_CLR() {LATGbits.LATG2=0;}
#define SCLA LATGbits.LATG2
/************************************************************
*Function: IIC
*parameter:
*Return:
*Modify:
*************************************************************/
void Start24C02(void) //开始
{
SDAA_AS_OUT();
SDAA_SET();
delay(10); //SDA=1
SCLA_SET();
delay(10); //SCL=1
SDAA_CLR();
delay(10); //SDA=0
}
/************************************************************
*Function: IIC
*parameter:
*Return:
*Modify:
*************************************************************/
void Stop24C02(void)//停止
{
SDAA_AS_OUT();
SDAA_CLR();
delay(10);//SDA=0
SCLA_SET();
delay(10);//SCL=1
SDAA_SET();
delay(10);//SDA=1
SDAA_AS_IN();
}
/***************************************************************************
IIC
***************************************************************************/
void Ack24C02(void)//响应
{
unsigned char i;
SDAA_AS_OUT();
SCLA_CLR();
delay(10);//SCL=0
SDAA_SET();
delay(10);//SDA=1
SCLA_SET();
delay(10);//SCL=1
SDAA_AS_IN();
while (SDAA && i<100)//SDA_24C02=1时表示没响应,延时等待响应SDA_24C02=0。I>10时强制退出
{
i++;
}
SCLA_CLR();
delay(10);
}
/***************************************************************************
IIC
***************************************************************************/
void NoAck()
{
SDAA_AS_OUT();
SDAA_SET();
delay(10);
SCLA_SET();
delay(10);
SCLA_CLR();
delay(10);
SDAA_AS_IN();
}
/***************************************************************************
IIC
***************************************************************************/
unsigned char TestAck()
{
unsigned char ack;
SDAA_AS_IN();
SCLA_SET();
delay(10);
SDAA_AS_IN();
delay(10);
ack=SDAA;
delay(10);
SCLA_CLR();
delay(10);
return(ack);
}
/************************************************************
*Function: IIC
*parameter:
*Return:
*Modify:
*************************************************************/
void WriteByte24C02(unsigned char command)
{
int i;
SDAA_AS_OUT();
for (i=0;i<8;i++)
{
if ((command&0x80)==0x80)
{
SDAA_SET();
}
else
{
SDAA_CLR();
}
delay(20);
SCLA_SET();
delay(20);
SCLA_CLR();
delay(20);
command=command<<1;
}
SDAA_SET();
SDAA_AS_IN();;
}
/************************************************************
*Function: IIC
*parameter:
*Return:
*Modify:
*************************************************************/
unsigned char ReadByte24C02(void)
{
unsigned char i,rdata=0;
SDAA_AS_IN();
for (i=0;i<8;i++)
{
SCLA_SET();
delay(10);
rdata=rdata<<1;
SDAA_AS_IN();
if (SDAA)
{
rdata|=0x01;
}
delay(10);
SCLA_CLR();
delay(10);
}
return (rdata);
}
void Init24C02(void)
{
SCLA_AS_OUT();
SDAA_AS_IN();
SCLA_CLR();
}
//chipAddr 选择器件地址, firstWrAdd 数据存入首地址,wrData[]待保存的数据,wrNumber待保存数据个数
//输出参数:无
void WriteNByte24C02(unsigned char wrChipAddr, unsigned char firstWrAdd, unsigned char wrData[], unsigned char wrNumber)
{
unsigned char n;
for (n=0; n<wrNumber; n++)//单子节循环存入
{
Start24C02();//开始命令
WriteByte24C02(wrChipAddr); //写入器件地址,
Ack24C02();
WriteByte24C02(firstWrAdd+n);//写入数据要存入首地址
Ack24C02();
WriteByte24C02(wrData[n]);
Ack24C02();
Stop24C02(); //停止信号
delay(1000);//1字节写完之后的延时
}
}
//读N各字节函数 指定地址顺序读数据方式
//输入参数,3个
//reChipAddr 器件地址,firstReAdd 待读数据的首地址,reNumber 待读数据个数
//输出参数:1个 reData[]读出的数据寄存器
void ReadNByte24C02(unsigned char reChipAddr, unsigned char reData[], unsigned char firstReAdd, unsigned char reNumber)
{
unsigned char n;
Start24C02();//开始信号
WriteByte24C02(reChipAddr-1);//因为要先写入ep读的起始地址,所以写入器件地址,(使能器件,处于写状态 )
Ack24C02();
WriteByte24C02(firstReAdd); //写入字节要读出的起始地址
Ack24C02();
for (n=0; n<reNumber; n++)
{
Start24C02();
WriteByte24C02(reChipAddr); //写入器件地址,(使能器件,处于读状态)
Ack24C02();
reData[n] = ReadByte24C02();//读单子节,读后存于数组reData[]中
if (n < (reNumber-1))
{
Ack24C02();
}
}
Stop24C02(); //停止信号
}
|