xuyuntangs

文章数:240 被阅读:410258

账号入驻

类比半导体ADX122驱动

最新更新时间:2023-12-25
    阅读数:
#ifndef __ADX122_H#define __ADX122_H//#include "stm32h7xx_hal.h"//#include "stm32g0xx_hal.h"#include "stm32f1xx.h"#include "bsp_lpspi.h"//#include "SPI.h"//#include "systick.h"
#define ADX122_ENABLE Lpspi_Start() //PULL DOWM CS START ADC#define ADX122_DISABLE Lpspi_Stop() //PULL UP CS STOP ADC
// Register Value#define ADX122_SS_START 0X8000 //Start ADC#define ADX122_NO_EFECT 0X0000 //NO Start ADC
#define ADX122_MUX_AIN0_AIN1 0X0000 //000 = AINP is AIN0 and AINN is AIN1 (default)#define ADX122_MUX_AIN0_AIN3 0x1000 //001 = AINP is AIN0 and AINN is AIN3#define ADX122_MUX_AIN1_AIN3 0X2000 //010 = AINP is AIN1 and AINN is AIN3#define ADX122_MUX_AIN2_AIN3 0X3000 //011 = AINP is AIN2 and AINN is AIN3#define ADX122_MUX_AIN0 0X4000 //100 = AINP is AIN0 and AINN is GND#define ADX122_MUX_AIN1 0X5000 //101 = AINP is AIN1 and AINN is GND#define ADX122_MUX_AIN2 0X6000 //110 = AINP is AIN2 and AINN is GND#define ADX122_MUX_AIN3 0X7000 //111 = AINP is AIN3 and AINN is GND
#define ADX122_PGA_6144 0X0000 //000 = FSR is ±6.144 V#define ADX122_PGA_4096 0X0200 //001 = FSR is ±4.096 V#define ADX122_PGA_2048 0X0400 //010 = FSR is ±2.048 V (default)#define ADX122_PGA_1024 0X0600 //011 = FSR is ±1.024 V#define ADX122_PGA_0512 0X0800 //100 = FSR is ±0.512 V#define ADX122_PGA_0256 0X0A00 //101 = FSR is ±0.256 V
#define ADX122_Continuous_MODE 0X0000 //0->Continuous#define ADX122_Sigle_SHOT_MODE 0X0100 //1->SIGNEL ADC
#define ADX122_DR_10SPS 0X0000 //000 = 10 SPS#define ADX122_DR_20SPS 0X0020 //001 = 20 SPS#define ADX122_DR_40SPS 0X0040 //010 = 40 SPS#define ADX122_DR_158SPS 0X0060 //011 = 158 SPS#define ADX122_DR_316SPS 0X0080 //100 = 316 SPS #define ADX122_DR_581SPS 0X00A0 //101 = 581 SPS#define ADX122_DR_1761SPS 0X00C0 //110 = 1761 SPS#define ADX122_DR_3571SPS 0X00E0 //111 = 3571 SPS
#define ADX122_PUUP_DIS 0X0000 //inside pullup disabled#define ADX122_PUUP_EN 0x0008 //inside pullup enabled
#define ADX122_NOP_UPDATA 0X0002 //update the Config register#define ADX122_NOP_UNUPDATA 0X0003 //do not update the Config register
#define ADX122_EN_FILT_50HZ 0x8000 //Enable FILT_50HZ#define ADX122_EN_FILT_60HZ 0X4000 //Enable FILT_60HZ#define ADX122_DIS_FILT 0X0000 //Disable FILT
#define ADX122_SDCS_Off 0X0000 //Sensor Current OFF#define ADX122_SDCS_0_5ua 0X0010 //Sensor Current 0.5ua#define ADX122_SDCS_2ua 0X0020 //Sensor Current 2ua#define ADX122_SDCS_10ua 0x0030 //Sensor Current 10ua
#define ADX122_In_Short_Mux 0x0000 //Short input mode control Follow CONFIG REGISTER#define ADX122_In_Short 0X0004 //Ainp and Ainn are shorted
uint8_t ADX122_Get_ID(void);void ADX122_Sigle_SHOT_Init();void ADX122_ADC_Continuous_Init(uint16_t channel,uint16_t PGA,uint16_t SPS,uint16_t FILT,uint16_t SDCS);float ADX122_Get_ADC_SIGLE_SHOT_Data(uint16_t channel,uint16_t PGA,uint16_t SPS,uint16_t FILT,uint16_t SDCS);float ADX122_Get_ADC_Continuous_Data(uint16_t channel,uint16_t PGA,uint16_t SPS,uint16_t FILT,uint16_t SDCS);#endif

