我用STM32F107 SPI方式采集ADC128S022的问题,采到的数据不对,我结合官网提供的M430的例子,没用过M430
我用STM32F107 SPI方式采集ADC128S022的问题,采到的数据不对,我结合官网提供的M430的例子,没用过M430,
#define NUM_OF_RESULTS 8 // Number of temp sensor samples to take
#define ADC_CHANNEL 0 // ADC channel for conversion
#define SCALE_FACTOR 3 // For averaging converted samples
#define ADC12_RATIO 0.805664 // 3300/4096(3300mV ref & 12bit converter, straight binary o/p)
#define ADC_INPUT0 0
#define ADC_INPUT1 1
#define ADC_INPUT2 2
#define ADC_INPUT3 3
#define ADC_INPUT4 4
#define ADC_INPUT5 5
#define ADC_INPUT6 6
#define ADC_INPUT7 7
volatile unsigned char bdata;
unsigned char ADC_Input = 0x00;
unsigned char ADC_Cnt = 0x00;
unsigned short ADC_Data = 0x0000;
unsigned char ADC_Numbytes = 0x00;
unsigned char i ;
void TI_ADC128S022_Init(void)
{
ADC_Numbytes = 16;
ADC_Data = 0;
AD_CS_DISABLE();
AD_SCLK_DISABLE();
Delay(1000);
AD_CS_ENABLE();
#if 0
switch(NUM_OF_RESULTS)
{
case 0:ADC_Input = ADC_INPUT0; break;
case 1:ADC_Input = ADC_INPUT1; break;
case 2:ADC_Input = ADC_INPUT2; break;
case 3:ADC_Input = ADC_INPUT3; break;
case 4:ADC_Input = ADC_INPUT4; break;
case 5:ADC_Input = ADC_INPUT5; break;
case 6:ADC_Input = ADC_INPUT6; break;
case 7:ADC_Input = ADC_INPUT7; break;
default:
break;
}
#endif
ADC_Input = 0xe1;
for(ADC_Cnt = 0; ADC_Cnt < ADC_Numbytes; ADC_Cnt++)
{
Delay(1000);
AD_SCLK_DISABLE();
if (ADC_Cnt<5)
{
if(ADC_Input&0x80)
{
ADC_DOU_ENABLE();
}
else
{
ADC_DOU_DISABLE();
}
ADC_Input <<= 1;
}
else
{
ADC_DOU_DISABLE();
}
Delay(1000);
AD_SCLK_ENABLE();
Delay(1000);
// if(ADC_Cnt >= 4 )
// {
// ADC_Data <<= 1;
//if(ADC_DIN_ENABLE)
// {
// ADC_Data |= 0x0001;
// }
// else
// {
// ADC_Data &= 0xfffe;
// }
}
// else
// {;}
//}
AD_CS_DISABLE();
//AD_SCLK_ENABLE();
// *adc_data = ADC_Data;
// return;
// printf("%d\r\n",ADC_Data); //这儿printf串口输出是4095
}
void TI_ADC128S022_ADC_vout (void)
{
uint8_t i;
static uint16_t results[NUM_OF_RESULTS]; // To store ADC output
static uint32_t sum_adc_data = 0; // accumulate and avg adc results
volatile static float vout; // ADC vout
for(i=0; i<8;i++)
{
results[i] = SPI_ReadWriteByte(ADC_CHANNEL); // read and store ADC Channel output
sum_adc_data += results[i]; // accumulate result
Delay(1000); // acquisition time
}
sum_adc_data >>= SCALE_FACTOR; // Average the accumulated sum
vout = sum_adc_data * ADC12_RATIO; // ADC vout
sum_adc_data = 0; // Set Breakpoint here & see measured vout
while(1)
{
printf("%f\r\n",vout); //#define NUM_OF_RESULTS 8 // Number of temp sensor samples to take
#define ADC_CHANNEL 0 // ADC channel for conversion
#define SCALE_FACTOR 3 // For averaging converted samples
#define ADC12_RATIO 0.805664 // 3300/4096(3300mV ref & 12bit converter, straight binary o/p)
#define ADC_INPUT0 0
#define ADC_INPUT1 1
#define ADC_INPUT2 2
#define ADC_INPUT3 3
#define ADC_INPUT4 4
#define ADC_INPUT5 5
#define ADC_INPUT6 6
#define ADC_INPUT7 7
volatile unsigned char bdata;
unsigned char ADC_Input = 0x00;
unsigned char ADC_Cnt = 0x00;
unsigned short ADC_Data = 0x0000;
unsigned char ADC_Numbytes = 0x00;
unsigned char i ;
void TI_ADC128S022_Init(void)
{
ADC_Numbytes = 16;
ADC_Data = 0;
AD_CS_DISABLE();
AD_SCLK_DISABLE();
Delay(1000);
AD_CS_ENABLE();
#if 0
switch(NUM_OF_RESULTS)
{
case 0:ADC_Input = ADC_INPUT0; break;
case 1:ADC_Input = ADC_INPUT1; break;
case 2:ADC_Input = ADC_INPUT2; break;
case 3:ADC_Input = ADC_INPUT3; break;
case 4:ADC_Input = ADC_INPUT4; break;
case 5:ADC_Input = ADC_INPUT5; break;
case 6:ADC_Input = ADC_INPUT6; break;
case 7:ADC_Input = ADC_INPUT7; break;
default:
break;
}
#endif
ADC_Input = 0xe1;
for(ADC_Cnt = 0; ADC_Cnt < ADC_Numbytes; ADC_Cnt++)
{
Delay(1000);
AD_SCLK_DISABLE();
if (ADC_Cnt<5)
{
if(ADC_Input&0x80)
{
ADC_DOU_ENABLE();
}
else
{
ADC_DOU_DISABLE();
}
ADC_Input <<= 1;
}
else
{
ADC_DOU_DISABLE();
}
Delay(1000);
AD_SCLK_ENABLE();
Delay(1000);
// if(ADC_Cnt >= 4 )
// {
// ADC_Data <<= 1;
//if(ADC_DIN_ENABLE)
// {
// ADC_Data |= 0x0001;
// }
// else
// {
// ADC_Data &= 0xfffe;
// }
}
// else
// {;}
//}
AD_CS_DISABLE();
//AD_SCLK_ENABLE();
// *adc_data = ADC_Data;
// return;
// printf("%d\r\n",ADC_Data);
}
void TI_ADC128S022_ADC_vout (void)
{
uint8_t i;
static uint16_t results[NUM_OF_RESULTS]; // To store ADC output
static uint32_t sum_adc_data = 0; // accumulate and avg adc results
volatile static float vout; // ADC vout
for(i=0; i<8;i++)
{
results[i] = SPI_ReadWriteByte(ADC_CHANNEL); // read and store ADC Channel output
sum_adc_data += results[i]; // accumulate result
Delay(1000); // acquisition time
}
sum_adc_data >>= SCALE_FACTOR; // Average the accumulated sum
vout = sum_adc_data * ADC12_RATIO; // ADC vout
sum_adc_data = 0; // Set Breakpoint here & see measured vout
while(1)
{
printf("%f\r\n",vout); //数据是205.444321
Delay(5000000);
}
}
Delay(5000000);
}
}
各位看看,问题出在哪了,急求呀,下面是我的程序,麻烦各位
采到的数据不对, 差别有规律吗?
我看先验证一下硬件和时序吧.