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