麻辣鸡丶 发表于 2020-5-21 15:20

stm32F429用模拟IIC驱动AD5933测量人体阻抗小阻抗),写入寄存器后读出的都是FF求解

<p>代码:</p>

<p>&nbsp;</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;</p>

<p>void SetPointer(char nAddr) &nbsp;// &nbsp; 设置地址指针<br />
{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;int nTemp = 0x1A; &nbsp; &nbsp; &nbsp;// AD5933的默认地址&amp;写控制位(低)<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp; IIC_Start(); &nbsp;<br />
&nbsp;&nbsp; &nbsp;IIC_Send_Byte(nTemp); &nbsp; &nbsp; // 发送地址&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;IIC_Wait_Ack(); &nbsp; &nbsp; // 等待 ACK&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</p>

<p>&nbsp;&nbsp; &nbsp;IIC_Send_Byte(0xB0); &nbsp; &nbsp; // 发送指针命令1101 0000<br />
&nbsp;&nbsp; &nbsp;IIC_Wait_Ack();</p>

<p>&nbsp;&nbsp; &nbsp;IIC_Send_Byte(nAddr);&nbsp;&nbsp; &nbsp;// 发送地址指针&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;IIC_Wait_Ack();&nbsp;&nbsp; &nbsp;</p>

<p>&nbsp; IIC_Stop();// 停止总线&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return;<br />
}<br />
u16 AD5933_ReadOneByte(u16 ReadAddr)<br />
{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;u16 temp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;SetPointer(ReadAddr);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;temp=0x1B;<br />
&nbsp; &nbsp; IIC_Start(); &nbsp;</p>

<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Send_Byte(temp); &nbsp; //发送器件地址0XA0,写数据 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Wait_Ack();&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;temp=IIC_Read_Byte(0);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Wait_Ack();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp; IIC_Stop();//产生一个停止条件&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay_ms(10);&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return temp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
}</p>

<p>void AD5933_WriteOneByte(u8 WriteAddr,u16 DataToWrite)<br />
{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int Temp = 0x1A;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Start(); &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Send_Byte(Temp); &nbsp; //发送器件地址0X1A,写数据 &nbsp;&nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Wait_Ack();&nbsp;&nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Send_Byte(WriteAddr); &nbsp; //发送地址<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Wait_Ack();&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Send_Byte(DataToWrite); &nbsp; &nbsp; //发送字节&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Wait_Ack(); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//printf(&quot;IIC_Wait_Ack:%d\n&quot;,IIC_Wait_Ack());<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Stop();//产生一个停止条件&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay_ms(10);&nbsp;&nbsp; &nbsp;<br />
}</p>

<p>void AD5933_Init(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;delay_ms(5);<br />
&nbsp;&nbsp; &nbsp;//写入扫描频率10KHz&nbsp;<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x82,0x04); &nbsp; //起始频率<br />
&nbsp;&nbsp; &nbsp;delay_ms(5);&nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x83,0xE2); &nbsp; //10kHz<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x84,0x18);&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;//写入频率增量0.5Hz<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x85,0x00); &nbsp; //频率增量<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x86,0x00); &nbsp; //0.5Hz<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x87,0x10);&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;//写入增量2<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x88,0x00); &nbsp; //增量数<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x89,0x02); &nbsp; //2点</p>

<p>&nbsp;&nbsp; &nbsp;//写入控制寄存器设置参数<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x80,0xB1); &nbsp; //将AD5933置于待机模式&nbsp; ,输出电压范围:典型值2.0Vpp;PGA增益:*1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x81,0x00); &nbsp; //选择内部系统时钟MCLK=16.776MHZ<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x81,0x10); &nbsp; //Reset<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x81,0x00); &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;//初始化起始频率(用开始频率初始化)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x80,0x11);&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;delay_ms(5);<br />
&nbsp;&nbsp; &nbsp;//建立等待周期数<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x8A,0x02); &nbsp; //时间循环周期<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x8B,0x08); &nbsp; //16个周期<br />
&nbsp;&nbsp; &nbsp;delay_ms(5);<br />
&nbsp; //启动频率扫描<br />
&nbsp;&nbsp; &nbsp;AD5933_WriteOneByte(0x80,0x21);<br />
&nbsp;&nbsp; &nbsp;<br />
}</p>

