微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 硬件电路设计 > TI模拟硬件电路设计 > 关于ADS1118模数转换器的问题

关于ADS1118模数转换器的问题

时间:10-02 整理:3721RD 点击:

我采用的是将cs引脚直接与GND相连,然后怎么能进行温度转化呢,用的是stm32的spi模块

CPOL设置为低电平CPHA设置为下降沿,然后每次都是给ADS1118发送 同样的设置

 

/**
  ******************************************************************************
  * @file    ADS1118.c
  * @author  HEU-ICEC-SAST SunZhengYuan
  * @version V1.0
  * @date    20-7-2014
  * @brief   Analog to Digital Converter Driver
  ******************************************************************************
  * @attention  The source of this program come from the Internet.
  *and intergrated by Sunzhengyuan.
  ******************************************************************************
  */
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"
#include "ADS1118.h"

//----------------------Data Buffer--------------------------------------
/**THESE PARAM TO STORE THE DATA THAT WILL TRANSMIT VIA SPI,AND RECERIVE
  *THE DATA FROM SPI
  ***/
volatile uint8_t readata1;
volatile uint8_t readata2;
volatile uint8_t readata3;
volatile uint8_t readata4;
volatile uint32_t readata;

/**********************************************************************
**********************************************************************/
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO , ENABLE);
 /*   //LDO power2 control pin
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;            //????
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);                      //B??
    //POWER OFF CONTROL PIN
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;                   //KEYPOWER PA15
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure); */

    //ADS1118
    //AD_SPI1_CS    PA4
    //AD_SPI1_SCK   PA5
    //AD_SPI1_MISO  PA6
    //AD_SPI1_MOSI  PA7

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                    //
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                    //
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                    //
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;                    //
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   
}

uint16_t ADS_configuration(void)
{
 uint16_t ADS_Con;
 ADS_InitTypeDef ADS_InitStructure;
 ADS_InitStructure.stru.OS         = SINGLE_CONVER;
  ADS_InitStructure.stru.MUX        = AINPN_0_1;
  ADS_InitStructure.stru.PGA        = PGA_2048;
  ADS_InitStructure.stru.MODE = CONTIOUS;
  ADS_InitStructure.stru.DR         = DR_128_SPS;
  ADS_InitStructure.stru.TS_MODE = TEMPERATURE_MODE;        //TEMPERATURE MODE
  ADS_InitStructure.stru.PULLUP         = PULL_UP_DIS;
  ADS_InitStructure.stru.NOP                 = DATA_VALID;
  ADS_InitStructure.stru.CNV_RDY_FL = DATA_NREADY;
 
 ADS_Con=ADS_InitStructure.word;
 return (ADS_Con);
 
}
/**********************************************************************
**********************************************************************/
void SPI_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);    
    //AD_SPI1_CS    PA4
    //AD_SPI1_SCK   PA5
    //AD_SPI1_MISO  PA6
    //AD_SPI1_MOSI  PA7
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                                            //
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;                    //
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                                            //
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;           //???
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;                                  //???
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;                              //8?
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;                                         //???? ?????,SCK?????
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; 
 // SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;   //???? ??????????????
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                                      //????NSS
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;          //????? SYSCLK/16
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;                                 //??????
    SPI_InitStructure.SPI_CRCPolynomial = 7;                                       //CRC??????????7
    SPI_Init(SPI1, &SPI_InitStructure);
    /* ??SPI1  */
    SPI_Cmd(SPI1, ENABLE);
}
/**********************************************************************

**********************************************************************/
void delay( uint32_t nCount )
{
    for(; nCount != 0; nCount--)
    {
        ;
    }
}
/**********************************************************************

**********************************************************************/
uint8_t SPI1_SendByte(uint8_t byte)
{

    while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
    {
        ;
    }

    SPI_I2S_SendData(SPI1, byte);

    while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
    {
        ;
    }

    return SPI_I2S_ReceiveData(SPI1);
}

/**********************************************************************
**********************************************************************/
uint32_t SPI_RW_Reg(uint16_t CofigReg)
{

   // delay(10);
 delay(10);
    readata1=SPI1_SendByte((uint8_t)(CofigReg>>8));
    readata2=SPI1_SendByte((uint8_t)CofigReg);
    readata3=SPI1_SendByte((uint8_t)(CofigReg>>8));
    readata4=SPI1_SendByte((uint8_t)CofigReg);
    readata= (uint32_t)readata4|((uint32_t)readata3<<8)|((uint32_t)readata2<<16)|((uint32_t)readata1<<24);
  //  delay(1000);
 delay(1000);
    //ads_cs_1();
    return readata;
}

 

#ifndef ADS1118_H
#define ADS1118_H
#include "stdint.h"
// ------ Public constants -----------------------------------------

