基本信息
源码名称:基于stm32f103的AD7091源码
源码大小:1.63KB
文件格式:.rar
开发语言:C/C++
更新时间:2019-11-19
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 3 元 
   源码介绍
本示例基于stm32f103外设固件库开发的AD7091驱动程序,内涵初始化和AD获取操作代码

#include "ad7091.h"
#include "delay.h"
/**
  * @brief  Select AD7091: Chip Select pin low
  */
#define AD7091_CS_LOW()       GPIO_ResetBits(AD7091_CS_GPIO_PORT, AD7091_CS_PIN)
/**
  * @brief  Deselect AD7091: Chip Select pin high
  */
#define AD7091_CS_HIGH()      GPIO_SetBits(AD7091_CS_GPIO_PORT, AD7091_CS_PIN)   

/**
  * @brief  Select AD7091: Chip CONVST pin low
  */
#define AD7091_CONVST_LOW()       GPIO_ResetBits(AD7091_CONVST_GPIO_PORT, AD7091_CONVST_PIN)
/**
  * @brief  Deselect AD7091: Chip CONVST pin high
  */
#define AD7091_CONVST_HIGH()      GPIO_SetBits(AD7091_CONVST_GPIO_PORT, AD7091_CONVST_PIN)  


#define AD7091_DUMMY_BYTE 0xffff



uint16_t AD7091_SendByte(uint16_t byte)
{
  /*!< Loop while DR register in not emplty */
  while (SPI_I2S_GetFlagStatus(AD7091_SPI, SPI_I2S_FLAG_TXE) == RESET);

  /*!< Send byte through the SPI1 peripheral */
  SPI_I2S_SendData(AD7091_SPI, byte);

  /*!< Wait to receive a byte */
  while (SPI_I2S_GetFlagStatus(AD7091_SPI, SPI_I2S_FLAG_RXNE) == RESET);

  /*!< Return the byte read from the SPI bus */
  return SPI_I2S_ReceiveData(AD7091_SPI);
}

uint16_t AD7091_ReadByte(void)
{
  return (AD7091_SendByte(AD7091_DUMMY_BYTE));
}

void AD7091_LowLevel_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /*!< AD7091_SPI_CS_GPIO, AD7091_SPI_MOSI_GPIO, AD7091_SPI_MISO_GPIO
       and AD7091_SPI_SCK_GPIO Periph clock enable */
  RCC_APB2PeriphClockCmd(AD7091_CS_GPIO_CLK | AD7091_SPI_MOSI_GPIO_CLK | AD7091_SPI_MISO_GPIO_CLK |
                         AD7091_SPI_SCK_GPIO_CLK | AD7091_CONVST_GPIO_CLK, ENABLE);

  /*!< AD7091_SPI Periph clock enable */
  RCC_APB2PeriphClockCmd(AD7091_SPI_CLK, ENABLE);
 
  /*!< Configure AD7091_SPI pins: SCK */
  GPIO_InitStructure.GPIO_Pin = AD7091_SPI_SCK_PIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(AD7091_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure AD7091_SPI pins: MOSI */
  GPIO_InitStructure.GPIO_Pin = AD7091_SPI_MOSI_PIN;
  GPIO_Init(AD7091_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure AD7091_SPI pins: MISO */
  GPIO_InitStructure.GPIO_Pin = AD7091_SPI_MISO_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
  GPIO_Init(AD7091_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
 
  /*!< Configure AD7091_CS_PIN pin: AD7091 Card CS pin */
  GPIO_InitStructure.GPIO_Pin = AD7091_CS_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(AD7091_CS_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure AD7091_CONVST_PIN pin: AD7091 Card CONVST pin */
  GPIO_InitStructure.GPIO_Pin = AD7091_CONVST_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(AD7091_CONVST_GPIO_PORT, &GPIO_InitStructure);  
}

uint16_t AD7091_GetSmpNormal(void)
{
#if 1
    uint16_t adc_smp;
    AD7091_CONVST_HIGH();
    AD7091_CONVST_LOW();
    AD7091_CONVST_HIGH();
    //delay<=650ns
    delay_us(1);
    AD7091_CS_LOW();
    adc_smp = AD7091_ReadByte();
    AD7091_CS_HIGH();
    return (adc_smp>>4);

#else
    uint16_t adc_smp;
    AD7091_CONVST_LOW();
    //delay>=10ns FIXME
    AD7091_CONVST_HIGH();
    //delay<=650ns
    delay_us(1);
    AD7091_CS_LOW();
    adc_smp = AD7091_ReadByte();
    AD7091_CS_HIGH();
    return (adc_smp>>4);
#endif    
}
uint16_t AD7091_GetSmpPowerDown(void)
{
    uint16_t adc_smp;
    AD7091_CONVST_HIGH();
    delay_us(100);
    AD7091_CONVST_LOW();
    delay_us(1);
    AD7091_CS_LOW();
    adc_smp = AD7091_ReadByte();
    AD7091_CS_HIGH();
    return (adc_smp>>4);
    
}

void AD7091_Init(void)
{
      SPI_InitTypeDef  SPI_InitStructure;
    
      AD7091_LowLevel_Init();
        
      /*!< Deselect the FLASH: Chip Select high */
      AD7091_CS_HIGH();
      AD7091_CONVST_HIGH();
      /*!< SPI configuration */
      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_1Edge;
      SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
      SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    
      SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
      SPI_InitStructure.SPI_CRCPolynomial = 7;
      SPI_Init(AD7091_SPI, &SPI_InitStructure);
    
      /*!< Enable the AD7091_SPI  */
      SPI_Cmd(AD7091_SPI, ENABLE);


}