stm32F429用模拟IIC驱动AD5933测量人体阻抗小阻抗),写入寄存器后读出的都是FF求解
<p>代码:</p><p> </p>
<p> </p>
<p>void SetPointer(char nAddr) // 设置地址指针<br />
{ <br />
int nTemp = 0x1A; // AD5933的默认地址&写控制位(低)<br />
<br />
IIC_Start(); <br />
IIC_Send_Byte(nTemp); // 发送地址 <br />
IIC_Wait_Ack(); // 等待 ACK </p>
<p> IIC_Send_Byte(0xB0); // 发送指针命令1101 0000<br />
IIC_Wait_Ack();</p>
<p> IIC_Send_Byte(nAddr); // 发送地址指针 <br />
IIC_Wait_Ack(); </p>
<p> IIC_Stop();// 停止总线 <br />
return;<br />
}<br />
u16 AD5933_ReadOneByte(u16 ReadAddr)<br />
{ <br />
u16 temp; <br />
SetPointer(ReadAddr);<br />
temp=0x1B;<br />
IIC_Start(); </p>
<p> IIC_Send_Byte(temp); //发送器件地址0XA0,写数据 <br />
IIC_Wait_Ack(); <br />
<br />
temp=IIC_Read_Byte(0);<br />
IIC_Wait_Ack();<br />
<br />
IIC_Stop();//产生一个停止条件 <br />
delay_ms(10); <br />
return temp;<br />
<br />
}</p>
<p>void AD5933_WriteOneByte(u8 WriteAddr,u16 DataToWrite)<br />
{ <br />
int Temp = 0x1A;<br />
IIC_Start(); <br />
IIC_Send_Byte(Temp); //发送器件地址0X1A,写数据 <br />
IIC_Wait_Ack(); <br />
<br />
IIC_Send_Byte(WriteAddr); //发送地址<br />
IIC_Wait_Ack(); <br />
<br />
IIC_Send_Byte(DataToWrite); //发送字节 <br />
IIC_Wait_Ack(); <br />
//printf("IIC_Wait_Ack:%d\n",IIC_Wait_Ack());<br />
IIC_Stop();//产生一个停止条件 <br />
delay_ms(10); <br />
}</p>
<p>void AD5933_Init(void)<br />
{<br />
delay_ms(5);<br />
//写入扫描频率10KHz <br />
AD5933_WriteOneByte(0x82,0x04); //起始频率<br />
delay_ms(5); <br />
AD5933_WriteOneByte(0x83,0xE2); //10kHz<br />
AD5933_WriteOneByte(0x84,0x18); <br />
//写入频率增量0.5Hz<br />
AD5933_WriteOneByte(0x85,0x00); //频率增量<br />
AD5933_WriteOneByte(0x86,0x00); //0.5Hz<br />
AD5933_WriteOneByte(0x87,0x10); <br />
//写入增量2<br />
AD5933_WriteOneByte(0x88,0x00); //增量数<br />
AD5933_WriteOneByte(0x89,0x02); //2点</p>
<p> //写入控制寄存器设置参数<br />
AD5933_WriteOneByte(0x80,0xB1); //将AD5933置于待机模式 ,输出电压范围:典型值2.0Vpp;PGA增益:*1 <br />
AD5933_WriteOneByte(0x81,0x00); //选择内部系统时钟MCLK=16.776MHZ<br />
AD5933_WriteOneByte(0x81,0x10); //Reset<br />
AD5933_WriteOneByte(0x81,0x00); <br />
//初始化起始频率(用开始频率初始化) <br />
<br />
AD5933_WriteOneByte(0x80,0x11); <br />
delay_ms(5);<br />
//建立等待周期数<br />
AD5933_WriteOneByte(0x8A,0x02); //时间循环周期<br />
AD5933_WriteOneByte(0x8B,0x08); //16个周期<br />
delay_ms(5);<br />
//启动频率扫描<br />
AD5933_WriteOneByte(0x80,0x21);<br />
<br />
}</p>
<p>void AD5933GetImpedance(void)<br />
{<br />
//printf("0x82: %x\n",AD5933_ReadOneByte(0x84));<br />
AD5933_Init();<br />
// delay_ms(10);<br />
//AD5933_WriteOneByte(0x89,0x02); <br />
printf("0x89: %x\n",AD5933_ReadOneByte(0x89));<br />
printf("0x82: %x\n",AD5933_ReadOneByte(0x82));<br />
//printf("before:\n",AD5933_ReadOneByte(0x80));<br />
// printf("after: %x\n",AD5933_ReadOneByte(0x80));</p>
<p>IIC用的正点原子的例程:void IIC_Start(void)<br />
{<br />
SDA_OUT(); //sda线输出<br />
IIC_SDA=1; <br />
IIC_SCL=1;<br />
delay_us(4);<br />
IIC_SDA=0;//START:when CLK is high,DATA change form high to low <br />
delay_us(4);<br />
IIC_SCL=0;//钳住I2C总线,准备发送或接收数据 <br />
} <br />
//产生IIC停止信号<br />
void IIC_Stop(void)<br />
{<br />
SDA_OUT();//sda线输出<br />
IIC_SCL=0;<br />
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high<br />
delay_us(4);<br />
IIC_SCL=1; <br />
delay_us(4); <br />
IIC_SDA=1;//发送I2C总线结束信号 <br />
}<br />
//等待应答信号到来<br />
//返回值:1,接收应答失败<br />
// 0,接收应答成功<br />
u8 IIC_Wait_Ack(void)<br />
{<br />
u8 ucErrTime=0;<br />
SDA_IN(); //SDA设置为输入 <br />
IIC_SDA=1;delay_us(1); <br />
IIC_SCL=1;delay_us(1); <br />
while(READ_SDA)<br />
{<br />
ucErrTime++;<br />
if(ucErrTime>250)<br />
{<br />
IIC_Stop();<br />
return 1;<br />
}<br />
}<br />
IIC_SCL=0;//时钟输出0 <br />
return 0; <br />
} <br />
//产生ACK应答<br />
void IIC_Ack(void)<br />
{<br />
IIC_SCL=0;<br />
SDA_OUT();<br />
IIC_SDA=0;<br />
delay_us(2);<br />
IIC_SCL=1;<br />
delay_us(2);<br />
IIC_SCL=0;<br />
}<br />
//不产生ACK应答 <br />
void IIC_NAck(void)<br />
{<br />
IIC_SCL=0;<br />
SDA_OUT();<br />
IIC_SDA=1;<br />
delay_us(2);<br />
IIC_SCL=1;<br />
delay_us(2);<br />
IIC_SCL=0;<br />
} <br />
//IIC发送一个字节<br />
//返回从机有无应答<br />
//1,有应答<br />
//0,无应答 <br />
void IIC_Send_Byte(u16 txd)<br />
{ <br />
u16 t; <br />
SDA_OUT(); <br />
IIC_SCL=0;//拉低时钟开始数据传输<br />
for(t=0;t<8;t++)<br />
{ <br />
IIC_SDA=(txd&0x80)>>7;<br />
txd<<=1; <br />
delay_us(2); //对TEA5767这三个延时都是必须的<br />
IIC_SCL=1;<br />
delay_us(2); <br />
IIC_SCL=0; <br />
delay_us(2);<br />
} <br />
} <br />
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK <br />
u8 IIC_Read_Byte(unsigned char ack)<br />
{<br />
unsigned char i,receive=0;<br />
SDA_IN();//SDA设置为输入<br />
for(i=0;i<8;i++ )<br />
{<br />
IIC_SCL=0; <br />
delay_us(2);<br />
IIC_SCL=1;<br />
receive<<=1;<br />
if(READ_SDA)receive++; <br />
delay_us(1); <br />
} <br />
if (!ack)<br />
IIC_NAck();//发送nACK<br />
else<br />
IIC_Ack(); //发送ACK <br />
return receive;<br />
}</p>
<p>不常在线,大佬如有解决办法求法邮箱254007060@qq.com,小弟感激不禁</p>
已解决!!!!!!!!!!!!!!1 <p>I2C电路 SCK和SDA要有4.7K的上拉电阻。这个必须。最大不能大于10K。</p>
<p>另外就是软件模拟看是否正确。</p>
<p> </p>
<p>希望楼主分享一下解决问题的方法的。让后面的人少入坑。哈哈。</p>
<p>我第一次讀0x94 ~0x97 有值 , 第二次就讀到0xff 請問樓主 如何解決的???</p>
<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]