请问为什么只有0x06,0x02,0x04三个寄存器不能进行写操作。求指点迷津!
程序如下:
#include "IIC_Master.h"
static void delay()
{
delay_us(10);
}
void IIC_Init(void)
{
BIT_OUT(IIC_SDA);
BIT_OUT(IIC_SCL);
SET_BIT(IIC_SDA);
SET_BIT(IIC_SCL);
}
void IIC_Start(void)
{
SET_BIT(IIC_SCL);
delay();
CLR_BIT(IIC_SDA);
delay();
}
void IIC_Stop(void)
{
CLR_BIT(IIC_SDA);
delay();
SET_BIT(IIC_SCL);
delay();
SET_BIT(IIC_SDA);
delay();
}
void IIC_WriteByte(INT8U data)
{
CLR_BIT(IIC_SCL);
delay();
for(int i = 0; i < 8; ++i)
{
if(data & 0x80)
{
SET_BIT(IIC_SDA);
}
else
{
CLR_BIT(IIC_SDA);
}
data = data << 1;
delay();
SET_BIT(IIC_SCL);
delay();
CLR_BIT(IIC_SCL);
delay();
}
}
INT8U IIC_ReadByte(void)
{
INT8U data=0;
CLR_BIT(IIC_SCL);
BIT_IN(IIC_SDA);
delay();
for(int i = 0; i < 8; ++i)
{
data = data << 1;
SET_BIT(IIC_SCL);
delay();
if(TEST_BIT(IIC_SDA))
{
data |= 0x01;
}
CLR_BIT(IIC_SCL);
delay();
}
BIT_OUT(IIC_SDA);
return data;
}
INT8U IIC_ReadAck()
{
INT8U ACK=0;
CLR_BIT(IIC_SCL);
BIT_IN(IIC_SDA);
delay();
SET_BIT(IIC_SCL);
delay();
ACK = TEST_BIT(IIC_SDA);
CLR_BIT(IIC_SCL);
delay();
BIT_OUT(IIC_SDA);
return ACK;
}
void IIC_SendAck(INT8U ACK)
{
CLR_BIT(IIC_SCL);
delay();
if(ACK)
{
SET_BIT(IIC_SDA);
}
else
{
CLR_BIT(IIC_SDA);
}
delay();
SET_BIT(IIC_SCL);
delay();
CLR_BIT(IIC_SCL);
delay();
}
INT8U IIC_WriteOneByteRegister(INT8U DeviceAddr,
INT8U RegisterAddr,
INT8U RegisterData)
{
INT8U ACK=0;
IIC_Start();
IIC_WriteByte(DeviceAddr << 1); //设备地址
ACK += IIC_ReadAck();
IIC_WriteByte(RegisterAddr); //寄存器地址
ACK += IIC_ReadAck();
IIC_WriteByte(RegisterData);
ACK += IIC_ReadAck();
IIC_Stop();
return ACK;
}
INT8U IIC_ReadOneByteRegister(INT8U DeviceAddr,INT8U RegisterAddr)
{
INT8U data=0;
INT8U ACK=0;
INT8U Addr=DeviceAddr << 1;
IIC_Start();
IIC_WriteByte(Addr);
ACK += IIC_ReadAck();
IIC_WriteByte(RegisterAddr);
ACK += IIC_ReadAck();
IIC_Start();
IIC_WriteByte(Addr + 1);
ACK += IIC_ReadAck();
data = IIC_ReadByte();
IIC_SendAck(IIC_NACK);
IIC_Stop();
return data;
}
void IIC_ReadMulByteRegister(INT8U DeviceAddr,INT8U RegisterAddr,INT8U * Reg_data) //读取FIFO寄存器中的数据
{
INT8U m,n,i,l,ACK=0;
INT8U Addr=DeviceAddr << 1;
m=IIC_ReadOneByteRegister(0x57,0x02); //读取写指针
n=IIC_ReadOneByteRegister(0x57,0x04); //读取读指针
IIC_Start();
IIC_WriteByte(Addr);
ACK += IIC_ReadAck();
IIC_WriteByte(RegisterAddr);
ACK += IIC_ReadAck();
IIC_Start();
IIC_WriteByte(Addr + 1);
ACK += IIC_ReadAck();
for(l=0;l<m-n;l++)
{
for(i=0;i<4;i++)
{
Reg_data[i] = IIC_ReadByte();
if(i==3)
IIC_SendAck(IIC_NACK);
else
IIC_SendAck(IIC_ACK);
}
}
IIC_Stop();
IIC_Start(); //重新写入读指针
IIC_WriteByte(Addr + 1);
IIC_WriteByte(0x04);
IIC_WriteByte(0x00);
IIC_Stop();
}
|