使用STM32单片机硬件(SPI),ADS1120芯片程序是出现通道之间数据相串(将信号输入IN0,其余的两个通道(IN1、IN2)出现ADC值一样);电路图如下:
在程序上我是使用硬件的SPI程序:
#include "ADS1120.h"
unsigned char channel0[8],channel1[8],Init_Config[4];
void SPI3_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);//PORTB时钟使能
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI3, ENABLE );//SPI3时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB3/4/5复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5); //PB3/4/5上拉
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;//SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPI3, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPI3, ENABLE); //使能SPI外设
SPI3_ReadWriteByte(0xFF);//启动传输
}
/***************************************************************************
* Function Name: SPI3_SetSpeed(u8 SPI_BaudRatePrescaler)
* Purpose: SPI velocity
* Params : 1.SPI_BaudRatePrescaler:速率
*
*************************************************************************/
//SPI 速度设置函数
//SpeedSet:
//SPI_BaudRatePrescaler_2 2分频
//SPI_BaudRatePrescaler_8 8分频
//SPI_BaudRatePrescaler_16 16分频
//SPI_BaudRatePrescaler_256 256分频
void SPI3_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI3->CR1&=0XFFC7;
SPI3->CR1|=SPI_BaudRatePrescaler; //设置SPI3速度
SPI_Cmd(SPI3,ENABLE);
}
//SPIx 读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
u8 SPI3_ReadWriteByte(u8 TxData)
{
u8 retry=0;
ADS11XX_CS = 0;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI3, TxData); //通过外设SPIx发送一个数据
retry=0;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位
{
retry++;
if(retry>200)return 0;
}
ADS11XX_CS = 1;
return SPI_I2S_ReceiveData(SPI3); //返回通过SPIx最近接收的数据
}
/****************************************************************
* @brief ADS1120_WriteRead.
* @param None
* @retval None
*/
void ADS1120_WriteRead(uint8_t Byte, uint8_t *RxBuffer,uint16_t RxLenth)
{
int i;
SPI3_ReadWriteByte(Byte);
for(i=0; i<RxLenth; i++)
{
*RxBuffer = SPI3_ReadWriteByte(0xFF);
RxBuffer++;
}
}
/***************************************************************************
* Function Name: void ADS1120_RESET(void)
* Purpose: reset ADS1120
* Params :
*
*************************************************************************/
void ADS1120_RESET(void)
{
SPI3_ReadWriteByte(ADS1220_RESET_CMD); //ADS1120复位
}
//
void ADS1120_START()
{
SPI3_ReadWriteByte(ADS1220_START_CMD); //ADS1120复位
}
/***************************************************************************
* Function Name: ReadData()
* Purpose: read data from ADS1120
* Params : result 0~65535
*
*************************************************************************/
unsigned int ReadData(void)
{
unsigned int result;
uint8_t buf[3];
ADS1120_WriteRead(ADS1220_RDATA_CMD,buf,3);
result = buf[0];
result = result*256+buf[1];
result = result*256+buf[2];
result = result/256;
return result;
}
/***************************************************************************
* Function Name: ADPowerDown()
* Purpose:
* Params :
*
*************************************************************************/
void ADPowerDown(void)
{
SPI3_ReadWriteByte(ADS1220_POWERDOWN_CMD);
}
/***************************************************************************
* Function Name: ADStartConversion()
* Purpose: start conversion
* Params :
*
*************************************************************************/
void ADStartConversion(void)
{
SPI3_ReadWriteByte(ADS1220_START_CMD);
}
/***************************************************************************
* Function Name: ReadRegister()
* Purpose: read data from register
* Params : data
*
*************************************************************************/
void ReadRegister(void)
{
unsigned char i;
unsigned long Data;
SPI3_ReadWriteByte(ADS1220_RREG_CMD|0x03);
// get the register content
for (i=0; i< 4; i++)
{
Data += SPI3_ReadWriteByte(0xFF);
}
return;
}
/***************************************************************************
* Function Name: WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData)
* Purpose: write data to register
* steps: 1.write address 2.write data
* Params NumRegs-----the numbers of register
*************************************************************************/
void WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData)
{
unsigned char i;
// send the command byte
SPI3_ReadWriteByte(ADS1220_WREG_CMD | (((StartAddress<<2) & 0x0c) |((NumRegs-1)&0x03)));
// send the data bytes
for (i=0; i< NumRegs; i++)
{
SPI3_ReadWriteByte(*pData);
pData++;
}
return;
}
/***************************************************************************
* Function Name: CofigAD(unsigned char channel)
* Purpose: set the sampling channel
* Params :
*
*************************************************************************/
void CofigAD(unsigned char channel)
{
switch(channel)
{
case 0: Init_Config[0] = 0x81;
// Init_Config[3] = 0x30;
break;
case 1: Init_Config[0] = 0x91;
// Init_Config[3] = 0x50;
break;
case 2: Init_Config[0] = 0xA1;
// Init_Config[3] = 0x70;
break;
case 3: Init_Config[0] = 0xB1;
break;
}
Init_Config[1] = 0X00;//Init_Config[1] = 0XC0;
Init_Config[2] = 0X40;//0X50;//Init_Config[2] = 0X40;
Init_Config[3] = 0X00;
WriteRegister(0x00,0x04,Init_Config);
ReadRegister();
}
/***************************************************************************
* Function Name: SetChannelPGA(uchar channel, uchar gain)
* Purpose: set the sampling channel and the gain
* Params :
*
*************************************************************************/
void SetChannelPGA(u8 channel, u8 gain)
{
switch(channel)
{
case 0:
if(gain==1)
channel0[0] = 0x81;
if(gain==2)
channel0[1] = 0x83;
if(gain==4)
channel0[2] = 0x85;
if(gain==8)
channel0[3] = 0x87;
if(gain==16)
channel0[4] = 0x89;
if(gain==32)
channel0[5] = 0x8B;
if(gain==64)
channel0[6] = 0x8D;
if(gain==128)
channel0[7] = 0x8F;
break;
case 1:
if(gain==1)
channel1[0] = 0x91;
if(gain==2)
channel1[1] = 0x93;
if(gain==4)
channel1[2] = 0x95;
if(gain==8)
channel1[3] = 0x97;
if(gain==16)
channel1[4] = 0x99;
if(gain==32)
channel1[5] = 0x9B;
if(gain==64)
channel1[6] = 0x9D;
if(gain==128)
channel0[7] = 0x9F;
break;
case 2: Init_Config[0] = 0xA1;
break;
case 3: Init_Config[0] = 0xB1;
break;
}
}
/***************************************************************************
* Function Name: GetAD(unsigned char channel)
* Purpose: get the result
* Params : Result 0~65535
*
*************************************************************************/
unsigned int GetAD(unsigned char channel)
{
unsigned int Result;
CofigAD(channel);
ADStartConversion();
// while(DRDY_IN);
Result = ReadData();
ADPowerDown();
return Result;
}
//初始化ADS1120
void ADS1120_Init(void)
{
SPI3_Init();
SPI3_SetSpeed(SPI_BaudRatePrescaler_8);//设置为18M时钟,高速模式
ADS1120_RESET();
}
file:///C:\Users\Administrator\Documents\Tencent Files\414941286\Image\C2C\Z756CXS]$ZS@A8W(WQPO)ER.png
file:///C:\Users\Administrator\Documents\Tencent Files\414941286\Image\C2C\Z756CXS]$ZS@A8W(WQPO)ER.png
|