基于msp430f5529的ads1118采样问题,请问为什么我采不出我想要的 电压,以下是我的程序代码
#include "msp430.h"
#include "lcd12864p.h"
#include "main.h"
uchar str2[6]={0};
void ADS1118_GPIO_Init(void)
{ //P1OUT |= 0x02; // Set P1.0 for LED
// Set P1.1 for slave reset
// P1DIR |= 0x03; // Set P1.0-2 to output direction
P3SEL |= BIT3+BIT4; // P3.3,4 option select
P2SEL |= BIT7; // P2.7 option select
P2OUT |= BIT3; // Set P1.1 for CS
P2DIR |= BIT3; // Set P1.1 to output direction
}
/*
* Mode 0: Only write config register to ADS1118
* Mode 1: Write config register to ADS1118 as well as read data from ADS1118
*/
signed int ADS1118_WriteSPI(unsigned int config, unsigned char mode)
{
signed int msb;
unsigned int temp;
signed int dummy;
temp = config;
if (mode==1) temp = 0;
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 = (dummy <<8) |UCA0RXBUF; // Read LSB of Config
__delay_cycles(100);
return msb;
}
void ADS1118_SPI_Init(void)
{
UCA0CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x02; // 2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__delay_cycles(100); // Wait for slave to initialize
}
void ADS1118_ADS_Config(signed int temp_config_value)
{
signed int Config_Value;
Config_Value = temp_config_value;
P2OUT &=~ BIT3; // Set CS low
__delay_cycles(100); // Wait for slave to initialize
ADS1118_WriteSPI(Config_Value,0); // Write configuration to ADS1118
__delay_cycles(100); // Wait for slave to initialize
P2OUT |= BIT3; // Set CS high
}
int ADS1118_ADS_read(void)
{
unsigned int Data, Config_Value;
Config_Value = 0;
P2OUT &=~ BIT3; // Set CS low
Data = ADS1118_WriteSPI(Config_Value,1); // read data from ADS1118
P2OUT |= BIT3; // Set CS high
return Data;
}
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
volatile int ADC_result;
float re;
ADS1118_GPIO_Init();
ADS1118_SPI_Init();
//ADS_Config();
Init_Lcd(); //LCD12864初始化
while(1)
{
ADS1118_ADS_Config(0xC3E3); //Only Select AIN1,860SPS,+-4.096V scan voltage range
ADC_result = ADS1118_ADS_read(); // read data from ch1,the last time result
re = ADC_result*1.0/32768*4.096;
LCD_WriteString(1, 0,"AD DA 测试"); //不使用printf();时lcd12864测试
LCD_WriteString(3, 0,"采样值:");
LCD_WriteString(4, 0,"A1:");
LCD_WriteString(3, 4, "%.4f",ADC_result);
LCD_WriteString(4, 4, "%.4f",re);
__delay_cycles(10000);
}
}
你采样得到的结果是什么呢?全0?全1?
有没有正确的读取出寄存器内容?或是硬件问题?
楼主想要得到什么样的输出结果呢?请描述的详细点,这样我们能够帮您哦。。
给您我以前写过的AD采样的程序给您参考一下,希望对您有帮助;
1.主函数:
void main(void)
{
// USER CODE BEGIN (MAIN_Main,2)
uword temp1=0,temp2=0,temp3=0,temp4=0;
// USER CODE END
MAIN_vInit();
// USER CODE BEGIN (MAIN_Main,3)
// USER CODE END
while(1)
{
// USER CODE BEGIN (MAIN_Main,4)
ADC_vStartSeqReqChNum(0,0,0,3);
temp1=ADC_uwGetResultData0();
temp1=temp1*10+24;
temp1=temp1/48;
temp2=temp1/10;
temp3=temp1%10;
temp4=temp2<<4|temp3;
P3_DATA=temp4;
// USER CODE END
}
} // End of function main
2.ADC函数:
在检测到某点电压大于或小于与设定值,单片机产生中断,产生相应措施。
void ADC_vIsr(void) interrupt ADCINT
{
// USER CODE BEGIN (ADC_Isr,2)
uword i=0,j=0;
// USER CODE END
SFR_PAGE(_su0, SST0); // switch to page 0
// Check Interrupt Request 0 Flag
if (((IRCON1 & 0x08) != 0)) //判断中断0标志位是否置位
{
IRCON1 &= ~(ubyte)0x08; // 置位了,清除该标志位, 为下次做准备
// USER CODE BEGIN (ADC_Isr,3)
for(i=0;i<1000;i++) //八个发光二极管间隔亮,延时时间约0.45s
for(j=0;j<500;j++)
P3_DATA=0xaa;
for(i=0;i<1000;i++)
for(j=0;j<500;j++)
P3_DATA=0x55;
// USER CODE END
}
// Check Interrupt Request 1 Flag
if (((IRCON1 & 0x10) != 0)) //判断中断1标志位是否置位
{
IRCON1 &= ~(ubyte)0x10; //置位了,清除该标志位,为下次做准备
// USER CODE BEGIN (ADC_Isr,4)
for(i=0;i<1000;i++) //八个发光二极管高低半间隔亮,
for(j=0;j<500;j++)
P3_DATA=0xf0;
for(i=0;i<1000;i++)
for(j=0;j<500;j++)
P3_DATA=0x0f;
// USER CODE END
}
// USER CODE BEGIN (ADC_Isr,5)
// USER CODE END
SFR_PAGE(_su0, RST0); // restore the old SCU page
} // End of function ADC_vIsr
就是采样值乱变,完全不是实际电压,不知道问题出在哪
结果乱跳,而且跳动范围特别大