以上是头文件

#include "adx122.h"//ADX122 supply voltage 2 - 5.5V//analog input voltage max VDD+0.3V.    VIN//(VDD-->3v3,PGA-->4096,)无法获得满量程ADC输出代码,这种意味着一些动态范围的丢失./*************************************************************************************************** * 函 数 名 称:ADX122_ADC_Continuous_Init * 函 数 说 明:初始化ADX122 * 函 数 形 参:channel = ADC外部通道选择,PGA = ADC满量程选择,SPS = ADC数据传输率选择 * 函 数 返 回:无 * 作       者:gugugu * 备       注:连续模式使用前,调用此函数完成初始化,之后调用ADX122_Get_ADC_Continuous_Data()连续读取数据***************************************************************************************************/void ADX122_ADC_Continuous_Init(uint16_t channel,uint16_t PGA,uint16_t SPS,uint16_t FILT,uint16_t SDCS){  //MySPI_Init();  uint32_t Txdata;  LPSPI_Init();//初始化SPI  ADX122_ENABLE;//拉低CS开始通信  Txdata = (ADX122_SS_START|channel|PGA|ADX122_Continuous_MODE|SPS|ADX122_PUUP_EN|ADX122_NOP_UPDATA);//16BIT SET CONFIG REGISTER  Txdata = (Txdata<<4)|((FILT|SDCS)>>12);//(16BIT << 4)|CONFIG_EXTRA = 20BIT  Lpspi_Write(1,(uint8_t)(Txdata>>12));//发送20位数据高8位数据  Lpspi_Write(1,(uint8_t)(Txdata>>4));//发送20位中8位数据  Lpspi_Write(1,(uint8_t)(Txdata<<4));//发送20位低4位数据  Spi_delay(30);  Lpspi_Stop();//拉高CS复位ADX122结束通信  Spi_delay(30);}/*************************************************************************************************** * 函 数 名 称:ADX122_ADC_Continuous_Init * 函 数 说 明:初始化ADX122 * 函 数 形 参:channel = ADC外部通道选择,PGA = ADC满量程选择,SPS = ADC数据传输率选择 * 函 数 返 回:无 * 作       者:gugugu * 备       注:连续模式使用前,调用此函数完成初始化,之后调用ADX122_Get_ADC_Continuous_Data()连续读取数据***************************************************************************************************/void ADX122_Sigle_SHOT_Init(){  LPSPI_Init();//初始化SPI}/****************************************************************** * 函 数 名 称:ADX122_Get_ADC_SIGLE_SHOT_Data * 函 数 说 明:单次获取AD电压值(mv) * 函 数 形 参:channel = ADC外部通道选择,PGA = ADC满量程选择,SPS = ADC数据传输率选择 * 函 数 返 回:AD电压值(mv) * 作       者:gugugu * 备       注:******************************************************************/float ADX122_Get_ADC_SIGLE_SHOT_Data(uint16_t channel,uint16_t PGA,uint16_t SPS,uint16_t FILT,uint16_t SDCS){  uint8_t Adc_Data_H=0;  uint8_t Adc_Data_M=0;  uint8_t Adc_Data_L=0;  uint32_t Adc_Data=0;  uint32_t TXdata=0;  float Tempture_data=0;  ADX122_ENABLE;//拉低CS开始通信  Spi_delay(30);  TXdata = (ADX122_SS_START|channel|PGA|ADX122_Sigle_SHOT_MODE|SPS|ADX122_PUUP_EN|ADX122_NOP_UPDATA);//16BIT SET CONFIG REGISTER  TXdata = (TXdata<<4)|((FILT|SDCS)>>12);//(16BIT << 4)|CONFIG_EXTRA = 20BIT  Lpspi_Write(1,(uint8_t)(TXdata>>12));//发送20位数据高8位数据  Lpspi_Write(1,(uint8_t)(TXdata>>4));//发送20位中8位数据  Lpspi_Write(1,(uint8_t)(TXdata<<4));//发送20位低4位数据  Spi_delay(30);  ADX122_DISABLE;//拉高CS复位ADX122结束通信  Spi_delay(30);  ADX122_ENABLE;//拉低CS开始通信  Spi_delay(30);  //while(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6));//DOUT拉低ADC1118数据更新准备完毕    while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6));//DOUT拉低ADC1118数据更新准备完毕    TXdata = (ADX122_SS_START|channel|PGA|ADX122_Sigle_SHOT_MODE|SPS|ADX122_PUUP_EN|ADX122_NOP_UPDATA);//16BIT SET CONFIG REGISTER  TXdata = (TXdata<<4)|((FILT|SDCS)>>12);//(16BIT << 4)|(CONFIG_EXTRA>>12) = 20BIT   Adc_Data_H = Lpspi_Read(1,(uint8_t)(TXdata>>12));  Adc_Data_M = Lpspi_Read(1,(uint8_t)(TXdata>>4));  Adc_Data_L= Lpspi_Read(1,(uint8_t)(TXdata<<4));  Spi_delay(30);    ADX122_DISABLE;//拉高CS复位ADX122结束通信  Adc_Data = (((Adc_Data_H<<16)|(Adc_Data_M<<8)|(Adc_Data_M))>>4);//读24bit数据转换20位adc数据  if(Adc_Data&0X80000)//MSB=1-->负数需要转换  {    Adc_Data = (~Adc_Data);    Adc_Data = Adc_Data&0x7ffff;    if(PGA == ADX122_PGA_6144)Tempture_data=Adc_Data*0.01171875;    else if(PGA == ADX122_PGA_4096)Tempture_data=Adc_Data*0.0078125;    else if(PGA == ADX122_PGA_2048)Tempture_data=Adc_Data*0.000390625;    else if(PGA == ADX122_PGA_1024)Tempture_data=Adc_Data*0.0001953125;    else if(PGA == ADX122_PGA_0512)Tempture_data=Adc_Data*0.00009765625;    else Tempture_data=Adc_Data*0.000048828125;    return -(Tempture_data);  }  else  {    if(PGA == ADX122_PGA_6144)Tempture_data=Adc_Data*0.01171875;    else if(PGA == ADX122_PGA_4096)Tempture_data=Adc_Data*0.0078125;    else if(PGA == ADX122_PGA_2048)Tempture_data=Adc_Data*0.00390625;    else if(PGA == ADX122_PGA_1024)Tempture_data=Adc_Data*0.0001953125;    else if(PGA == ADX122_PGA_0512)Tempture_data=Adc_Data*0.00009765625;    else Tempture_data=Adc_Data*0.000048828125;    return Tempture_data;    }    }/*************************************************************************************** * 函 数 名 称:ADX122_Get_ADC_Continuous_Data * 函 数 说 明:连续获取AD电压值 * 函 数 形 参:channel = ADC外部通道选择,PGA = ADC满量程选择,SPS = ADC数据传输率选择 * 函 数 返 回:AD电压值(mv) * 作       者:gugugu * 备       注:使用此函数前调用ADX122_ADC_Continuous_Init()****************************************************************************************/float  ADX122_Get_ADC_Continuous_Data(uint16_t channel,uint16_t PGA,uint16_t SPS,uint16_t FILT,uint16_t SDCS){  uint8_t Adc_Data_H=0;  uint8_t Adc_Data_M=0;  uint8_t Adc_Data_L=0;  uint32_t Adc_Data=0;  uint32_t TXdata=0;  float Tempture_data;    ADX122_ENABLE;//拉低CS开始通信  Spi_delay(3);  while(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6));//DOUT拉低ADC1118数据更新准备完毕    while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6));//DOUT拉低ADC1118数据更新准备完毕    TXdata = (ADX122_SS_START|channel|PGA|ADX122_Continuous_MODE|SPS|ADX122_PUUP_EN|ADX122_NOP_UNUPDATA);//16BIT SET CONFIG REGISTER  TXdata = (TXdata<<4)|((FILT|SDCS)>>12);//(16BIT << 4)|(CONFIG_EXTRA>>12) = 20BIT   Adc_Data_H = Lpspi_Read(1,(uint8_t)(TXdata>>12));  Adc_Data_M = Lpspi_Read(1,(uint8_t)(TXdata>>4));  Adc_Data_L= Lpspi_Read(1,(uint8_t)(TXdata<<4));    Spi_delay(30);    ADX122_DISABLE;//拉高CS复位ADX122结束通信  Adc_Data = (((Adc_Data_H<<16)|(Adc_Data_M<<8)|(Adc_Data_M))>>4);//读24bit数据转换20位adc数据  if(Adc_Data&0X80000)//MSB=1-->负数需要转换  {    Adc_Data = (~Adc_Data);    Adc_Data = Adc_Data&0x7ffff;    if(PGA == ADX122_PGA_6144)Tempture_data=Adc_Data*0.01171875;    else if(PGA == ADX122_PGA_4096)Tempture_data=Adc_Data*0.0078125;    else if(PGA == ADX122_PGA_2048)Tempture_data=Adc_Data*0.000390625;    else if(PGA == ADX122_PGA_1024)Tempture_data=Adc_Data*0.0001953125;    else if(PGA == ADX122_PGA_0512)Tempture_data=Adc_Data*0.00009765625;    else Tempture_data=Adc_Data*0.000048828125;    return -(Tempture_data);  }  else  {    if(PGA == ADX122_PGA_6144)Tempture_data=Adc_Data*0.01171875;    else if(PGA == ADX122_PGA_4096)Tempture_data=Adc_Data*0.0078125;    else if(PGA == ADX122_PGA_2048)Tempture_data=Adc_Data*0.00390625;    else if(PGA == ADX122_PGA_1024)Tempture_data=Adc_Data*0.0001953125;    else if(PGA == ADX122_PGA_0512)Tempture_data=Adc_Data*0.00009765625;    else Tempture_data=Adc_Data*0.000048828125;    return Tempture_data;   }    } /*************************************************************************************** * 函 数 名 称:ADX122_Get_ID * 函 数 说 明:读取ADX122ID,测试通信 * 函 数 形 参:无 * 函 数 返 回:ADX122_Device_ID * 作       者:gugugu * 备       注:80位数据传输,Device_ID_Register-->16bit,[15-8]-->Device_ID_Value****************************************************************************************/uint8_t ADX122_Get_ID(void){  uint16_t Txdata_1;//CONFIG DATA  uint16_t Txdata_2;//CONFIG_EXTRA DATA  uint8_t Adx_122_ID=0;     ADX122_ENABLE;//拉低CS开始通信    Spi_delay(30);    Txdata_1 = (ADX122_SS_START|ADX122_MUX_AIN0|ADX122_PGA_6144|ADX122_Sigle_SHOT_MODE|ADX122_DR_10SPS|ADX122_PUUP_EN|ADX122_NOP_UPDATA);  Txdata_2 = (ADX122_EN_FILT_50HZ|ADX122_SDCS_Off|ADX122_In_Short_Mux);//16bit  Lpspi_Write(1,(uint8_t)(Txdata_1>>8));//send 36 bit  Lpspi_Write(1,(uint8_t)Txdata_1);  Lpspi_Write(1,(uint8_t)(Txdata_2>>8));  Lpspi_Write(1,(uint8_t)Txdata_2);  for(int i=0;i<6;i++)//DIN拉低  {    Lpspi_Write(1,(uint8_t)0x00);  }  //Spi_delay(30);  //ADX122_DISABLE;//拉高CS复位ADX122结束通信  //Spi_delay(30);  //ADX122_ENABLE;//拉低CS开始通信  //Spi_delay(30);  while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6));//DOUT拉低ADX122数据更新准备完毕  Lpspi_Write(1,(uint8_t)(Txdata_1>>8));//send 36 bit  Lpspi_Write(1,(uint8_t)Txdata_1);  Lpspi_Write(1,(uint8_t)(Txdata_2>>8));  Lpspi_Write(1,(uint8_t)Txdata_2);  for(int i=0;i<6;i++)//DIN拉低  {    if(i<4)    {      Lpspi_Write(1,(uint8_t)0x00);    }    else if(i==4)    {            Adx_122_ID = Lpspi_Read(1,(uint8_t)0x00);      Lpspi_Write(1,(uint8_t)0x00);    }    }  Spi_delay(30);  ADX122_DISABLE;//拉高CS复位ADX122结束通信  return Adx_122_ID;}

以上.c文件

芯片设计,产品主要面向工业,通讯,医疗,汽车等市场。 让我们用“芯”点亮未来,期待您的加入!">


 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: TI培训

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved