硬件SPIADS8684ADS8688驱动程序说明
使⽤3个ADS8688芯⽚,21个AD通道,ADS8688采⽤菊花链式连接⽅式,具体电路参考ADS8688数据⼿册。变量及宏定义
/*******************************引脚定义************************************/
#define WRITE_REG(REG, VAL)  ((REG) = (VAL))
#define READ_REG(REG)        ((REG))
#define ADS8688_CS_1    WRITE_REG(GPIOA->BSRR, GPIO_Pin_4)
#define ADS8688_CS_0    WRITE_REG(GPIOA->BRR, GPIO_Pin_4)
/**************************************ADS8688寄存器********************************************/
//与命令寄存器相关的命令
#define NO_OP  0x0000
#define STDBY  0x8200
#define PWR_DN  0x8300
#define RST  0x8500
#define AUTO_RST 0xA000
#define MAN_Ch_0 0xC000
#define MAN_CH_1 0xC400
#define MAN_Ch_2 0xC800
#define MAN_Ch_3 0xCC00
#define MAN_Ch_4 0xD000
#define MAN_Ch_5 0xD400
#define MAN_Ch_6 0xD800
#define MAN_Ch_7 0xDC00
#define MAN_AUX  0xE000
//与程序寄存器相关的命令
#define WRITE  1
#define READ  0
#define AUTO_SEQ_EN    (0x01<<1)
#define Channel_Power_Down  (0x02<<1)
#define Feature_Select  (0x03<<1)
#define CH7_EN  0x80
#define CH6_EN  0x40
#define CH5_EN  0x20
#define CH4_EN  0x10
#define CH3_EN  0x08
#define CH2_EN  0x04
#define CH1_EN  0x02
#define CH0_EN  0x01
#define CH7_PD  0x80
#define CH6_PD  0x40
#define CH5_PD  0x20
#define CH4_PD  0x10
#define CH3_PD  0x08
#define CH2_PD  0x04
#define CH1_PD  0x02
#define CH0_PD  0x01
#define Channel_0_Input_Range  (0x05<<1)
#define Channel_1_Input_Range  (0x06<<1)
#define Channel_2_Input_Range  (0x07<<1)
#define Channel_2_Input_Range  (0x07<<1)
#define Channel_3_Input_Range  (0x08<<1)
#define Channel_4_Input_Range  (0x09<<1)
#define Channel_5_Input_Range  (0x0A<<1)
#define Channel_6_Input_Range  (0x0B<<1)
#define Channel_7_Input_Range  (0x0C<<1)
#define VREF_B_25    0x00  //通道输⼊范围:±2.5×VREF
#define VREF_B_125    0x01  //通道输⼊范围:±1.25×VREF
#define VREF_B_0625    0x02  //通道输⼊范围:±0.625×VREF
#define VREF_U_0_25    0x05  //通道输⼊范围:0-2.5×VREF
#define VREF_U_0_125  0x06  //通道输⼊范围:0-1.25×VREF
#define Ch_0_Hysteresis    (0x15<<1)
#define Ch_0_High_Threshold_MSB  (0x16<<1)
#define Ch_0_High_Threshold_LSB  (0x17<<1)
#define Ch_0_Low_Threshold_MSB  (0x18<<1)
#define Ch_0_Low_Threshold_LSB  (0x19<<1)
#define Ch_7_Hysteresis    (0x38<<1)
#define Ch_7_High_Threshold_MSB  (0x39<<1)
#define Ch_7_High_Threshold_LSB  (0x3A<<1)
#define Ch_7_Low_Threshold_MSB  (0x3B<<1)
#define Ch_7_Low_Threshold_LSB  (0x3C<<1)
#define Command_Read_Back  (0x3F<<1)
/*******************************变量声明************************************/
ADS8688_EXT u16 Transfer_Data[21];  //存储3个 ADC 21个通道的输出数据
#define ADC_BUF_1    Transfer_Data[17]
#define ADC_BUF_2    Transfer_Data[14]
#define ADC_BUF_3    Transfer_Data[11]
#define ADC_BUF_4    Transfer_Data[8]
#define ADC_BUF_5    Transfer_Data[5]
#define ADC_BUF_6    Transfer_Data[2]
#define ADC_BUF_7    Transfer_Data[20]
#define ADC_BUF_8    Transfer_Data[16]
#define ADC_BUF_9    Transfer_Data[13]
#define ADC_BUF_10  Transfer_Data[10]
#define ADC_BUF_11  Transfer_Data[7]
#define ADC_BUF_12  Transfer_Data[4]
#define ADC_BUF_13  Transfer_Data[1]
#define ADC_BUF_14  Transfer_Data[19]
#define ADC_BUF_15  Transfer_Data[15]
#define ADC_BUF_16  Transfer_Data[12]
#define ADC_BUF_17  Transfer_Data[9]
#define ADC_BUF_18  Transfer_Data[6]
#define ADC_BUF_19  Transfer_Data[3]
#define ADC_BUF_20  Transfer_Data[0]
#define ADC_BUF_21  Transfer_Data[18]
/*******************************函数声明************************************/
void SPI_GPIO_Init(void);
u16 ADS8688_WriteCmdReg(u16 cmd);
u16 ADS8688_WriteProgramRegister(u16 Addr, u16 data);
//void ADS8688_ReadProgramRegister(u8 Addr);
void ADS8688_Init(void);
void Get_AUTO_RST_Mode_Data(u8 chn);
函数功能
/*===============================================================
函数名称:SPI_Init
函数功能:SPI初始化
输⼊参数:⽆
返回值:⽆
================================================================*/
void SPI_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
/
/ NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,  ENABLE);  //SPI2时钟使能
SPI_Cmd(SPI1, DISABLE);  //失能SPI外设
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//双线双向全双⼯ SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;//16位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//时钟悬空低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//第⼆个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//内部NSS信号由SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//预分频值为4 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7;//⽤于CRC值计算的多项式
SPI_Init(SPI1,&SPI_InitStructure);//初始化配置
SPI_Cmd(SPI1, ENABLE);  //使能SPI外设
}
/*===============================================================
函数名称:ADS8688_WriteCmdReg
函数功能:写ADS8688命令寄存器
输⼊参数:cmd:写⼊的命令
返回值:⽆
================================================================*/
u16 ADS8688_WriteCmdReg(u16 cmd)
{
ADS8688_CS_0;
while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);//判断接收数据缓存
SPI1->DR = cmd;
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
ADS8688_CS_1;
return SPI1->DR;
}
/*===============================================================
函数名称:ADS8688_WriteProgramRegister
write的返回值函数功能:向程序寄存器中写⼊地址和数据
输⼊参数:Addr:地址位:BIT[15:9]、读写位:BIT8,
data:数据位:BIT[7:0]
返回值:⽆
================================================================*/
u16 ADS8688_WriteProgramRegister(u16 Addr, u16 data)
{
ADS8688_CS_0;
ADS8688_CS_0;
while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);//判断接收数据缓存
SPI1->DR = (((Addr| WRITE)<<8)|data);
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
ADS8688_CS_1;
return SPI1->DR;
}
///*===============================================================
//函数名称:ADS8688_ReadProgramRegister
//函数功能:读取程序寄存器中的数据
//输⼊参数:Addr:地址位:BIT[15:9]、读写位:BIT8
//返回值:data:读取的8位数据
//================================================================*/
//void ADS8688_ReadProgramRegister(u8 Addr)
//{
// u16 i;
/
/ SPI1->DR = (Addr<<8);
// while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
// i = SPI1->DR;
// SPI1->DR = 0;
// while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
// Rxh[0] = SPI1->DR;
// SPI1->DR = 0;
// while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
// Rxh[1] = SPI1->DR;
// SPI1->DR = 0;
// while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
/
/ Rxh[2] = SPI1->DR;
//}
/*===============================================================
函数名称:ADS8688_Init
函数功能:ADS8688初始化
输⼊参数:⽆
返回值:⽆
================================================================*/
void ADS8688_Init(void)
{
ADS8688_WriteProgramRegister(Channel_0_Input_Range, VREF_U_0_125);  //设置通道0输⼊范围为:±2.5×VREF
//第⼀次写⼊异常
ADS8688_WriteProgramRegister(Channel_0_Input_Range, VREF_U_0_125);  //设置通道0输⼊范围为:±2.5×VREF
ADS8688_WriteProgramRegister(Channel_1_Input_Range, VREF_U_0_125);  //设置通道1输⼊范围为:±2.5×VREF
ADS8688_WriteProgramRegister(Channel_2_Input_Range, VREF_U_0_125);  //设置通道2输⼊范围为:±2.5×VREF
ADS8688_WriteProgramRegister(Channel_3_Input_Range, VREF_U_0_125);  //设置通道3输⼊范围为:±2.5×VREF
ADS8688_WriteProgramRegister(Channel_4_Input_Range, VREF_U_0_125);  //设置通道4输⼊范围为:±2.5×VREF
ADS8688_WriteProgramRegister(Channel_5_Input_Range, VREF_U_0_125);          //设置通道5输⼊范围为:±2.5×VREF  ADS8688_WriteProgramRegister(Channel_6_Input_Range, VREF_U_0_125);          //设置通道6输⼊范围为:±2.5×VREF  ADS8688_WriteProgramRegister(Channel_Power_Down,
0x80); //通道0-6上电
ADS8688_WriteProgramRegister(AUTO_SEQ_EN, 0x7F);//⾃动扫描通道0-6
ADS8688_WriteCmdReg(AUTO_RST);//进⼊⾃动扫描通道模式(具有复位功能)
}
/*===============================================================
函数名称:Get_AUTO_RST_Mode_Data
函数功能:读取AUTO_RST模式下的输出数据
输⼊参数:chn:总通道号
返回值:⽆
================================================================*/ void Get_AUTO_RST_Mode_Data(u8 chn)
{
u16 i = 0;
u16 j;
for(i=0; i<chn; i++)
{
ADS8688_CS_0;
SPI1->DR = 0;
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
j = SPI1->DR;
SPI1->DR = 0;
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
Transfer_Data[0+i*3] = SPI1->DR;
SPI1->DR = 0;
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
Transfer_Data[1+i*3] = SPI1->DR;
SPI1->DR = 0;
while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);//判断接收数据缓存
Transfer_Data[2+i*3] = SPI1->DR;
ADS8688_CS_1;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。