最近项目中用到了AD7793读取铂电阻值来得到相应的温度,编写了基于MSP430的硬件驱动程序和模拟驱动程序,并且能成功运行,现在记录一下分享给大家。
AD7793硬件IIC驱动完整版下载链接:https://download.csdn.net/download/qq_42580947/11041933
AD7793模拟IIC驱动完整版下载链接:https://download.csdn.net/download/qq_42580947/11041961
PS:目前资源积分无法自行调整,有谁知道可以修改积分的方法请告知,我想免费分享给大家。
1. AD7793硬件SPI驱动
单片机型号为:MSP430F57XX
硬件SPI驱动失败的几个原因有:单片机配置SPI资源错误(包括SPI的时钟,SPI的运行模型等配置错误),单片机的系统时钟配置错误(注意MCLK, SMCLK, ACLK的分配)
1.1 SPI read with AD7793
/******************** SPI read with AD7793 ***********************/
void AD7793_R(unsigned char addr, unsigned char *buffer, int n)
{
unsigned int index;
unsigned char temp;
AD7793_CS_DIS;
__delay_cycles(200);
AD7793_CS_EN;
// t1>0ns
__delay_cycles(200);
// send address
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = addr;
while(!(UCB0IFG & UCRXIFG));
temp = UCB0RXBUF;
// Delay > 10us, after writing to RAM
Delay10us(2);
// Read data from register and load in buffer[]
for(index=0; index < n; index++)
{
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = 0x00;
while(!(UCB0IFG & UCRXIFG));
buffer[index] = UCB0RXBUF;
Delay10us(100);
}
AD7793_CS_DIS;
}
1.2 SPI write with AD7793
/******************** SPI write with AD7793 ***********************/
void AD7793_W(unsigned char addr, unsigned char *buffer, char n)
{
unsigned char index;
AD7793_CS_EN;
// t1>0ns
__delay_cycles(200);
// send address
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = addr;
// Delay > 10us, after writing to RAM
Delay10us(2);
// Read data from register and load in buffer[]
for(index=0; index < n; index++)
{
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = buffer[index];
}
AD7793_CS_DIS;
}
1.3 AD7793 相关寄存器配置
/******************** AD7793 Configuration ***********************/
void AD7793_CON(void)
{
AD7793_CS_EN;
//single model
SPI_Data[0] = 0x20;
//64k intenal CLK,4.17Hz update rate(stable time 500ms)
SPI_Data[1] = 0x0f;
//Model regeister
AD7793_W(0x08, SPI_Data, 2);
AD7793_CS_DIS;
}
1.4 电压测量
/******************** voltage measurement ***********************/
void V_measure (void)
{
/*** Read the both sides voltage of RTD ***/
//bioplar encoding, 8 times, input range 312.5mV
//external reference voltage, buffer mode, AIN1 input
AD7793_CON();
SPI_Data[0] = 0x03;
SPI_Data[1] = 0x10;
AD7793_W(0x10, SPI_Data, 2);
//IEXC1,IEXC2 connected with IOUT1,current is 210uA
//IO register
SPI_Data[0] = 0x0E;
AD7793_W(0x28, SPI_Data, 1);
//data register
AD7793_R(0x58, SPI_Data, 3);
//get the temperature values
ad_result_T = SPI_Data[0];
ad_result_T <<= 8;
ad_result_T += SPI_Data[1];
ad_result_T <<= 8;
ad_result_T += SPI_Data[2];
ad_result_T &= 0x00ffffff;
ad_result_T -= 8388608;
T =(float)ad_result_T*3.7104E-5;
/*** Read the ambient temperature ***/
//bioplar encoding, 8 times, input range 312.5mV
//external reference voltage, buffer mode, AIN1 input
SPI_Data[0] = 0x00;
SPI_Data[1] = 0x86;
AD7793_W(0x10, SPI_Data, 2);
//data register
AD7793_R(0x58, SPI_Data, 3);
//get the ambient temperature
ad_result_T1 = SPI_Data[0];
ad_result_T1 <<= 8;
ad_result_T1 += SPI_Data[1];
ad_result_T1 <<= 8;
ad_result_T1 += SPI_Data[2];
ad_result_T1 &= 0x00ffffff;
ad_result_T1 -= 8388608;
Tenvir =(float)ad_result_T1*3.7104E-5;
}
2. AD7793模拟SPI驱动
模拟SPI驱动失败的最主要原因是:时钟!时钟!时钟!注意时钟的配置和延时的处理
2.1 AD7793基础子函数的处理
void AD7793_CS_SET()//CS置1
{
P1OUT |= BIT1;
}
void AD7793_CS_CLR()//CS置0
{
P1OUT &=~ BIT1;
}
void AD7793_SCLK_SET()
{
P2OUT |= BIT2;
}
void AD7793_SCLK_CLR()
{
P2OUT &=~ BIT2;
}
void AD7793_DIN_SET()
{
P1OUT |= BIT6;
}
void AD7793_DIN_CLR()
{
P1OUT &=~ BIT6;
}
unsigned int AD7793_DOUT_GET()
{
unsigned int i;
if(P1IN & BIT7)
i = 1;
else
i = 0;
return i;
}
2.2 向AD7793写入一个字节
void WriteToReg(unsigned char ByteData) //向AD7793写入一个字节
{
unsigned char temp;
unsigned char i;
AD7793_CS_CLR();
AD7793_SCLK_SET();
temp=0x80;
for(i=0;i<8;i++)
{
if((temp & ByteData)==0)
{
AD7793_DIN_CLR();
}
else
{
AD7793_DIN_SET();
}
AD7793_SCLK_CLR();
Delay();
AD7793_SCLK_SET();
Delay();
temp=temp>>1;
}
AD7793_CS_SET();
}
2.3 复位AD7793
void AD7793_Reset(void)//AD7793复位
{
P1DIR |= BIT6;
P2DIR |= BIT2;
P1DIR &=~ BIT7;
unsigned int ResetTime;
ResetTime = 32;
AD7793_SCLK_SET();
AD7793_CS_CLR();
AD7793_DIN_SET();
while(ResetTime--)
{
Delay();
AD7793_SCLK_CLR();
Delay();
AD7793_SCLK_SET();
}
AD7793_CS_SET();
}
2.4 读取AD7793
unsigned char AD7793_ReadStatusRegister(void) //读取7793的状态寄存器
{
unsigned char j;
unsigned char temp;
WriteToReg(0x40);
AD7793_DIN_SET();
AD7793_CS_CLR();
temp=0;
for(j=0; j<8; j++)
{
AD7793_SCLK_CLR();
if(AD7793_DOUT_GET()==0)
{
temp=temp<<1;
}
else
{
temp=temp<<1;
temp=temp+0x01;
}
Delay();
AD7793_SCLK_SET();
Delay();
}
AD7793_CS_SET();
return temp;
}
2.5 配置相关寄存器
void Ad7793_WriteModeRegister(unsigned char ModeRegisterH,unsigned char ModeRegisterL)
//配置模式寄存器
{
WriteToReg(0x08);
WriteToReg(ModeRegisterH);
WriteToReg(ModeRegisterL);
}
void Ad7793_WriteConfigRegister(unsigned char ConfigRegisterH,unsigned char ConfigRegisterL)
//配置配置寄存器
{
WriteToReg(0x10);
WriteToReg(ConfigRegisterH);
WriteToReg(ConfigRegisterL);
}
void Ad7793_WriteIORegister(unsigned char IORegister)
//配置IO寄存器
{
WriteToReg(0x28);
WriteToReg(IORegister);
}
2.6 读取AD7793数据寄存器
long AD7793_ReadDataRegister(void)
{
union
{
unsigned char chardata[3];
unsigned long longdata;
}AD7793Result;
unsigned char i,j;
unsigned char temp;
temp=AD7793_ReadStatusRegister();
while((temp&0x80)==0x80)
{
temp=AD7793_ReadStatusRegister();
}
WriteToReg(0x58);
AD7793_DIN_SET();
AD7793_CS_CLR();
for(i=0; i<3; i++)
{
for(j=0; j<8; j++)
{
AD7793_SCLK_CLR();
if(AD7793_DOUT_GET()==0)
{
temp=temp<<1;
}
else
{
temp=temp<<1;
temp=temp+0x01;
}
Delay();
AD7793_SCLK_SET();
Delay();
}
AD7793Result.chardata[3-i]=temp;
}
AD7793_CS_SET();
AD7793Result.longdata=AD7793Result.longdata>>8;
return AD7793Result.longdata;
}
2.7 初始化AD7793
void Init_AD7793(void)//AD7793的初始化
{
AD7793_CS_SET();
AD7793_Reset();
Ad7793_WriteModeRegister(0x00,0x0F);//连续转换
Ad7793_WriteConfigRegister(0x03,0x10);//292.5mv,1.65V偏压
Ad7793_WriteIORegister(0x0E);
}
上一篇:STM8L低功耗HALT模式一些总结
下一篇:MSP430杂谈--delay_cycles的精准延时
推荐阅读最新更新时间:2024-11-03 12:19
设计资源 培训 开发板 精华推荐
- NCP1532GEVB,NCP1532 双路降压 DC-DC 转换器评估板
- LDF 1-A 极低压降固定稳压器的典型应用
- 使用 MaxLinear, Inc 的 SPX29150 的参考设计
- RT8065 3A、2MHz、同步降压转换器的典型应用
- AM2DM-2415SH60-NZ 15 Vout、2W 单路输出 DC-DC 转换器的典型应用
- 【训练营】LED控制-940430A
- LT3027EDD 演示板,双路输出低噪声 LDO 稳压器,Vin1/Vin2=2.3V-20V,Vout1/Vout2=2.5V/3.3V/5V @ 100mA
- 立创EDA客制化87键机械键盘
- LDK120M28R 2.8V低压降稳压器典型应用固定电路
- 具有迟滞功能的 NCV2904DR2G 比较器的典型应用