//------------------------------------------------------------------
// Should Not Need To Edit The Section Blow
//------------------------------------------------------------------
// ------ Define and Typedefine-------------------------------------
//#define   AD_SPI1_CS      PA4
//#define   AD_SPI1_SCK     PA5
//#define   AD_SPI1_MISO    PA6
//#define   AD_SPI1_MOSI    PA7
//( Config Register )
//Operational status/single-shot conversion start
#define     CONFIG_BIT_OS       (1<<15)
//MUX[2:0]: Input multiplexer configuration
#define     CONFIG_BIT_MUX      (7<<12)
//PGA[2:0]: Programmable gain amplifier configuration
#define     CONFIG_BIT_PGA      (7<<9)
//MODE: Device operating mode
#define     CONFIG_BIT_MODE     (1<<8)
//DR[2:0]: Data rate
#define     CONFIG_BIT_DR       (7<<5)
//TS_MODE: Temperature sensor mode
#define     CONFIG_BIT_TS_MODE  (1<<4)
//PULL_UP_EN: Pull-up enable
#define     CONFIG_BIT_PULLUP_EN    (1<<3)
//NOP: No operation
#define     CONFIG_BIT_NOP      (3<<1)
//CNV_RDY_FL: Conversion ready flag
#define     CONFIG_BIT_CNV_RDY_FL   (1<<0)
// ------ Public data type declarations ----------------------------
typedef union
{
    //uint8_t Data_MSB  :8;
    //uint8_t   Data_LSB    :8;
    struct
    {
        volatile uint8_t    CNV_RDY_FL  :1;   //low
        volatile uint8_t    NOP         :2;
        volatile uint8_t    PULLUP      :1;
        volatile uint8_t    TS_MODE     :1;
        volatile uint8_t    DR          :3;
        volatile uint8_t    MODE        :1;
        volatile uint8_t    PGA         :3;
        volatile uint8_t    MUX         :3;
        volatile uint8_t    OS          :1;   //high
    } stru;
    volatile uint16_t word;

} ADS_InitTypeDef;
typedef enum
{
    CONVERING = 0x0,
    SINGLE_CONVER = 0x1
} ADS_OS_TypeDef;
typedef enum
{
    AINPN_0_1 = 0x0,
    AINPN_0_3 =   0x1,
    AINPN_1_3 =   0x2,
    AINPN_2_3 =   0x3,
    AINPN_0_GND=  0x4,
    AINPN_1_GND=  0x5,
    AINPN_2_GND=  0x6,
    AINPN_3_GND=  0x7
} ADS_MUX_TypeDef;
typedef enum
{
    PGA_6144 = 0x0,
    PGA_4096 = 0x1,
    PGA_2048 = 0x2,
    PGA_1024 = 0x3,
    PGA_512 = 0x4,
    PGA_256 = 0x5
} ADS_PGA_TypeDef;
typedef enum
{
    CONTIOUS  =  0x0,
    SINGLE_SHOT = 0x1
} ADS_MODE_TypeDef;
typedef enum
{
    DR_8_SPS   =   0x0,
    DR_16_SPS  =   0x1,
    DR_32_SPS  =   0x2,
    DR_64_SPS  =   0x3,
    DR_128_SPS =   0x4,
    DR_250_SPS =   0x5,
    DR_475_SPS =   0x6,
    DR_860_SPS =   0x7
} ADS_DATARATE_TypeDef;
typedef enum
{
    ADC_MODE    =   0x0,
    TEMPERATURE_MODE =  0x1
} ADS_TSMODE_TypeDef;
typedef enum
{
    PULL_UP_DIS = 0x0,
    PULL_UP_EN  = 0x1
} ADS_PULL_TypeDef;
typedef enum
{
    DATA_VALID      = 0x1,
    DATA_INVALID    = 0x0
} ADS_NOP_TypeDef;
typedef enum
{
    DATA_READY = 0x0,
    DATA_NREADY = 0x1
} ADS_RDY_TypeDef;

/*******************************************************************
                **** DO AS THE DIRECTION ****
*******************************************************************/

// There is no need to use this part when CS is tied to the Pin of GND.
/*
// ------ Public function prototypes -------------------------------
#define ads_cs_0()     GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define ads_cs_1()    GPIO_SetBits(GPIOA, GPIO_Pin_4)
*/


void delay( uint32_t nCount );

/**
  *IF you want to configuration ADS1118,you should add these two
  *configuration function to your Process first .
  **/
 void GPIO_Configuration(void);
 void SPI_Configuration(void);
/**
  *You can config the ADS in the ADS_InitInstrcutre,when you use
 *a parameter to rever a reval of this function;
 *          EXA:   ADS_Con=ADS_configuration();
 *Then,when you want to get a converted value ,just use a new
 *parameter to do like this
 *          EXA:   Converted_value=SPI_RW_Reg(ADS_Con);
 *then the value is in this parameter "Converted_value"
  **/
 uint16_t ADS_configuration(void);
 uint32_t SPI_RW_Reg(uint16_t CofigReg);
/**
  *Private function
  **/
uint8_t SPI1_SendByte(uint8_t byte);

#endif
/*******************************************************************
            **** END OF FILE ****
*******************************************************************/

 

程序本来是从网上找的,我后来改的,唯一的优点是接上电路能检测单存在DRDY低电平,但是示数却总是卡住,每次按复位数字又变化,但是好像是随机数,拜托,希望高手给看看是不是程序的问题

你好,

在CS引脚一直拉低的情况下,ADS1118内部进行转换,当新的转换结果可以读取的时候,DOUT/DRDY 就被拉低了,可以用这个信号作为中断信号,触发MCU开始读取转换结果。

数据传送的具体细节可以参考数据手册23页 32-Bit Data Transmission Cycle 章节。

建议将 通信时序 用示波器采出来,跟数据手册第六页的SPI TIMING CHARACTERISTICS做比较,也可以上传以便分析。

不太清楚你所描述的具体问题,能否将你的测试过程详细地描述一下?

请问有没有参考程序,我想看看究竟是我电路板的问题还是程序的问题,因为一直采不到波形

sunzhengyuan@qq.com

你好,

没有C语言的参考程序,有ADS1118EVM的参考程序:ADS1118EVM Source Code 

采不到波形?你的输入是什么样的信号?

这个ADC配置起来不复杂,先检查供电电压,输入电压;然后检查接口时序。一般读出数据来是没问题的。建议输入DC电压测试先。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top