
#include "ADS1220.h"
#include "spi.h"
#include "main.h"
#include "math.h"
#include <stdio.h>
#define R_ref 20000
#define ADS1120_MAXCODE 32768
void ADS1220AssertCS(int fAssert)
{
if (fAssert)
CLR_CS1;
else
SET_CS1;
}
void ADS1220SendByte(unsigned char Byte)
{
HAL_SPI_Transmit(&hspi2,&Byte,1,10);
}
unsigned char ADS1220ReceiveByte()
{
unsigned char SData = 0xff ,Result = 0;
HAL_SPI_TransmitReceive(&hspi2,&SData,&Result,1,10);
return Result;
}
/*
******************************************************************************
higher level functions
*/
void ADS1220ReadRegister(int StartAddress, int NumRegs, unsigned * pData)
{
int i;
/* assert CS to start transfer */
ADS1220AssertCS(1);
/* send the command byte */
ADS1220SendByte(ADS1220_CMD_RREG | (((StartAddress<<2) & 0x0c) |((NumRegs-1)&0x03)));
/* get the register content */
for (i=0; i< NumRegs; i++)
{
*pData++ = ADS1220ReceiveByte();
}
/* de-assert CS */
ADS1220AssertCS(0);
return;
}
void ADS1220WriteRegister(int StartAddress, int NumRegs, unsigned char * pData)
{
int i;
/* assert CS to start transfer */
ADS1220AssertCS(1);
/* send the command byte */
ADS1220SendByte(ADS1220_CMD_WREG | (((StartAddress<<2) & 0x0c) |((NumRegs-1)&0x03)));
/* send the data bytes */
for (i=0; i< NumRegs; i++)
{
ADS1220SendByte(*pData++);
}
/* de-assert CS */
// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
ADS1220AssertCS(0);
return;
}
void ADS1220SendResetCommand()
{
/* assert CS to start transfer */
ADS1220AssertCS(1);
/* send the command byte */
ADS1220SendByte(ADS1220_CMD_RESET);
/* de-assert CS */
ADS1220AssertCS(0);
return;
}
void ADS1220SendStartCommand()
{
/* assert CS to start transfer */
ADS1220AssertCS(1);
/* send the command byte */
ADS1220SendByte(ADS1220_CMD_SYNC);
/* de-assert CS */
ADS1220AssertCS(0);
return;
}
void ADS1220SendShutdownCommand()
{
/* assert CS to start transfer */
ADS1220AssertCS(1);
/* send the command byte */
ADS1220SendByte(ADS1220_CMD_SHUTDOWN);
/* de-assert CS */
ADS1220AssertCS(0);
return;
}
/* ADS1220 Initial Configuration */
void ADS1220Init(void)
{
uint8_t ch_cfg[4]={ADS1220_MUX_1_0|ADS1220_GAIN_1|ADS1220_PGA_BYPASS, ADS1220_CC|ADS1220_DR_1000, ADS1220_VREF_EX_DED|ADS1220_PSW_SW|ADS1220_IDAC_100|ADS1220_REJECT_BOTH, ADS1220_IDAC1_AIN3|ADS1220_IDAC2_OFF|ADS1220_DRDY_MODE};
// uint8_t ch_cfg[4]={ADS1220_MUX_0_G|ADS1220_GAIN_1|ADS1220_PGA_BYPASS, ADS1220_CC|ADS1220_DR_20, ADS1220_VREF_INT|ADS1220_PSW_SW|ADS1220_IDAC_OFF, ADS1220_IDAC1_OFF|ADS1220_IDAC2_OFF};
ADS1220SendResetCommand();//¸´Î»
HAL_Delay(100);
ADS1220WriteRegister(ADS1220_0_REGISTER,4,ch_cfg);//ÅäÖÃ4¸ö¼Ä´æÆ÷
HAL_Delay(100);
ADS1220SendStartCommand();
}
long ADS1220ReadData()
{
long Data;
// unsigned int cfg[4];
/* assert CS to start transfer */
ADS1220AssertCS(1);
/* send the command byte */
ADS1220SendByte(ADS1220_CMD_RDATA);
/* get the conversion result */
//#ifdef ADS1120
// ADS1220ReadRegister(ADS1220_0_REGISTER,4,cfg);
Data = ADS1220ReceiveByte();
Data = (Data<<8) | ADS1220ReceiveByte();
/* sign extend data */
// if (Data & 0x8000)
// Data |= 0xffff0000;
//#else
// Data = ADS1220ReceiveByte();
// Data = (Data << 8) | ADS1220ReceiveByte();
////Data = (Data << 8) | ADS1220ReceiveByte();
// /* sign extend data */
// if (Data & 0x8000)
// Data |= 0xffff0000;
//#endif
/* de-assert CS */
ADS1220AssertCS(0);
return Data;
}当按照原理图和代码配置完成后,并没有电流源的输出,而且测不到准确的代码,一直在变动