文章目录
前言一、AFE94X是什么?二、使用步骤1.AFE94X.h2.AFE94X.c
前言
AFE94X多通道同步采集,64KSPS,24bit SD_ADC AFE94X
验证驱动,阻塞等待DRDY(仅验证驱动使用),兼容-类比芯片STM32HAL库SPI
https://blog.csdn.net/hhhhwdnmd/article/details/135774192?spm=1001.2014.3001.5501
一、AFE94X是什么?
AFE94X是多通道同步采样 24 位 Δ-Σ 模数转换器(ADC)系列,内置有可编程增益放大器(PGA)、内部基准以及板载振荡器。AFE94x 包含医疗心电图(ECG)和脑电图(EEG)应用中通
常所需的全部功能。凭借高集成度和出色性能,AFE94x能够以大幅缩小的尺寸、显著降低的功耗和整体成本开发可扩展的医疗仪器系统。
二、使用步骤
1.AFE94X.h
/* Analogysemi xutong 2023/02/15*/#ifndef __AFE94X_H#define __AFE94X_H #include "bsp_lpspi.h"#include "stm32f4xx.h" #define AFE94x_START_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()#define AFE94x_PWDN_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()#define AFE94x_DRDY_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()#define AFE94x_REST_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() /* Definition for AFE94x Pins */#define AFE94x_START_PIN GPIO_PIN_8#define AFE94x_START_GPIO_PORT GPIOA#define AFE94x_DRDY_PIN GPIO_PIN_12#define AFE94x_DRDY_GPIO_PORT GPIOA#define AFE94x_PWDN_PIN GPIO_PIN_11#define AFE94x_PWDN_GPIO_PORT GPIOA#define AFE94x_REST_PIN GPIO_PIN_1#define AFE94x_REST_GPIO_PORT GPIOB /*command define*/ #define AFE94X_WAKEUP 0x02#define AFE94X_STANDBY 0x04#define AFE94X_RESET 0x06#define AFE94X_START 0x08#define AFE94X_STOP 0x0A#define AFE94X_RDATAC 0x10#define AFE94X_SDATAC 0x11#define AFE94X_RDATA 0x12#define AFE94X_OFFSETCAL 0x1A#define AFE94X_RREG 0X20#define AFE94X_WREG 0X40#define AFE94X_RREGX 0xE4#define AFE94X_WREGX 0xE8//RREG Read n nnnn registers starting at address r rrrr //First Byte 001r rrrr (2xh)//Second Byte 000n nnnn //n nnnn = number of registers to be read or written – 1. For example, to read or write three registers, set n nnnn = 0 (0010). r rrrr = the//starting register address for read and write commands//WREG 同上//WREG First Byte (4xh)/* 寄存器地址*/#define AFE94X_ID 0x00#define AFE94X_CONFIG1 0x01#define AFE94X_CONFIG2 0x02#define AFE94X_CONFIG3 0x03#define AFE94X_LOFF 0x04#define AFE94X_CH1SET 0X05#define AFE94X_CH2SET 0X06#define AFE94X_CH3SET 0X07#define AFE94X_CH4SET 0X08#define AFE94X_CH5SET 0X09#define AFE94X_CH6SET 0X0A#define AFE94X_CH7SET 0X0B#define AFE94X_CH8SET 0X0C#define AFE94X_RLD_SENSP 0X0D#define AFE94X_RLD_SENSN 0X0E#define AFE94X_LOFF_SENSP 0X0F#define AFE94X_LOFF_SENSN 0X10#define AFE94X_LOFF_FLIP 0X11#define LOFF_STATP_Status 0X12#define LOFF_STATN_Status 0X13#define AFE94X_GPIO 0X14 #define AFE94X_PACE 0X15#define AFE94X_RESV 0X16#define AFE94X_CONFIG4 0X17#define AFE94X_WCT1 0X18#define AFE94X_WCT2 0X19#define AFE94X_WCT3 0X1A#define AFE94X_CONFIG5 0X1C#define AFE94X_CONFIG6 0X1D#define AFE94X_CONFIG7 0X1E#define AFE94X_CONFIG8 0X1F#define AFE94X_CONFIG9 0X20#define AFE94X_LOFF_CFCG1 0X21#define AFE94X_LOFF_CFCG2 0X22#define AFE94X_LOFF_ACLO1 0X23#define AFE94X_LOFF_ACLO2 0X24#define AFE94X_REDR2P 0X25#define AFE94X_CHMUX_MSB 0X26#define AFE94X_LOFF_CMP_FLIP 0X27#define AFE94X_LOFF_SENSN1 0X28#define AFE94X_LOFF_SENSP1 0X29#define AFE94X_SRB1_COFIG 0X2A#define AFE94X_BIAS_DAC 0X2B#define AFE94X_XOSC_CFG 0X2C#define AFE94X_LOW_NOISE 0X2E#define AFE94X_MISC_ANA 0X34#define AFE94X_MOD_STAT 0X35#define AFE94X_CMD_STAT 0X36#define AFE94X_PGAP_OOR 0X37#define AFE94X_PGAN_OOR 0X28/* 寄存器设置*///CONFIG2#defineEXT_TEST_ON 0X00//关闭内部测试信号,外部(默认)#defineINT_TEST_ON 0X10//CONFIG3#define INT_REF_EN 0X80 //内部基准(default)#define EXT_REF_EN 0X00 //外部基准#define AFE94X_REF_2_4V 0X00 //default#define AFE94X_REF_4V 0X20#define RLD_REF_PD_EN 0X04 //default#define RLD_REF_PD_DIS 0X00#define AFE94X_RLDREF_INT 0X08#define AFE94X_RLDREF_EXT 0X00 //default#define AFE94X_RLD_BUF_EN 0X04#define AFE94X_RLD_BUF_DIS 0X00 //default#define RLD_LOFF_SENS_EN 0X02#define RLD_LOFF_SENS_DIS 0X00 //default #define RLD_STAT_EN 0X00 //default#define RLD_STAT_DIS 0X01//LOFF#define AFE94X_COMP_95_5 0X00#define AFE94X_COMP_92_7 0X20#define AFE94X_COMP_90_8 0X40#define AFE94X_COMP_87_12 0X60#define AFE94X_COMP_85_15 0X80#define AFE94X_COMP_80_20 0XA0#define AFE94X_COMP_75_25 0XC0#define AFE94X_COMP_70_30 0XE0#define LEAD_CURRENT_9NA 0X00#define LEAD_CURRENT_18NA 0X03#define LEAD_CURRENT_27NA 0X04#define LEAD_CURRENT_40NA 0X0C#define FLEAD_AC_LEAD 0X01#define FLEAD_DC_LEAD 0X03//CHXSET#define AFE94X_CH_PD 0X80 //Channel power-down#define AFE94X_CH_EN 0X00 //Normal operation#define AFE94X_PGA_1 0X10#define AFE94X_PGA_2 0X20#define AFE94X_PGA_3 0X30#define AFE94X_PGA_4 0X40#define AFE94X_PGA_6 0X00 //default#define AFE94X_PGA_8 0X40#define AFE94X_PGA_12 0X60#define AFE94X_PGA_24 0X70#defineMUX_Normal_input 0X00//普通电极输入(默认)#defineMUX_Input_shorted 0X01//输入短路#define MUX_RLD_Test 0x02#defineMUX_Test_signal 0X05//测试信号输入#defineMUX_RLD_DRP 0X06#defineMUX_RLD_DRN 0X07#defineMUX_RLD_DRPM 0X00//CHX_MUX_MSB must setCHX_MUX_MSB|MUXn = 1000#defineMUX_RSP_RESP 0X01 //CHX_MUX_MSB must set CHX_MUX_MSB|MUXn = 1001#define MUX_AFE_BYPASS 0X02 //CHX_MUX_MSB must set CHX_MUX_MSB|MUXn = 1010//CONFIG4#define AFE94X_SHOT_MODE 0X20#define AFE94X_CONT_MODE 0X00 //default#define AFE94X_COMP_PDEN 0X02 #define AFE94X_COMP_PDDIS 0X00 //default#define WCT_RLD_CONNECT 0X01 //WCT1#define PD_WCTA_DIS 0X00 //default#define PD_WCTA_EN 0X08#define WCTA_AIN1P_Amp 0X00#define WCTA_AIN1N_Amp 0X01#define WCTA_AIN2P_Amp 0X02#define WCTA_AIN2N_Amp 0X03#define WCTA_AIN3P_Amp 0X04#define WCTA_AIN3N_Amp 0X05#define WCTA_AIN4P_Amp 0X06#define WCTA_AIN4N_Amp 0X07//WCT2#define PD_WCTC_DIS 0X00 //default#define PD_WCTC_EN 0X80#define PD_WCTB_DIS 0X00#define PD_WCTB_EN 0X40 //default#define WCTB_AIN1P_Amp 0X00#define WCTB_AIN1N_Amp 0X08#define WCTB_AIN2P_Amp 0X10#define WCTB_AIN2N_Amp 0X18#define WCTB_AIN3P_Amp 0X20#define WCTB_AIN3N_Amp 0X28#define WCTB_AIN4P_Amp 0X30#define WCTB_AIN4N_Amp 0X38#define WCTC_AIN1P_Amp 0X00#define WCTC_AIN1N_Amp 0X01#define WCTC_AIN2P_Amp 0X02#define WCTC_AIN2N_Amp 0X03#define WCTC_AIN3P_Amp 0X04#define WCTC_AIN3N_Amp 0X05#define WCTC_AIN4P_Amp 0X06#define WCTC_AIN4N_Amp 0X07//WCT3#define WCT_SW7_OFF 0X00 //default#define WCT_SW7_ON 0X80#define WCT_SW6_OFF 0X00 //default#define WCT_SW6_ON 0X40 #define WCT_SW5_OFF 0X00 //default#define WCT_SW5_ON 0X20#define WCT_SW4_OFF 0X00 //default#define WCT_SW4_ON 0X10#define WCT_SW4_EN 0X08#define WCT_SW4_DIS 0X00 //default#define WCT_BUF_LOW 0X04 //default#define WCT_BUF_HIGH 0X00#define WCT_BYPASS_BUF 0X01//CONFIG5#define INT_OSC_2M 0X00 //default Internal OSC frequency 2M#define INT_OSC_1M 0X40 //Internal OSC frequency 1M#define INT_OSC_0_5M 0X80 //Internal OSC frequency 512K#define INT_OSC_0_2M 0XC0 //Internal OSC frequency 216K#define AFE94X_HS_MODE 0X02#define AFE94X_LP_MODE 0X01//L0FF_CGF2#define RLD_COMP_95_5 0X00 //default#define RLD_COMP_92_7 0X04#define RLD_COMP_90_8 0X08#define RLD_COMP_87_12 0X0C#define RLD_COMP_85_15 0X10#define RLD_COMP_80_20 0X14#define RLD_COMP_75_25 0X18#define RLD_COMP_70_30 0X38#define LEAD_Debounce_DIS 0X00 //default#define LEDA_Debounce_117 0x01//CHMUX_MSB#define CH8_MUX_MSB 0X80#define CH7_MUX_MSB 0X40#define CH6_MUX_MSB 0X20#define CH5_MUX_MSB 0X10#define CH4_MUX_MSB 0X08#define CH3_MUX_MSB 0X04#define CH2_MUX_MSB 0X02#define CH1_MUX_MSB 0X01//GPIO#define GPIOD_GPIO1 0X10#define GPIOD_GPIO2 0X20#define GPIOD_GPIO3 0X40#define GPIOD_GPIO4 0X80#define GPIO_IN_GPIO1 0X01#define GPIO_IN_GPIO2 0X02#define GPIO_IN_GPIO3 0X04#define GPIO_IN_GPIO4 0X08#define GPIO_OUT_GPIO1 0X00#define GPIO_OUT_GPIO2 0X00#define GPIO_OUT_GPIO3 0X00#define GPIO_OUT_GPIO4 0X00//LOFF_CFG1#define RLD_COMP_EN#define CUR_LEVEL//RLD_DAC#define RLD_DAC_EN 0X80#define RLD_DAC_DIS 0X00#define RLD_REF_AVDD 0X00#define RLD_REF_AREEP 0X40/* 通道选择 channel select*/#define AFE94X_AIN1 0#define AFE94X_AIN2 1#define AFE94X_AIN3 2#define AFE94X_AIN4 3#define AFE94X_AIN5 4#define AFE94X_AIN6 5#define AFE94X_AIN7 6#define AFE94X_AIN8 7//写命令void AFE9XX_Command(uint8_t Com);//获取芯片的ID号uint8_t AFE9XX_GetChipID(void); //获取寄存器值uint8_t AFE9XX_GetReg(uint8_t Rreg);//获取多个寄存器值void AFE9XX_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);//写入寄存器值void AFE9XX_WriteReg(uint8_t Wreg,uint8_t Data);//写多个寄存器的值void AFE9XX_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);//获取所有寄存器值void AFE9XX_GetAllReg(void);//设定通道并且获取ADC的值// CH设置为 0或者设通道0/1void AFE9XX_GetADC_Value(uint8_t AIN_P,uint8_t AIN_N,uint8_t *Rxdata);//初始化AFE9XXvoid AFE9XX_Init(void);//获取通道ADC值float AFE9xx_GetValue(uint8_t AIN_CH,uint8_t AFE94X_REF,uint8_t *Rxdata);//获取所有通道ADC值void AFE94X_GetAllValue(uint8_t AFE94X_REF,uint8_t *Rxdata,float *Voltage);//获取内部温度传感器值float AFE9XX_GetTempture(uint8_t *Rxdata);//获取设备噪声值float AFE9XX_Noise_Test(uint8_t *Rxdata);//电源测试float AFE9XX_PWR_Test(uint8_t *Rxdata);//直流导联脱落void AFE9XX_DCLEAD_OFF(uint8_t *Rxdata);//内部信号测试void AFE9XX_SINGLE_TEST(uint8_t *Rxdata);void TEST_RLD_SINGLE(uint8_t *Rxdata);#endif
2.AFE94X.c
代码如下(示例):
#include "AFE94X.h"//初始化AF94Xvoid AFE9XX_Init(void){ //拉PWDN和RSTGPIO_InitTypeDef GPIO_Initure; AFE94x_START_GPIO_CLK_ENABLE(); //开启START时钟AFE94x_PWDN_GPIO_CLK_ENABLE(); //开启PWDN时钟AFE94x_DRDY_GPIO_CLK_ENABLE(); //开启DRDY时钟AFE94x_REST_GPIO_CLK_ENABLE(); //开启REST时钟 GPIO_Initure.Pin=AFE94x_DRDY_PIN; //DRDY GPIO_Initure.Mode=GPIO_MODE_INPUT; GPIO_Initure.Pull=GPIO_PULLUP; GPIO_Initure.Speed=GPIO_SPEED_HIGH; HAL_GPIO_Init(AFE94x_DRDY_GPIO_PORT,&GPIO_Initure);GPIO_Initure.Pin=AFE94x_PWDN_PIN; //PWDN GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; GPIO_Initure.Pull=GPIO_PULLUP; GPIO_Initure.Speed=GPIO_SPEED_HIGH; HAL_GPIO_Init(AFE94x_PWDN_GPIO_PORT,&GPIO_Initure);GPIO_Initure.Pin=AFE94x_START_PIN; //START GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; GPIO_Initure.Pull=GPIO_PULLUP; GPIO_Initure.Speed=GPIO_SPEED_HIGH; HAL_GPIO_Init(AFE94x_START_GPIO_PORT,&GPIO_Initure); GPIO_Initure.Pin=AFE94x_REST_PIN; //REST GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; GPIO_Initure.Pull=GPIO_PULLUP; GPIO_Initure.Speed=GPIO_SPEED_HIGH; HAL_GPIO_Init(AFE94x_REST_GPIO_PORT,&GPIO_Initure);HAL_GPIO_WritePin(AFE94x_PWDN_GPIO_PORT,AFE94x_PWDN_PIN,1); //拉高PWDNHAL_GPIO_WritePin(AFE94x_REST_GPIO_PORT,AFE94x_REST_PIN,0); //拉低RESTHAL_Delay(1);HAL_GPIO_WritePin(AFE94x_REST_GPIO_PORT,AFE94x_REST_PIN,1); //拉低RESTHAL_GPIO_WritePin(AFE94x_START_GPIO_PORT,AFE94x_START_PIN,0);//STARTAFE9XX_Command(AFE94X_RESET);}//获取芯片的ID号uint8_t AFE9XX_GetChipID(void){ uint8_t ID; ID=AFE9XX_GetReg(AFE94X_ID); return ID;}//获取芯片寄存器值uint8_t AFE9XX_GetReg(uint8_t Rreg){ uint8_t TxData[3]; uint8_t RxData[3]; uint8_t Reg_Val=0;//寄存器地址小于0X1F if(Rreg <= 0X1F) {//读取命令,选择要读取的寄存器TxData[0]=AFE94X_RREG|Rreg;//读取单个寄存器TxData[1]=0x00;Lpspi_Read(3,TxData,RxData);//返回寄存器值 } //寄存器地址大于0X1F else { //获取CONFIG8寄存器ValueReg_Val=AFE9XX_GetReg(AFE94X_CONFIG8);//写入CONFIG8寄存器地址偏移命令,TxData[0]=AFE94X_WREG|AFE94X_CONFIG8;//选择要写入的的寄存器TxData[1]=0x00;//读取写入寄存器TxData[2]=Reg_Val|0x80; //SET ADDR_OFFSETLpspi_Write(3,TxData); //读取命令,TxData[0]=AFE94X_RREG|Rreg;//选择要读取的寄存器TxData[1]=0x00;//开始读取Lpspi_Read(3,TxData,RxData);//返回寄存器值 //清除CONFIG8寄存器地址偏移位,TxData[0]=AFE94X_WREG|AFE94X_CONFIG8;//选择要写入的的寄存器TxData[1]=0x00;//读取写入寄存器TxData[2]=Reg_Val&0X7F; //Clear ADDR_OFFSETLpspi_Write(3,TxData); } return RxData[2];}//写单个芯片寄存器值void AFE9XX_WriteReg(uint8_t Wreg,uint8_t Data){ uint8_t TxData[3];uint8_t Reg_Val=0;//寄存器地址小于0X1Fif(Wreg<=0x1F){//写入命令,TxData[0]=AFE94X_WREG|Wreg;//选择要写入的的寄存器TxData[1]=0x00;//读取写入寄存器TxData[2]=Data;Lpspi_Write(3,TxData);}//寄存器地址大于0X1Felse{//获取CONFIG8寄存器ValueReg_Val=AFE9XX_GetReg(AFE94X_CONFIG8);//写入CONFIG8寄存器地址偏移命令,TxData[0]=AFE94X_WREG|AFE94X_CONFIG8;//选择要写入的的寄存器TxData[1]=0x00;//读取写入寄存器TxData[2]=Reg_Val|0x80; //SET ADDR_OFFSETLpspi_Write(3,TxData);//写入命令,TxData[0]=AFE94X_WREG|Wreg;//选择要写入的的寄存器TxData[1]=0x00;//读取写入寄存器TxData[2]=Data;Lpspi_Write(3,TxData);//清除MISC_ANA寄存器地址偏移位,TxData[0]=AFE94X_WREG|AFE94X_CONFIG8;//选择要写入的的寄存器TxData[1]=0x00;//读取写入寄存器TxData[2]=Reg_Val&0X7F; //Clear ADDR_OFFSETLpspi_Write(3,TxData);} }//写多个芯片寄存器值//StartReg 起始寄存器地址//RegNum 要写的寄存器数量//*Data 寄存器参数传入void AFE9XX_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data){ uint8_t TxData[52]; uint8_t i; //写入命令, TxData[0]=AFE94X_WREGX;TxData[1]=StartReg; //选择要写入的的寄存器 TxData[2]=RegNum-1; for(i=1;i<=RegNum;i++) { //将指针数据赋值给Txdata TxData[2+i]=*Data; //指向下个数据地址 Data++; } //最终发送多少个数据 Lpspi_Write(3+RegNum,TxData);}//读多个芯片寄存器值//StartReg 起始寄存器地址//RegNum 要写的寄存器数量//*Data 寄存器参数传出void AFE9XX_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data){ //读取寄存器 uint8_t TxData[24]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //读取命令, TxData[0]=AFE94X_RREGX;TxData[1]=StartReg; //选择要写入的的寄存器 TxData[2]=RegNum-1; //最终接收多少个数据 Lpspi_Read(3+RegNum,TxData,Data);}//发送AFE9XX 数据指令 如SDATAC或者RDATAC//详情见头文件command define部分void AFE9XX_Command(uint8_t Com){ uint8_t TxData[1];TxData[0]=Com; Lpspi_Write(1,TxData);}//测量固有噪声//返回噪声值float AFE9XX_Noise_Test(uint8_t *Rxdata){//set->pga=1 and Input shorted (for offset or noise measurements) uint8_t Chset[]={0X11,0X11,0X11,0X11,0X11,0X11,0X11,0X11,0X11,0X11};uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};uint32_t AD_Value=0;float Voltage;//停止连续读数据AFE9XX_Command(AFE94X_SDATAC);//PGA_Gain set 1AFE9XX_WriteMultiReg(AFE94X_CH1SET,8,Chset);//singel shot modeAFE9XX_WriteReg(AFE94X_CONFIG4,AFE94X_SHOT_MODE);//SET 250SPSAFE9XX_WriteReg(AFE94X_CONFIG1,0x06);AFE9XX_WriteReg(AFE94X_CONFIG2,0x00);//设置内部基准默认基准2.4VAFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN);//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);//select single Channel ValueAD_Value = (Rxdata[3]<<16|Rxdata[4]<<8|Rxdata[5]);//MSB=1->采集数据为负数if((AD_Value>>16)&0X80) {AD_Value = ~AD_Value;AD_Value = AD_Value&0X7FFFFF;}Voltage = (AD_Value*(2.4/8388607))*1000;return Voltage;}//测量内部测试信号1mv(VREF->2.4V)void AFE9XX_SINGLE_TEST(uint8_t *Rxdata){//Select Test sigleuint8_t Chset[]={0X05,0X05,0015,0X05,0X05,0X05,0X05,0X05,0X05,0X05};uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//停止连续数据传输AFE9XX_Command(AFE94X_SDATAC);AFE9XX_WriteMultiReg(AFE94X_CH1SET,8,Chset);AFE9XX_WriteReg(AFE94X_CONFIG2,0x10);//设置内部基准AFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN|AFE94X_REF_2_4V);//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);}//电源测量float AFE9XX_PWR_Test(uint8_t *Rxdata){//SET PGA=1 AND uint8_t CHSET[]={0X13,0X13,0X13,0X13,0X13,0X13,0X13,0X13};uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};uint32_t AD_Value=0;float Voltage;//停止连续读数据AFE9XX_Command(AFE94X_SDATAC);//PGA_Gain set 1AFE9XX_WriteMultiReg(AFE94X_CH1SET,8,CHSET);AFE9XX_WriteReg(AFE94X_CONFIG4,AFE94X_SHOT_MODE);AFE9XX_WriteReg(AFE94X_CONFIG1,0x06);AFE9XX_WriteReg(AFE94X_CONFIG2,0x00);//设置内部基准4VAFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN|AFE94X_REF_4V);//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);//select single Channel ValueAD_Value = (Rxdata[3]<<16|Rxdata[4]<<8|Rxdata[5]);if((AD_Value>>16)&0X80) //MSB=1->采集数据为负数{AD_Value = ~AD_Value;AD_Value = AD_Value&0X7FFFFF;}Voltage = (AD_Value*(4.0/8388607))*1000;return Voltage;}void AFE9XX_DCLEAD_OFF(uint8_t *Rxdata){//Select loff channeluint8_t PGA_Gain[]={0X10,0X10,0X10,0X10,0X10,0X10,0X10,0X10};uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//停止连续数据传输AFE9XX_Command(AFE94X_SDATAC);//使能直流导联脱落,设置比较阈值AFE9XX_WriteReg(AFE94X_LOFF,AFE94X_COMP_70_30|FLEAD_DC_LEAD);//内部比较器上电AFE9XX_WriteReg(AFE94X_CONFIG4,AFE94X_COMP_PDEN);//选择使能导联脱离通道AFE9XX_WriteReg(AFE94X_LOFF_SENSN,0XFF);AFE9XX_WriteReg(AFE94X_LOFF_SENSP,0XFF);//设置内部基准AFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN|AFE94X_REF_4V);//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);}//读温度传感器//返回温度值℃float AFE9XX_GetTempture(uint8_t *Rxdata){uint8_t PGA_Gain[]={0X14,0X14,0X14,0X14,0X14,0X14,0X14,0X14};uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};uint32_t AD_Value=0;float Temp_Value;//停止连续读数据AFE9XX_Command(AFE94X_SDATAC);//PGA_Gain set 1AFE9XX_WriteMultiReg(AFE94X_CH1SET,8,PGA_Gain);AFE9XX_WriteReg(AFE94X_CONFIG4,AFE94X_SHOT_MODE);AFE9XX_WriteReg(AFE94X_CONFIG1,0x06);AFE9XX_WriteReg(AFE94X_CONFIG2,0x00);//设置内部基准AFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN|AFE94X_REF_2_4V);//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);//select single Channel ValueAD_Value = (Rxdata[3]<<16|Rxdata[4]<<8|Rxdata[5]);if((AD_Value>>16)&0X80) //MSB=1->采集数据为负数{AD_Value = ~AD_Value;AD_Value = AD_Value&0X7FFFFF;}Temp_Value = (((AD_Value*(2.4/8388607))*1000000-143200)/481)+25;return Temp_Value;}//读单个ADC通道电压值(PGA=1)//AIN_Channel 要读ADC通道//AFE94X_REF 内部基准选择//返回单通道ADC电压值(mv)float AFE9xx_GetValue(uint8_t AIN_CH,uint8_t AFE94X_REF,uint8_t *Rxdata){uint8_t PGA_Gain[]={0X10,0X10,0X10,0X10,0X10,0X10,0X10,0X10};uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};uint32_t AD_Value=0;float Voltage;//停止连续读数据AFE9XX_Command(AFE94X_SDATAC);//PGA_Gain set 1AFE9XX_WriteMultiReg(AFE94X_CH1SET,8,PGA_Gain);AFE9XX_WriteReg(AFE94X_CONFIG4,AFE94X_SHOT_MODE);AFE9XX_WriteReg(AFE94X_CONFIG1,0x06);AFE9XX_WriteReg(AFE94X_CONFIG2,0x00);//设置内部基准4VAFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN|AFE94X_REF);//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);//select single Channel ValueAD_Value = (Rxdata[3+(AIN_CH*3)]<<16|Rxdata[4+(AIN_CH*3)]<<8|Rxdata[5+(AIN_CH*3)]);//MSB=1->采集数据为负数if((AD_Value>>16)&0X80) {AD_Value = ~AD_Value;AD_Value = AD_Value&0X7FFFFF;}//内部基准选择2.4Vif(AFE94X_REF==AFE94X_REF_2_4V){Voltage = (AD_Value*(2.4/8388607))*1000;}//内部基准选择4Velse if(AFE94X_REF==AFE94X_REF_4V){Voltage = (AD_Value*(4.0/8388607))*1000;}//外部基准else if(AFE94X_REF == EXT_REF_EN){//Voltage = (AD_Value*(LSB)*1000;}return Voltage;}//读多通道ADC电压值(PGA=1)//AFE94X_REF 基准选择// * AFE94X_REF_2_4V // * AFE94X_REF_4V // * AFE94X_EXT_REF//Voltage 返回多通道ADC电压值(mv)void AFE94X_GetAllValue(uint8_t AFE94X_REF,uint8_t *Rxdata,float *Voltage){uint8_t PGA_Gain[]={0X10,0X10,0X10,0X10,0X10,0X10,0X10,0X10};uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};uint32_t AD_Value=0;//停止连续读数据AFE9XX_Command(AFE94X_SDATAC);//PGA_Gain set 1,MUX Normal inputAFE9XX_WriteMultiReg(AFE94X_CH1SET,8,PGA_Gain);//单次模式AFE9XX_WriteReg(AFE94X_CONFIG4,AFE94X_SHOT_MODE);//设置采样率AFE9XX_WriteReg(AFE94X_CONFIG1,0x06);//设置内部基准AFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN|AFE94X_REF);//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);for(int i=0;i<8;i++){AD_Value = (Rxdata[3+(i*3)]<<16|Rxdata[4+(i*3)]<<8|Rxdata[5+(i*3)]); //MSB=1->采集数据为负数if((AD_Value>>16)&0X80) {AD_Value = ~AD_Value;AD_Value = AD_Value&0X7FFFFF;}//内部基准选择2.4Vif(AFE94X_REF==AFE94X_REF_2_4V){Voltage[i] = (AD_Value*(2.4/8388607))*1000;}//内部基准选择4Velse if(AFE94X_REF==AFE94X_REF_4V){Voltage[i] = (AD_Value*(4.0/8388607))*1000;}//外部基准else if(AFE94X_REF==EXT_REF_EN){//Voltage[i] = (AD_Value*(LSB)*1000;}}}//右腿驱动void TEST_RLD_SINGLE(uint8_t *Rxdata){uint8_t Txdata[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};uint32_t AD_Value=0;//停止连续读数据AFE9XX_Command(AFE94X_SDATAC);//channel 1-3 PGA_Gain set 1,MUX Normal inputAFE9XX_WriteReg(AFE94X_CH1SET,AFE94X_PGA_1|MUX_Normal_input);AFE9XX_WriteReg(AFE94X_CH2SET,AFE94X_PGA_1|MUX_Normal_input);AFE9XX_WriteReg(AFE94X_CH3SET,AFE94X_PGA_1|MUX_Normal_input);//channel 1-3 connect RLDAFE9XX_WriteReg(AFE94X_RLD_SENSP,0X07);AFE9XX_WriteReg(AFE94X_RLD_SENSN,0X07);AFE9XX_WriteReg(AFE94X_CONFIG3,0X1C);//set channel 4-5AFE9XX_WriteReg(AFE94X_CH4SET,AFE94X_CH_PD|AFE94X_PGA_1|MUX_RLD_DRN);AFE9XX_WriteReg(AFE94X_CH5SET,AFE94X_CH_PD|AFE94X_PGA_1|MUX_RLD_Test);//单次模式AFE9XX_WriteReg(AFE94X_CONFIG4,AFE94X_SHOT_MODE);//设置采样率AFE9XX_WriteReg(AFE94X_CONFIG1,0x06);//设置内部基准AFE9XX_WriteReg(AFE94X_CONFIG3,INT_REF_EN|AFE94X_REF_2_4V );//启动转换AFE9XX_Command(AFE94X_START);//连续读数据AFE9XX_Command(AFE94X_RDATAC);//等待DRDYwhile(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));Lpspi_Read(27,Txdata,Rxdata);}