用MSP430控制ad7708的程序void AD7708WriteRegister(char addr,long dat)
{
SpiWriteData(addr); //写通信寄存器,通知下个操作:写addr寄存器
if(IsLong[addr]) //如果是16位寄存器, 7718则24位若移植要改if内语句
{
SpiWriteData(dat>>8);
}
SpiWriteData(0xFF&dat); //写入低位数据
}
char IsLong[16] = {0,0,0,0,1,1,1,0,0,0,0,0,1,1,0,0};
long AD7708ReadRegister(char addr)
{
char h = 0,l = 0; //高低字节数据
SpiWriteData(0x40|addr); //写通信寄存器,通知下个操作:读addr寄存器
if(IsLong[addr])
{
h = SpiWriteData(0xFF);
}
l = SpiWriteData(0xFF);
return ((unsigned int)h<<8)|l;
}
long AD7708ReadResultData()
{
while((AD7708ReadRegister(0x00)&0x80)==0); //等待转换完成
return AD7708ReadRegister(0x04);
}
void AD7708Cal(char channel)
{
adccon = (adccon&0x0f)|(channel<<4);
mode = (mode&0xf8)|0x04; //内部0校准
AD7708WriteRegister(0x02,adccon); //ADC控制寄存器,channel通道
AD7708WriteRegister(0x01,mode); //模式寄存器
while((AD7708ReadRegister(0x01)&0x07)!=0x01); //等待校准完成
mode = (mode&0xf8)|0x05; //内部 满标度校准
AD7708WriteRegister(0x01,mode); //模式寄存器
while((AD7708ReadRegister(0x01)&0x07)!=0x01); //等待校准完成
}
void AD7708Init(char chop)
{
//主机模式,115200,8位数据位,三线模式,时钟模式1(具体见spi.c)
SpiMasterInit(115200,8,3,1); //时钟不是准确的115200(具体见spi.c)
_EINT(); //开中断,spi读写程序要需要中断
char filter;
adccon = 0x0f;
if(chop == 0)
{
filter = 0x03; //滤波寄存器设为最小值,可以更改
mode = 0x91; //斩波禁止,10通道,无缓冲,空闲模式
}
else
{
filter = 0x0D; //滤波寄存器设为最小值,可以更改
mode = 0x11; //斩波启用,10通道,无缓冲,空闲模式
}
AD7708WriteRegister(0x07,0x00); //IO寄存器,不用==
AD7708WriteRegister(0x03,filter); //滤波寄存器
AD7708WriteRegister(0x02,0x0F); //ADC控制寄存器,0通道,单极性
AD7708WriteRegister(0x01,mode); //模式寄存器
if(chop == 0)
for(int i = 0; i<5;i++)
{
//校准,因只有5个失调寄存器,多的就会覆盖之前的,只校准5个即可
AD7708Cal(5);
}
_DINT();
}
void AD7708Start(char channel)
{
adccon = (adccon&0x0f)|(channel<<4);
mode = (mode&0xf8)|0x02;
AD7708WriteRegister(0x02,adccon);
AD7708WriteRegister(0x01,mode);
}
long a;
void main()
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
ClkInit();
AD7708Init(0); //禁止斩波 1时启用斩波
_EINT(); //开中断,程序需要用到SPI的中断;冲突,可以更改SPI函数
while(1) //串口测试
{
AD7708Start(0);
a = AD7708ReadResultData(); //读取AD采样后的结果
//电压计算方法:a*1.024*2.5(参考电压)/65535
a = AD7708ReadRegister(0); //去状态值,此处函数不需要用
}
}
|