<p>void AD5933GetImpedance(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//printf(&quot;0x82: %x\n&quot;,AD5933_ReadOneByte(0x84));<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;AD5933_Init();<br />
&nbsp;&nbsp; &nbsp;//&nbsp;&nbsp; &nbsp;delay_ms(10);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//AD5933_WriteOneByte(0x89,0x02); &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf(&quot;0x89: %x\n&quot;,AD5933_ReadOneByte(0x89));<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf(&quot;0x82: %x\n&quot;,AD5933_ReadOneByte(0x82));<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//printf(&quot;before:\n&quot;,AD5933_ReadOneByte(0x80));<br />
&nbsp; &nbsp; &nbsp; &nbsp;//&nbsp;printf(&quot;after: %x\n&quot;,AD5933_ReadOneByte(0x80));</p>

<p>IIC用的正点原子的例程:void IIC_Start(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;SDA_OUT(); &nbsp; &nbsp; //sda线输出<br />
&nbsp;&nbsp; &nbsp;IIC_SDA=1;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=1;<br />
&nbsp;&nbsp; &nbsp;delay_us(4);<br />
&nbsp;&nbsp;&nbsp; &nbsp;IIC_SDA=0;//START:when CLK is high,DATA change form high to low&nbsp;<br />
&nbsp;&nbsp; &nbsp;delay_us(4);<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=0;//钳住I2C总线,准备发送或接收数据&nbsp;<br />
}&nbsp;&nbsp; &nbsp; &nbsp;<br />
//产生IIC停止信号<br />
void IIC_Stop(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;SDA_OUT();//sda线输出<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=0;<br />
&nbsp;&nbsp; &nbsp;IIC_SDA=0;//STOP:when CLK is high DATA change form low to high<br />
&nbsp;&nbsp;&nbsp; &nbsp;delay_us(4);<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=1;&nbsp;<br />
&nbsp;&nbsp; &nbsp;delay_us(4);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;IIC_SDA=1;//发送I2C总线结束信号&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<br />
}<br />
//等待应答信号到来<br />
//返回值:1,接收应答失败<br />
// &nbsp; &nbsp; &nbsp; &nbsp;0,接收应答成功<br />
u8 IIC_Wait_Ack(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;u8 ucErrTime=0;<br />
&nbsp;&nbsp; &nbsp;SDA_IN(); &nbsp; &nbsp; &nbsp;//SDA设置为输入 &nbsp;<br />
&nbsp;&nbsp; &nbsp;IIC_SDA=1;delay_us(1);&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=1;delay_us(1);&nbsp;&nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;while(READ_SDA)<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ucErrTime++;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(ucErrTime&gt;250)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_Stop();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return 1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=0;//时钟输出0 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;return 0; &nbsp;<br />
}&nbsp;<br />
//产生ACK应答<br />
void IIC_Ack(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=0;<br />
&nbsp;&nbsp; &nbsp;SDA_OUT();<br />
&nbsp;&nbsp; &nbsp;IIC_SDA=0;<br />
&nbsp;&nbsp; &nbsp;delay_us(2);<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=1;<br />
&nbsp;&nbsp; &nbsp;delay_us(2);<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=0;<br />
}<br />
//不产生ACK应答&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br />
void IIC_NAck(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=0;<br />
&nbsp;&nbsp; &nbsp;SDA_OUT();<br />
&nbsp;&nbsp; &nbsp;IIC_SDA=1;<br />
&nbsp;&nbsp; &nbsp;delay_us(2);<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=1;<br />
&nbsp;&nbsp; &nbsp;delay_us(2);<br />
&nbsp;&nbsp; &nbsp;IIC_SCL=0;<br />
}&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
//IIC发送一个字节<br />
//返回从机有无应答<br />
//1,有应答<br />
//0,无应答&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<br />
void IIC_Send_Byte(u16 txd)<br />
{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; u16 t; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;SDA_OUT(); &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; IIC_SCL=0;//拉低时钟开始数据传输<br />
&nbsp; &nbsp; for(t=0;t&lt;8;t++)<br />
&nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; IIC_SDA=(txd&amp;0x80)&gt;&gt;7;<br />
&nbsp; &nbsp; &nbsp; &nbsp; txd&lt;&lt;=1; &nbsp;&nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay_us(2); &nbsp; //对TEA5767这三个延时都是必须的<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_SCL=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay_us(2);&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_SCL=0;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay_us(2);<br />
&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp;&nbsp;<br />
} &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br />
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK &nbsp;&nbsp;<br />
u8 IIC_Read_Byte(unsigned char ack)<br />
{<br />
&nbsp;&nbsp; &nbsp;unsigned char i,receive=0;<br />
&nbsp;&nbsp; &nbsp;SDA_IN();//SDA设置为输入<br />
&nbsp; &nbsp; for(i=0;i&lt;8;i++ )<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp; IIC_SCL=0;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; delay_us(2);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IIC_SCL=1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; receive&lt;&lt;=1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; if(READ_SDA)receive++; &nbsp;&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay_us(1);&nbsp;<br />
&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<br />
&nbsp; &nbsp; if (!ack)<br />
&nbsp; &nbsp; &nbsp; &nbsp; IIC_NAck();//发送nACK<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; IIC_Ack(); //发送ACK &nbsp;&nbsp;<br />
&nbsp; &nbsp; return receive;<br />
}</p>

<p>不常在线,大佬如有解决办法求法邮箱254007060@qq.com,小弟感激不禁</p>

麻辣鸡丶 发表于 2020-5-25 14:04

已解决!!!!!!!!!!!!!!1

damiaa 发表于 2020-6-16 09:24

<p>I2C电路 SCK和SDA要有4.7K的上拉电阻。这个必须。最大不能大于10K。</p>

<p>另外就是软件模拟看是否正确。</p>

<p>&nbsp;</p>

<p>希望楼主分享一下解决问题的方法的。让后面的人少入坑。哈哈。</p>

阿宏 发表于 2020-7-1 15:09

<p>我第一次讀0x94 ~0x97 有值 , 第二次就讀到0xff&nbsp; 請問樓主 如何解決的???</p>

超级小人 发表于 2021-3-15 17:17

<p>你好,我最近在用AD5933做测量人体阻抗,不过我是用52单片机,<img height="28" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/dizzy-face_1f635.png" width="28" />可以参考一下</p>

<p>驱动AD5933测量人体阻抗的全部代码吗</p>
页: [1]
查看完整版本: stm32F429用模拟IIC驱动AD5933测量人体阻抗小阻抗),写入寄存器后读出的都是FF求解