微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 硬件电路设计 > TI模拟硬件电路设计 > 求助:ADS1118的使用问题

求助:ADS1118的使用问题

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

        我用ADS1118转换热电阻电桥信号,大概0.3V左右的电压。使用的命令字为0x058B,其他的0x048B,0x058A,0x058E都用过,但是转换的数值不断变化,没有稳定的数值。并且UCTXIFG总是1,无法变成0.读出的数值随机变化,请高手帮帮忙,十分感谢。

#include "msp430x54xA.h"

void GPIO_Init(void);
void SPI_Init(void);
void ADS_Config(void);
int ADS_Read(void);
signed int WriteSPI(unsigned int Config, unsigned char mode);

void main(void)
{

 volatile int ADC_Result;

 WDTCTL = WDTPW+WDTHOLD;                 // Stop watchdog timer

 GPIO_Init();
 SPI_Init();
 ADS_Config();
 while(1)
 {
  ADC_Result = 2*ADS_Read();   // Read data from ADS1118
                if(ADC_Result>100) P5OUT &= ~BIT0;
                else P5OUT |= BIT0;
  //P1OUT = P1OUT;
 }
}

void GPIO_Init(void)
{
 //P1.0 CS
 //P3.4 DataOut(SIMO)
 //P3.5 DataIn(SOMI)
 //P3.0 Serial Clock Out

 P1OUT |= BIT0;                        //P1.0输出1
 P1DIR |= BIT0;                          //P1.0设置为输出
 P3SEL |= BIT0+BIT4+BIT5;                          // P3.0,4,5设置为第二功能

}

void SPI_Init(void)
{
 UCA0CTL1 |= UCSWRST;                    // **Put state machine in reset**
 UCA0CTL0 |= UCMST+UCSYNC+UCCKPH+UCMSB;  // 3-pin, 8-bit SPI master
                                         // Clock polarity high, MSB
 UCA0CTL1 |= UCSSEL_2;                   // SMCLK
 UCA0BR0 = 200;                         // /2
 UCA0BR1 = 1;                            //
 UCA0CTL1 &= ~UCSWRST;                   // **Initialize USCI state machine**

 __delay_cycles(100);                    // Wait for slave to initialize
}

void ADS_Config(void)
{
 signed int Config_Value;
 Config_Value = 0x058B;     // Initial Config Register
 // ADS1118 configuration AIN0/AIN1, FS=+/-2.048, DR=128sps, PULLUP on DOUT

 P1OUT &=~ BIT0;       // Set CS low
 WriteSPI(Config_Value,0);    // Write configuration to ADS1118
 P1OUT |= BIT0;       // Set CS high
}

int ADS_Read(void)
{
 unsigned int Data, Config_Value;

 Config_Value = 0x058B;
 // ADS1118 configuration AIN0/AIN1, FS=+/-2.048, DR=128sps, PULLUP on DOUT

 P1OUT &=~ BIT0;       // Set CS low
 Data = WriteSPI(Config_Value,1);  // Read data from ADS1118
 P1OUT |= BIT0;       // Set CS high

 return Data;
}

/*
 * Mode 0: Only write config register to ADS1118
 * Mode 1: Write config register to ADS1118 as well as read data from ADS1118
 */
signed int WriteSPI(unsigned int config, unsigned char mode)
{
 unsigned int temp;
 signed int msb;
 volatile signed int dummy;

 //change the polarity of UCI0B_CLK to driver ADS1118.
 UCB0CTL1 |= UCSWRST;
 UCB0CTL0 = UCMSB + UCMST + UCMODE_0 + UCSYNC;
 UCB0CTL1 &= ~UCSWRST;


 temp = config;
 if (mode==1) temp = config | 0x8000;


 while(!(UCA0IFG&UCTXIFG));
 UCA0TXBUF = (temp >> 8 );          // Write MSB of Config

 while(!(UCA0IFG&UCRXIFG));
    msb = UCA0RXBUF;                     // Read MSB of Data

    while(!(UCA0IFG&UCTXIFG));
    UCA0TXBUF = (temp & 0xff);          // Write LSB of Config

    while(!(UCA0IFG&UCRXIFG));
    msb = (msb << 8) | UCA0RXBUF;   // Read LSB of Data


    while(!(UCA0IFG&UCTXIFG));
     UCA0TXBUF = (temp >> 8 );          // Write MSB of Config


 while(!(UCA0IFG&UCRXIFG));
 dummy = UCA0RXBUF;                     // Read MSB of Config

  while(!(UCA0IFG&UCTXIFG));
     UCA0TXBUF = (temp & 0xff);          // Write LSB of Config

 while(!(UCA0IFG&UCRXIFG));
 dummy = UCA0RXBUF;                     // Read LSB of Config

 while(UCBUSY&UCA0STAT);

 //change the polarity of UCI0B_CLK to driver ADS1118.
  UCB0CTL1 |= UCSWRST;
  UCB0CTL0 = UCMSB + UCMST + UCMODE_0 + UCSYNC;
  UCB0CTL1 &= ~UCSWRST;

 

 

 __delay_cycles(10);

 return msb;
}

Bits[7:5] DR[2:0]: Data rate : 000 : 8SPS

 

上面你将BIT8位设置成了1,这就使ADS1118工作在了单次采样模式下。

在这模式下,必需每次采样时,向BIT23位写1. 这样才能读出这次采样的值,并启动下一次采样,让下一次读数时,有数可读。

请参考另一个贴子里的代码。

http://www.deyisupport.com/question_answer/analog/data_converters/f/58/t/18777.aspx

   多谢高人指点!

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

网站地图

Top