AD5933的SCL引脚接STC12C5A60S2的p1.5引脚,AD5933的SDA引脚接STC12C5A60S2的p1.6引脚,编写代码使得stc单片机读取到AD5933的阻抗参数
时间: 2024-05-06 18:17:35 浏览: 145
以下是STC12C5A60S2和AD5933的连接示意图:
```
STC12C5A60S2 AD5933
+------------+ +----------+
| | | |
| | | |
| P1.6---|<------------|SDA |
| | | |
| P1.5---|<------------|SCL |
| | | |
| | | |
+------------+ +----------+
```
下面是STC12C5A60S2的代码,使用I2C协议读取AD5933的阻抗参数:
```c
#include<reg52.h>
sbit SDA=P1^6; //定义IIC总线的SDA引脚
sbit SCL=P1^5; //定义IIC总线的SCL引脚
void Delay_us(unsigned char us)
{
unsigned char i;
while(us--)
{
for(i=0;i<12;i++);
}
}
void IIC_Start()
{
SDA=1;
SCL=1;
Delay_us(2);
SDA=0;
Delay_us(2);
SCL=0;
}
void IIC_Stop()
{
SDA=0;
SCL=1;
Delay_us(2);
SDA=1;
Delay_us(2);
}
void IIC_SendByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SDA=dat&0x80;
dat<<=1;
SCL=1;
Delay_us(2);
SCL=0;
Delay_us(2);
}
SDA=1;
SCL=1;
Delay_us(2);
SCL=0;
}
unsigned char IIC_ReadByte(unsigned char ack)
{
unsigned char i,dat=0;
SDA=1;
for(i=0;i<8;i++)
{
SCL=1;
Delay_us(2);
dat<<=1;
dat|=SDA;
SCL=0;
Delay_us(2);
}
if(ack)
SDA=0;
else
SDA=1;
SCL=1;
Delay_us(2);
SCL=0;
SDA=1;
return dat;
}
void AD5933_WriteReg(unsigned char reg,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x0d<<1);
IIC_SendByte(reg);
IIC_SendByte(dat);
IIC_Stop();
}
unsigned char AD5933_ReadReg(unsigned char reg)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0x0d<<1);
IIC_SendByte(reg);
IIC_Start();
IIC_SendByte((0x0d<<1)|0x01);
dat=IIC_ReadByte(0);
IIC_Stop();
return dat;
}
void AD5933_Init()
{
unsigned char i;
AD5933_WriteReg(0x80,0x10); //复位芯片
for(i=0;i<10;i++); //等待复位完成
AD5933_WriteReg(0x80,0x00); //退出复位状态
AD5933_WriteReg(0x81,0x00); //设置增益为1
AD5933_WriteReg(0x82,0x00); //设置输出电流为100ua
AD5933_WriteReg(0x85,0x00); //设置输出时钟为外部时钟
AD5933_WriteReg(0x86,0x00); //设置外部时钟为12.5MHZ
AD5933_WriteReg(0x87,0x10); //设置增益控制为增益因子1
AD5933_WriteReg(0x88,0x01); //设置增益控制为增益因子1
}
void AD5933_StartSweep(unsigned long startFreq,unsigned long incFreq,unsigned char numInc)
{
unsigned long freq=startFreq;
AD5933_WriteReg(0x90,numInc); //设置增量频率的段数
AD5933_WriteReg(0x92,freq&0xff); //设置起始频率的低8位
AD5933_WriteReg(0x93,(freq>>8)&0xff); //设置起始频率的高8位
AD5933_WriteReg(0x94,(freq>>16)&0xff); //设置起始频率的最高8位
AD5933_WriteReg(0x95,incFreq&0xff); //设置增量频率的低8位
AD5933_WriteReg(0x96,(incFreq>>8)&0xff); //设置增量频率的高8位
AD5933_WriteReg(0x97,(incFreq>>16)&0xff); //设置增量频率的最高8位
AD5933_WriteReg(0x98,0x01); //设置输出波形为正弦波
AD5933_WriteReg(0x8f,0x01); //启动频率扫描
while(AD5933_ReadReg(0x8f)&0x02); //等待扫描完成
}
unsigned long AD5933_ReadReal(unsigned char reg)
{
unsigned long dat;
dat=AD5933_ReadReg(reg+2);
dat=(dat<<8)|AD5933_ReadReg(reg+1);
dat=(dat<<8)|AD5933_ReadReg(reg);
return dat;
}
void main()
{
unsigned long real,imag;
AD5933_Init();
AD5933_StartSweep(100000,10000,10);
real=AD5933_ReadReal(0x94);
imag=AD5933_ReadReal(0x98);
while(1);
}
```
上述代码中,`Delay_us()`函数用于延时一定时间,以等待I2C总线上电平变化稳定;`IIC_Start()`和`IIC_Stop()`函数分别实现I2C总线的起始和停止信号;`IIC_SendByte()`函数用于发送一个字节的数据;`IIC_ReadByte()`函数用于读取一个字节的数据;`AD5933_WriteReg()`函数用于向AD5933的寄存器写入一个字节的数据;`AD5933_ReadReg()`函数用于从AD5933的寄存器读取一个字节的数据;`AD5933_Init()`函数用于初始化AD5933的寄存器;`AD5933_StartSweep()`函数用于启动AD5933的频率扫描;`AD5933_ReadReal()`函数用于从AD5933的实部寄存器或虚部寄存器读取32位的数据;`main()`函数中启动频率扫描后,读取AD5933的实部和虚部数据。
阅读全文