再次求助,ADS1118读取第二通道的问题
再次求助,ads1118要读取第二通道,按如下设置,读到的都是第一通道的值,无法读取第二通道的值,请高人再次指点。感谢!
#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);
float Voltage23;
void main(void)
{
volatile int ADC_Result;
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
GPIO_Init();
SPI_Init();
ADS_Config();
while(1)
{
ADS_Config();
ADC_Result = ADS_Read(); // Read data from ADS1118
Voltage23 = ADC_Result*1.0/32768*2.048;
__delay_cycles(100);
_no_operation();
}
}
void GPIO_Init(void)
{
P1OUT |= 0x02; // Set P1.1 for CS
P1DIR |= 0x02; // Set P1.1 to output direction
P3SEL |= 0x80; // P3.7 option select
P5SEL |= 0x30; // P5.4,5 option select
P5DIR |= BIT0; // P5.0 Output
P5OUT |= BIT0; // P5.0 Output High
}
void SPI_Init(void)
{
UCB1CTL1 |= UCSWRST; // **Put state machine in reset**
UCB1CTL0 |= UCMST+UCSYNC+UCCKPH+UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCB1CTL1 |= UCSSEL_2; // SMCLK
UCB1BR0 = 200; // /2
UCB1BR1 = 1; //
UCB1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__delay_cycles(100); // Wait for slave to initialize
}
void ADS_Config(void)
{
signed int Config_Value; // Initial Config Register
Config_Value = 0xB48C;
// ADS1118 configuration AIN0/AIN1, FS=+/-2.048, DR=128sps, PULLUP on DOUT
P1OUT &=~ 0x02; // Set CS low
WriteSPI(Config_Value,0); // Write configuration to ADS1118
P1OUT |= 0x02; // Set CS high
}
int ADS_Read(void)
{
unsigned int Data, Config_Value;
Config_Value = 0xB48C;
// ADS1118 configuration AIN0/AIN1, FS=+/-2.048, DR=128sps, PULLUP on DOUT
P1OUT &=~ 0x02; // Set CS low
Data = WriteSPI(Config_Value,1); // Read data from ADS1118
P1OUT |= 0x02; // 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)
{
signed int msb;
unsigned int temp;
volatile signed int dummy;
temp = config;
if (mode==1) temp = 0;
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF = (temp >> 8 ); // Write MSB of Config
while(!(UCB1IFG&UCRXIFG));
msb = UCB1RXBUF; // Read MSB of Data
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF = (temp & 0xff); // Write LSB of Config
while(!(UCB1IFG&UCRXIFG));
msb = (msb << 8) | UCB1RXBUF; // Read LSB of Data
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF = (temp >> 8 ); // Write MSB of Config
while(!(UCB1IFG&UCRXIFG));
dummy = UCB1RXBUF; // Read MSB of Config
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF= (temp & 0xff); // Write LSB of Config
while(!(UCB1IFG&UCRXIFG));
dummy = (dummy <<8) | UCB1RXBUF; // Read LSB of Config
__delay_cycles(100);
return msb;
}
是这样的,对ADS1118,当次写入命令时,读到的值是上一次配置的转化结果。当你对ADC进行这次配置时,在下一次不管配置如何,读到的都是按这次配置的转化结果。
首先感谢专家。我当时也这么考虑过,但是试了好久,也用好几种控制字。还用了您帖子上的程序,好像都是只读到一个通道。