IIC读取16位的数据寄存器

2019-07-21 02:31发布

请各位大侠指点:

这些天一直在用mini Stm32平台来读取另外一块板子的AD值,但最后发现:代码能够正确读写那块板子的控制寄存器(control registers),但不能正确读取相应的数据寄存器的值。

备注:控制寄存器为8位,数据寄存器为16位。在读取数据寄存器的过程中发现:数据寄存器的低八位是有值的,而且是可以随外部信号的变化而变化,但高八位的值一直是0,不管外部信号如何变化。 这意味着数据寄存器允许的最大值只有255,与16位所设定的范围差了很多很多呢。

不知道哪位大侠能就上述问题指点迷津:有可能哪里有问题,从哪些方面来着手排查。非常感谢!






友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
7条回答
chenchxli
2019-07-22 01:38
就是连续两次读呢。但第二次读到数都是0。

代码如下,里面用到的start stop ack nack 均是直接用的原子哥的myiic.c呢 。下面的代码没层次来着,不好意思哈,还不知道怎样贴有层次的代码呢
u16 AB4_ReadLenByte(u8 ReadAddr,u8 Len)
{
u8 t;
u16 temp=0;
u16 temp2=0;

IIC_Start();
IIC_Send_Byte(0XE8);    //发送写命令
if(IIC_Wait_Ack())
{
printf(" 读数据时,器件寻址失败 ");
delay_ms(1000);
}       
IIC_Send_Byte(ReadAddr);
if(IIC_Wait_Ack())
{
printf(" 读数据时,数据地址寻址失败 ");
delay_ms(1000);
}     
IIC_Start();
IIC_Send_Byte(0XE9);
if(IIC_Wait_Ack()) //waiting for the repsonse from slave, in order to confirm that the writing is OK
{
printf(" 读数据时,器件寻址失败 ");
delay_ms(1000);
}


for(t=0;t<Len-1;t++)
{
// ack=1时,发送ACK,ack=0,发送nACK 
temp|=IIC_Read_Byte(1);
temp><<=8;
delay_ms(50);
}

delay_ms(1000);

temp|=IIC_Read_Byte(0);


IIC_Stop();//产生一个停止条件
delay_ms(50);  
temp2=0;
for(t=0;t<Len;t++)
{
temp2=temp2<<8;
temp2|=((temp>>(8*t))&0Xff);
printf(" at the No %dth transformation,OUT0(half word)=%d ",t,temp2);


}
printf(" after transformation,OUT0(15...8,7...0)=%d ",temp2);

  
return temp2;     
}

一周热门 更多>