本帖最后由 zgy_1218 于 2013-9-26 11:01 编辑
主要问题是写入一字节数据,在读取应答位的时候,应答为高电平,就会返回错误,剩余的数据就写不进去
unsigned char time[7]={0x11,0x37,0x10,0x04,0x26,0x03,0x13},buf[7];
int main(void)
{
system_init();
while(1)
{
WDR();
RX_8025_IIC_WritePage(0,7,time) ;
RX_8025_IIC_ReadPage(0,7,buf);
}
}
#define RX_8025_IIC_ADDRESS 0x64
#define RX_8025_SDA_1 PORTB|=(1<<PB5)
#define RX_8025_SDA_0 PORTB&=~(1<<PB5)
#define RX_8025_SDA_DDR_1 DDRB|=(1<<PB5)
#define RX_8025_SDA_DDR_0 DDRB&=~(1<<PB5)
#define RX_8025_SCL_1 PORTB|=(1<<PB6)
#define RX_8025_SCL_0 PORTB&=~(1<<PB6)
#define RX_8025_SCL_DDR_1 DDRB|=(1<<PB6)
#define RX_8025_SCL_DDR_0 DDRB&=~(1<<PB6)
/**************************RX_8025_IIC初始化**************************/
void RX_8025_IIC_Init(void)
{
RX_8025_SDA_DDR_1;//设定SDA输出
RX_8025_SCL_DDR_1;//设定SCL输出
}
/***************************************RX_8025_IIC起始信号*******************************/
void RX_8025_IIC_Start(void)
{
RX_8025_SDA_1;
RX_8025_SCL_1;
RX_8025_delay5us();
RX_8025_SDA_0;
RX_8025_delay5us();
RX_8025_SCL_0;
}
/**************************************RX_8025_IIC结束信号********************************/
void RX_8025_IIC_Stop(void)
{
RX_8025_SDA_0;
RX_8025_SCL_1;
RX_8025_delay5us();
RX_8025_SDA_1;
RX_8025_delay5us();
}
/*************************************RX_8025_IIC应答信号***********************************/
unsigned char RX_8025_IIC_ASK()
{
unsigned char i=0;
RX_8025_SCL_DDR_1;//设定SCL输出
RX_8025_SDA_DDR_0;//设定SDA为输入引脚
// RX_8025_SDA_0;
RX_8025_SCL_1;
RX_8025_delay5us();
while(PINB&0x20)
{
i++;
if(i>=200)
{
RX_8025_SCL_0;
RX_8025_SDA_DDR_1;//设定SDA为输出引脚
return False;
}
}
RX_8025_SCL_0;
RX_8025_delay5us();
RX_8025_SDA_DDR_1;//设定SDA为输出引脚
return True;
}
/**************************RX_8025_IIC发送应答*******************************************/
void RX_8025_IIC_SendACK(void)
{
RX_8025_SDA_0;
RX_8025_SCL_1;
RX_8025_delay5us();
RX_8025_SCL_0;
RX_8025_delay5us();
RX_8025_SDA_1;
}
/**************************RX_8025_IIC发送无应答*******************************************/
void RX_8025_IIC_SendNACK(void)
{
RX_8025_SDA_1;
RX_8025_SCL_1;
RX_8025_delay5us();
RX_8025_SCL_0;
RX_8025_delay5us();
}
/**************************************写一个字节***************************/
void RX_8025_IIC_WriteByte(unsigned char dat)
{
unsigned char i;
RX_8025_SDA_DDR_1;//设定SDA为输出引脚
RX_8025_SCL_DDR_1;//设定SCL输出
for(i=0;i<8;i++)
{
if((dat&0x80)==0x80)
{
RX_8025_SDA_1;
}
else
{
RX_8025_SDA_0;
}
RX_8025_SCL_1; //SCL 拉低数据写完毕
RX_8025_delay5us();
RX_8025_SCL_0; //SCL 拉高准备写数据
RX_8025_delay5us();
dat=dat<<1;
}
// RX_8025_IIC_ASK();
}
/***********************读数据***************************/
unsigned char RX_8025_IIC_ReadByte(void)
{
unsigned char i,dat=0;
RX_8025_SDA_DDR_0;//设定SDA为输入引脚
RX_8025_SDA_1;
for(i=0;i<8;i++)
{
RX_8025_SCL_1; //SCL 拉高准备读数据
RX_8025_delay5us();
dat=dat<<1;
if(PINB&0x20==0x20)
{
dat=dat|0x01;
}
RX_8025_SCL_0; //SCL 拉低数据写完毕
RX_8025_delay5us();
}
RX_8025_SDA_DDR_1;//设定SDA为输出引脚
return dat;
}
/***********************************************固定地址写入一个数据*************************************/
unsigned char RX_8025_IIC_Write_ADD_Byte(unsigned char add,unsigned char dat)
{
RX_8025_IIC_Start();
RX_8025_IIC_WriteByte(RX_8025_IIC_ADDRESS);
if(RX_8025_IIC_ASK()==True)
{
RX_8025_IIC_WriteByte(add);
if(RX_8025_IIC_ASK()==True)
{
RX_8025_IIC_WriteByte(dat);
if(RX_8025_IIC_ASK()==True)
{}
else
return False;
}
else
return False;
}
else
return False;
RX_8025_IIC_Stop();
RX_8025_Delayms(10); //5ms的延时很关键,不然写入会出错
return True;
}
/***********************随机地址读取数据*************************************************/
unsigned char RX_8025_IIC_Read_ADD_Byte(unsigned char add)
{
unsigned char dat;
RX_8025_IIC_Start();
RX_8025_IIC_WriteByte(RX_8025_IIC_ADDRESS);
if(RX_8025_IIC_ASK()==True)
{
RX_8025_IIC_WriteByte(add);
if(RX_8025_IIC_ASK()==True)
{
RX_8025_IIC_Start();
RX_8025_IIC_WriteByte(RX_8025_IIC_ADDRESS|0x01); //读操作
if(RX_8025_IIC_ASK()==True)
dat=RX_8025_IIC_ReadByte();
else
return False;
}
else
return False;
}
else
return False;
RX_8025_IIC_SendNACK();
RX_8025_IIC_Stop();
return dat;
}
/****************************************RX_8025连续读数据****************************************************/
unsigned char RX_8025_IIC_ReadPage(unsigned char add,unsigned char n,unsigned char *dat)
{
unsigned char i;
RX_8025_IIC_Start();
RX_8025_IIC_WriteByte(RX_8025_IIC_ADDRESS);
if(RX_8025_IIC_ASK()==True)
{
RX_8025_IIC_WriteByte(add);
if(RX_8025_IIC_ASK()==True)
{
RX_8025_IIC_Start();
RX_8025_IIC_WriteByte(RX_8025_IIC_ADDRESS|0x01); //读操作
if(RX_8025_IIC_ASK()==True)
{
for(i=0;i<n-1;i++)
{
*dat=RX_8025_IIC_ReadByte();
RX_8025_IIC_SendACK();
dat++;
}
*dat=RX_8025_IIC_ReadByte();
RX_8025_IIC_SendNACK();
}
else
return False;
}
else
return False;
}
else
return False;
RX_8025_IIC_Stop();
return True;
}
/***********************************************连续数据写入**********************************************/
unsigned char RX_8025_IIC_WritePage(unsigned char add,unsigned char n,unsigned char *dat)
{
unsigned char i;
WDR();
RX_8025_IIC_Start();
RX_8025_IIC_WriteByte(RX_8025_IIC_ADDRESS);
if(RX_8025_IIC_ASK()==True)
{
RX_8025_IIC_WriteByte(add);
if(RX_8025_IIC_ASK()==True)
{
for(i=0;i<n;i++)
{
RX_8025_IIC_WriteByte(*dat);
if(RX_8025_IIC_ASK()==True)
{
dat++;
}
else
return False;
}
}
else
return False;
}
else
return False;
RX_8025_IIC_Stop();
WDR();
RX_8025_Delayms(15);
return True;
}
void RX_8025_delay5us()
{
unsigned char n = 3;
while (n--)
{
nop();
}
}
void RX_8025_Delayus(unsigned int lus)
{
while(lus--);
WDR();
}
void RX_8025_Delayms(unsigned int lms)
{
while(lms--)
{
RX_8025_Delayus(1300);
WDR();
}
}
|