因为有项目需求,需要用到模拟量采集,但是又考虑到抗干扰,故采用专用ADC芯片,用ADUM1411将控制电路和采集电路隔离,并且考虑到IIC的方案采用软件模拟比较占用系统资源,因此确定方案,采用带有SPI功能的ADC128S022;采用DMA的模式自动采集8路adc信号。
硬件方案
100欧姆电阻必须要有
?软件方案
读1路adc数据,只需要发送两个字节?
第一个字节只需要改变第3-第5位的值,确定要采样的通道,其他都可以填0;
第二个字节直接填0就好了;
在接收的数据中只取后12位就可以了;
?实测读取adc通道时,会滞后,比如读0通道,实际读到的通道7;
#ifdef SPIU16
uint16_t aTxBuffer[8] = {0X0000,0X0800,0X1000,0X1800,0X2000,0X2800,0X3000,0X3800};//发送数组
uint16_t aRxBuffer[8];//接收数组
#else
uint8_t aTxBuffer[16] = {0X00,0x00,0X08,0x00,0X10,0x00,0X18,0x00,0X20,0x00,0X28,0x00,0X30,0x00,0X38,0x00};//发送数组
uint8_t aRxBuffer[16];//接收数组
#endif
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);//使能SPI时钟
RCC_AHB1PeriphClockCmd(SPIx_CS_GPIO_CLK, ENABLE);//使能片选时钟
#ifdef SPI_3
RCC_APB1PeriphClockCmd(SPIx_CLK, ENABLE);//使能SPI3时钟
#else
RCC_APB2PeriphClockCmd(SPIx_CLK, ENABLE);//使能SPI4时钟
#endif
RCC_AHB1PeriphClockCmd(SPIx_DMA_CLK, ENABLE); //开启DMA
//SPI外设初始化
GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;// SCK配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;//MISO配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;//MOSI配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = SPIx_CS_PIN;//片选配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(SPIx_CS_GPIO_PORT, &GPIO_InitStructure);//初始化
CS_HIGH; //片选拉高
GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT,SPIx_SCK_SOURCE,SPIx_SCK_AF); //SCK复用
GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT,SPIx_MISO_SOURCE,SPIx_MISO_AF); //MISO复用
GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT,SPIx_MOSI_SOURCE,SPIx_MOSI_AF); //MOSI复用
#ifdef SPIU16
#ifdef SPI_3
RCC_APB1PeriphResetCmd(SPIx_CLK,ENABLE);//复位SPI3
RCC_APB1PeriphResetCmd(SPIx_CLK,DISABLE);//停止复位SPI3
#else
RCC_APB2PeriphResetCmd(SPIx_CLK,ENABLE);//复位SPI4
RCC_APB2PeriphResetCmd(SPIx_CLK,DISABLE);//停止复位SPI4
#endif
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_16b; //设置SPI的数据大小:SPI发送接收16位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第一个跳变沿(上升或下降)数据被采样-----------
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; //定义波特率预分频的值:波特率预分频值为64
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 0; //CRC值计算的多项式
SPI_Init(SPIx, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPIx, ENABLE); //使能SPI外设
DMA_DeInit(SPIx_TX_DMA_STREAM);
DMA_DeInit(SPIx_RX_DMA_STREAM);
while (DMA_GetCmdStatus(SPIx_TX_DMA_STREAM) != DISABLE)
{
}
while (DMA_GetCmdStatus(SPIx_RX_DMA_STREAM) != DISABLE)
{
}
/* Configure DMA Initialization Structure */
DMA_InitStructure.DMA_BufferSize = BUFFERSIZE ; //数据传输量
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;//
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ;//
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;//
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//存储器数据长度
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //存储器增量模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//普通模式
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(SPIx->DR)) ;//外设地址
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设数据长度
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//
/* Configure TX DMA */
DMA_InitStructure.DMA_Channel = SPIx_TX_DMA_CHANNEL ;//
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;//
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aTxBuffer ;//
DMA_Init(SPIx_TX_DMA_STREAM, &DMA_InitStructure);//
/* Configure RX DMA */
DMA_InitStructure.DMA_Channel = SPIx_RX_DMA_CHANNEL ;//
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aRxBuffer ; //
DMA_Init(SPIx_RX_DMA_STREAM, &DMA_InitStructure);
//DMA_ITConfig(SPIx_TX_DMA_STREAM, DMA_IT_TC, ENABLE);
DMA_ITConfig(SPIx_RX_DMA_STREAM, DMA_IT_TC, ENABLE);
SPI_Init(SPIx, &SPI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SPIx_RX_DMA_IRQ; //接收完成
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearFlag(SPIx_TX_DMA_STREAM,SPIx_TX_DMA_FLAG_TCIF);
DMA_ClearFlag(SPIx_RX_DMA_STREAM,SPIx_RX_DMA_FLAG_TCIF);
CS_LOW;
DMA_Cmd(SPIx_TX_DMA_STREAM,ENABLE);
DMA_Cmd(SPIx_RX_DMA_STREAM,ENABLE);
SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE);
#else
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3,ENABLE);//复位SPI3
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3,DISABLE);//停止复位SPI3
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发送接收16位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第一个跳变沿(上升或下降)数据被采样-----------
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为64
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外设
DMA_DeInit(SPIx_TX_DMA_STREAM);
DMA_DeInit(SPIx_RX_DMA_STREAM);
/* Configure DMA Initialization Structure */
DMA_InitStructure.DMA_BufferSize = BUFFERSIZE ; //数据传输量
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;//
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ;//
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;//
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//存储器数据长度
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //存储器增量模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//普通模式
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(SPIx->DR)) ;//外设地址
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据长度
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//
/* Configure TX DMA */
DMA_InitStructure.DMA_Channel = SPIx_TX_DMA_CHANNEL ;//
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;//
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aTxBuffer ;//
DMA_Init(SPIx_TX_DMA_STREAM, &DMA_InitStructure);//
/* Configure RX DMA */
DMA_InitStructure.DMA_Channel = SPIx_RX_DMA_CHANNEL ;//
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aRxBuffer ; //
DMA_Init(SPIx_RX_DMA_STREAM, &DMA_InitStructure);
//DMA_ITConfig(SPIx_TX_DMA_STREAM, DMA_IT_TC, ENABLE);
DMA_ITConfig(SPIx_RX_DMA_STREAM, DMA_IT_TC, ENABLE);
SPI_Init(SPIx, &SPI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SPIx_RX_DMA_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
CS_LOW;
DMA_Cmd(SPIx_TX_DMA_STREAM,ENABLE);
DMA_Cmd(SPIx_RX_DMA_STREAM,ENABLE);
SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE);
#endif
}
//------------------------------------------------------------------------------------------------------
// 功能描述 :DMA数据接收完成中断
// 输入 :无
// 输出 : 无
// 调用 :中断调用
//------------------------------------------------------------------------------------------------------
void DMA1_Stream0_IRQHandler(void) //SPI3 DMA接收中断
{
if((DMA_GetFlagStatus(SPIx_RX_DMA_STREAM,SPIx_RX_DMA_FLAG_TCIF)==SET) && (DMA_GetFlagStatus(SPIx_TX_DMA_STREAM,SPIx_TX_DMA_FLAG_TCIF)==SET))
{
DMA_ClearFlag(SPIx_TX_DMA_STREAM,SPIx_TX_DMA_FLAG_TCIF);
DMA_ClearFlag(SPIx_RX_DMA_STREAM,SPIx_RX_DMA_FLAG_TCIF);
CS_HIGH;
SPIx_TX_DMA_STREAM->CR &= ~(uint32_t)DMA_SxCR_EN; //DMA发送禁止
SPIx_RX_DMA_STREAM->CR &= ~(uint32_t)DMA_SxCR_EN;//DMA接收禁止
SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq_Tx; //SPI DMA发送禁止
SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq_Rx; //SPI DMA接收禁止
CS_LOW;
SPIx_TX_DMA_STREAM->CR |= (uint32_t)DMA_SxCR_EN;//DMA发送使能
SPIx_RX_DMA_STREAM->CR |= (uint32_t)DMA_SxCR_EN;//DMA发送使能
SPIx->CR2 |= SPI_I2S_DMAReq_Tx;//SPI DMA发送使能
SPIx->CR2 |= SPI_I2S_DMAReq_Rx;//SPI DMA发送使能
}
}
|