在EK-LM4F120XL Launchpad中,
我分别用
AIN0(PE3口)采样1.3V电压
AIN1(PE2口)采样0.65V电压,
AIN8(PE5口)采样1.95V电压,
AIN9(PE4口)采样2.6V电压,
假如以3.0V作为参考电压,那么在数据寄存器中采样结果应该是:
1775,对应1.3V电压
888,对应采样0.65V电压,
2659,对应采样1.95V电压,
3563,对应采样2.6V电压,
但是实际结果是:
AIN0:1645,根据1.3*4096/1645=3.23,参考电压为3.23V,
AIN1:1005,根据0.65*4096/1005=2.65,参考电压为2.65V,
AIN8:2175,根据1.95*4096/2175=3.76,参考电压为3.67V,
AIN9:2765,根据2.61*4096/2765=3.87,参考电压为3.87V,
如上,每次的结果都不一样。这是为什么呢?(如果参考电压假定为3.3V,结果也是类似的)
我的源程序如下(采样时只要修改相应的端口就可以了):
#include"inc/hw_ints.h"
#include"inc/hw_types.h"
#include"inc/hw_memmap.h"
#include"driverlib/sysctl.h"
#include"driverlib/interrupt.h"
#include"driverlib/adc.h"
#include"driverlib/gpio.h"
unsigned char adc_endflag=0;
void enable_adc_pin()
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeADC(GPIO_PORTE_BASE,GPIO_PIN_2);
}
void config_adc()
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlADCSpeedSet(SYSCTL_ADCSPEED_125KSPS);
ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE,0,0,ADC_CTL_CH1|ADC_CTL_END|ADC_CTL_IE);
ADCSequenceEnable(ADC0_BASE, 0);
ADCIntEnable(ADC0_BASE,0);
IntEnable(INT_ADC0);
IntMasterEnable();
}
unsigned long start_adc_sample()
{
unsigned long ulvalue;
ADCProcessorTrigger(ADC0_BASE,0);
while(adc_endflag==0);
adc_endflag=0;
ADCSequenceDataGet(ADC0_BASE,0,&ulvalue);
return(ulvalue);
}
void main()
{
//SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|
// SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
unsigned int ulva[40]={0};
unsigned char i=0;
enable_adc_pin();
config_adc();
while(1)
{
ulva
=start_adc_sample();
if(ulva>4000)
ulva=0;
i+=1;
if(i==40)
i=0;
SysCtlDelay(20000);
}
}
void adc_int_isr()
{
ADCIntClear(ADC0_BASE,0);
adc_endflag=1;
}
此帖出自小平头技术问答
一周热门 更多>