完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近项目中用到了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);}
|
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
1324 浏览 0 评论
2095 浏览 0 评论
7926 浏览 0 评论
写了一个用DMA读取ADC数据的程序,记录下整个过程和一点心得
3247 浏览 0 评论
3515 浏览 0 评论
1288浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 23:05 , Processed in 0.613426 second(s), Total 71, Slave 52 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号