标题: 求助 DS1302读出来的数据乱码 [打印本页]

作者: nivans    时间: 2015-9-1 12:30
标题: 求助 DS1302读出来的数据乱码
/*---------------------------------------------
/* DS1302 Read Byte
/*-------------------------------------------*/
uint8 ReadByte()
{
uint8 i,tmp = 0x00;

for(i = 0; i < 8 ; i ++)
  {  
   
    if(io) tmp|=0x80;
    sclk = 1 ;
    tmp >>= 1;
   sclk = 0;
  }
  return tmp;
  
}
/*---------------------------------------------
  DS1302 WriteByte()
--------------------------------------------*/
void WriteByte(uint8 dat)
{
uint8 i;

   for(i = 0; i < 8 ; i ++)
   {
   io = (bit)(dat & 0x01);
    dat >>= 1;
    sclk = 1;
   sclk = 0;
   }
   
}
/*---------------------------------------------
/* DS1302 SingleRead
/*-------------------------------------------*/
uint8 SingleRead(uint8 addr)
{
   uint8 tmp;

  cs = 1;
   _nop_();
   WriteByte(addr);
   tmp = ReadByte();
   cs = 0;
return tmp;
}
/*----------------------------------------------
/* DS1302 SingleWrite
/---------------------------------------------*/
void SingleWrite(uint8 addr,uint8 dat)
{
  cs = 1;
   WriteByte(addr);
   WriteByte(dat);
   cs = 0;
}

SingleRead函数写入的的地址没问题,读出的数据是错的。
请高手帮忙看一下,哪里错了?

作者: WYY1032829355    时间: 2016-2-17 14:27
同学,你的ReadByte()函数有问题,循环8次,所以右移8次,出问题。比如数据1000  0000,然后读取最高位时候tem=1000 0000,然后右移一位了,此时变成了0100  0000 了




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1