STM32F103控制AD7606采集模拟信号

1、配置STM32F103的SPI口
void AD7606_Port_Init(void)
{
GPIO_InitTypeDef  GPIO_InitStructure;
SPI_InitTypeDef   SPI_InitStructure;
RCC_APB2PeriphClockCmd(AD_SPI_CS_GPIO_CLK | AD_SPI_MISO_GPIO_CLK | AD_SPI_SCK_GPIO_CLK, ENABLE);

RCC_APB1PeriphClockCmd(AD_SPI_CLK, ENABLE); 

/////////////////////////SPI_CLK////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(AD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);

/////////////////////////SPI_MISO////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
GPIO_Init(AD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);

  /////////////////////////SPI_CS////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(AD_SPI_CS_GPIO_PORT, &GPIO_InitStructure);

/////////////////////////SPI_³õʼ»¯////////////////////////
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(AD_SPI, &SPI_InitStructure);
SPI_Cmd(AD_SPI, ENABLE); 




/////////////////////////AD7606_RESET////////////////////////
RCC_APB2PeriphClockCmd(AD_RESET_GPIO_CLK | AD_CONVT_GPIO_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = AD_RESET_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(AD_RESET_GPIO_PORT, &GPIO_InitStructure);

/////////////////////////AD7606_CONVT////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_CONVT_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(AD_CONVT_GPIO_PORT, &GPIO_InitStructure);


}

2、AD7606复位信号
void AD7606_Reset(void)
{
AD_RESET_LOW();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_LOW();
}

3、AD7606开始转换信号
void AD7606_StartConvt(void)
{
AD_CONVT_LOW();
AD_CONVT_LOW();
AD_CONVT_LOW();
AD_CONVT_HIGH();
}

4、STM32F103读取AD7606数据
short AD7606_ReadBytes(void)
{
  short AD7606_Data = 0;
while (SPI_I2S_GetFlagStatus(AD_SPI, SPI_I2S_FLAG_TXE) == RESET)
  {
  }
SPI_I2S_SendData(AD_SPI, 0xFFFF);
while (SPI_I2S_GetFlagStatus(AD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  {
  }
AD7606_Data = SPI_I2S_ReceiveData(AD_SPI);
return AD7606_Data;


}

5、STM32F103读取AD7606前CS拉低,拉高,重新开始转换
void samp_return(void)
{
int j=0;
AD_SPI_CS_LOW();
for(j=0;j<6;j++)
{
AD7606_Return[j]=AD7606_ReadBytes();
}
AD_SPI_CS_HIGH();
//ads7606_Delay(100);
AD7606_StartConvt();
ads7606_Delay(200);


}

6、数据量转化为模拟量公式

    电压=samp_sum_jiaoshudu_return[0]*10.0/32768/(2.5/2.5);//采用正负10电压模拟输入,参考电压2.5V

7、主函数main中初始化AD7606

                AD7606_Port_Init();
Adc_Init();
      AD7606_Reset();
   AD_SPI_CS_HIGH();
   AD_CONVT_HIGH();

          AD7606_StartConvt();



你可能感兴趣的:(单片机,VC++)