ADS1118通道混乱了
需要多通道ADC采集,发现使用单通道的时候可以正确的读出每个通道的ADC值,如果程序中采集多个通道的电压,就会发现通道错位的情况,比如通道0上的电压到通道1上,通道1上的电压值到通道2上,或者通道2的电压到通道0,2上。CPU用的是MSP430F6638,调试了一下感觉是程序的问题,请专家看一下是什么情况,谢谢!
void __DELAY(u32 n)
{
u32 m=4*n;
while(m--);
};
void SPIA1_Init(void)
{
P8SEL |= BIT1+BIT2+BIT3; // Assign P8.1 to UCB0CLK and...
P8DIR |= BIT0+BIT1+BIT2; // P8.3 UCB0SOMI P8.2 UCB0SIMO
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL0 |= UCMST+UCSYNC+UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCA1CTL1 |= UCSSEL__SMCLK; // SMCLK
UCA1BR0 = 106;
UCA1BR1 = 0; //
UCA1MCTL = 0; // No modulation
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
// UCA1IE = UCRXIE+ UCTXIE; // Enable USCI_A0 RX,TX interrupt
}
/*MODE 0: Only write config regist to ADS1118
MODE 1: Write config regist to ADS1118 as well as read data from ADS1118
*/
int ADS1118_WriteSPI(u32 config,u8 mode)
{
u32 temp;
temp=config;
if(mode==1)
temp=config|0x8000;
int msb;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = (temp>>8);
__DELAY(100);
while (!(UCA1IFG&UCRXIFG));
msb = UCA1RXBUF;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = (temp & 0xff);
__DELAY(100);
while (!(UCA1IFG&UCRXIFG));
msb = (msb<<8) | UCA1RXBUF;
return msb;
}
void ADS1118_Config(u32 congfig_value)
{
CLR_AD_CS; //set CS low
__DELAY(100); //Wait for slave to initialize
ADS1118_WriteSPI(congfig_value,0); //Write configuration to ADS1118
__DELAY(100);
SET_AD_CS;
}
int ADS1118_Read(u32 config_value)
{
int DATA;
CLR_AD_CS;
__DELAY(100);
// delay_ms(1);
DATA = ADS1118_WriteSPI(config_value,1);
__DELAY(100);
//delay_ms(1);
SET_AD_CS;
return DATA;
}
int ADS1118(u32 congfig_value)
{
int result;
SET_AD_CS;
ADS1118_Config(congfig_value);
__DELAY(1000);
// delay_ms(5);
result = ADS1118_Read(congfig_value);
return result;
}
int GetADC_0(void)
{
return ADS1118(0x458B);
//return ADS1118(0xC58B);
}
int GetADC_1(void)
{
return ADS1118(0x558B);
//return ADS1118(0xD58B);
}
int GetADC_2(void)
{
return ADS1118(0x658B);
//return ADS1118(0xE58B);
}
int GetADC_3(void)
{
return ADS1118(0x758B);
//return ADS1118(0xF58B);
}
程序的问题建议转到MSP430的论坛会比较好一些。如果单个通道能采样,而且单个通道采样模型下可以切换到任意一个通道,那么做多通道采样就应该没啥问题。
现在的的现象,像是每次得到的数据都是上一次本应该读出来的数据。MSP430论坛有推荐的吗?