打印

LPC2119的IO模拟I2C读写PCF8563问题

[复制链接]
1686|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
gghhz|  楼主 | 2012-9-6 17:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
大侠帮忙看一下啊。读出来的数据不对,写进去的数据可能也不对,因为中断不动作

/*******************************************
启动I2C
*******************************************/
void I2CStart(void)
{
DATAHIG;

SCLRSET;
Delay_Us(60);
DATALOW;
Delay_Us(60);
SCLRCLR;
Delay_Us(60);
}
/*******************************************
停止I2C
*******************************************/
void I2CStop(void)
{
DATALOW;

SCLRCLR;
Delay_Us(60);
SCLRSET;
Delay_Us(60);
DATAHIG;
Delay_Us(60);
}
/*******************************************
写1
*******************************************/
void WriteHigh(void)
{
DATAHIG;
SCLRSET;
Delay_Us(60);
SCLRCLR;
Delay_Us(60);
}
/*******************************************
写0
*******************************************/
void WriteLow(void)
{
DATALOW;

SCLRSET;
Delay_Us(60);
SCLRCLR;
Delay_Us(60);
}
/*******************************************
写一个字节
*******************************************/
void WriteOneByte(uint8 Data)
{
uint8 i;
for(i=0;i<8;i++)
    {
     if((Data<<8)&0x80)
         WriteHigh();
     else
         WriteLow();   
     }
}
/*******************************************
写多个字节
*******************************************/
void WriteNByte(uint8 *Data,uint8 WriteLength)
{
uint8 i;

I2CStart();
for(i=0;i<WriteLength;i++)
    {
     WriteOneByte(Data[i]);
     }
I2CStop();   
}
/*******************************************
连续写入
*******************************************/
void WritePCF8563(uint8 WriteData,uint8 WriteAddr)
{
//uint8 i;
uint8 WriteBuf[4];

WriteBuf[0] = 0xa2;
WriteBuf[1] = WriteAddr;
WriteBuf[2] = WriteData;

WriteNByte(WriteBuf,3);
}
/*******************************************
写一个字节
*******************************************/
void WriteByte(uint8 Data)
{
uint8 i;


for(i=0;i<8;i++)
    {
     if((Data<<i)&0x80)
         WriteHigh();
     else
         WriteLow();   
     }
   
}
/*******************************************

*******************************************/
void WriteNoACK(void)
{
DATAHIG;

Delay_Us(60);
SCLRSET;
Delay_Us(60);
SCLRCLR;
}
/*******************************************

*******************************************/
void WaitACK(void)
{
uint8 c_ErrTime = 20;

DATAHIG;

Delay_Us(60);
SCLRSET;
Delay_Us(60);
while((1<<3)==(IO0PIN&(1<<3)))
      {
       c_ErrTime--;
       if(!c_ErrTime)I2CStop();
       }
SCLRCLR;
Delay_Us(60);      
}
/*******************************************
读一个字节
*******************************************/
uint8 ReadByte(void)
{
uint8 i;
uint8 ReadData;
DATAIN;
for(i=0;i<8;i++)
    {
     SCLRSET;
     
     if((1<<3)==(IO0PIN&(1<<3)))
       {
        ReadData<<=1;
        ReadData |= 1;
        }
     else
        ReadData<<=1;
           
     SCLRCLR;
     Delay_Us(60);
     }
DATAOUT;   
return ReadData;   
}
/*******************************************
读多个字节
*******************************************/
uint8 ReadData(uint8 ReadAddr)
{
//uint8 i;
uint8 ReadData;

I2CStart();
WriteByte(0xa2);
WriteByte(ReadAddr);
//Delay_Us(1);
I2CStart();
WriteByte(0xa3);
//Delay_Us(1);
ReadData = ReadByte();
WaitACK();
WriteNoACK();

I2CStop();   
return ReadData;
}
/*******************************************
初始化PCF8563
*******************************************/
uint8 PCF8563Init(void)
{
DATAOUT;
SCLRDIR;

WritePCF8563(0x08,0x0a);
WritePCF8563(0x12,0x01);
WritePCF8563(0x12,0x02);
WritePCF8563(0x82,0x0e);
WritePCF8563(0x0a,0x0f);
WritePCF8563(0x00,0x00);
WritePCF8563(0x11,0x01);

return 1;
}

相关帖子

沙发
阿南| | 2012-9-6 18:08 | 只看该作者
中断不能产生,不一定是IIC的问题,也有可能是中断设置的问题。
楼主先思考如何确定读正确。
PCF8563,ZLG应该用测试代码呀。

使用特权

评论回复
板凳
gghhz|  楼主 | 2012-9-7 08:59 | 只看该作者
回复2楼:读出来的时间应该是BCD码,但是现在读出来的不是,应答信号也不知道该怎么处理。
周立功的测试代码用的是I2C的功能块儿,不是IO模拟的I2C。

使用特权

评论回复
地板
阿南| | 2012-9-7 09:47 | 只看该作者
你可以先用测试代码,测试器件和接口本身没有问题,然后再调试你的程序,思路应该会更清晰。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

70

帖子

2

粉丝