S3C2440芯片内部共有8路A/D转换通道AIN0~AIN7
AIN4、AIN5、AIN6、AIN7给触摸屏用的;
AIN0用于可调电阻(这样电压就可以作为一路模拟信号进行转换)
设置采样频率
最大的转换率在2.5MHZ转换时钟下能达到500KSPS(每秒采样500千次)
怎么判断开始转换了?用一个whlie循环,查询ENABLE_START是否变成0
怎么判断转换完成了?不断查询ECFLG是否为1
读取转换值
void Main(void)
{
int a0=0,tmp;
int Scom=0;
Set_Clk();
Uart_Init(0,115200);
Uart_Select(Scom);
adc_init();
while(1)
{
a0=ReadAdc(0);
Uart_Printf( "AIN0: %04d ", a0);
delay(1000) ;
}
}
int a0=0,tmp;
int Scom=0;
Set_Clk();
Uart_Init(0,115200);
Uart_Select(Scom);
adc_init();
while(1)
{
a0=ReadAdc(0);
Uart_Printf( "AIN0: %04d ", a0);
delay(1000) ;
}
}
#define ADC_FREQ 2500000
void adc_init(void)
{
volatile U32 preScaler;
int channel=0; //AIN0,对应开发板上W1可调电阻
preScaler = ADC_FREQ;
Uart_Printf("ADC conv,freq. = %dHz ",preScaler);
preScaler = 50000000/ADC_FREQ - 1; //PCLK=50M preScaler = 19
// 50MHz/(19+1)=2.5MHz
Uart_Printf("PRSCVL=PCLK/ADC_FREQ - 1=%d ",preScaler);
/*AD转换频率设置,最大频率为2.5MHz*/
rADCCON = (1<<14)|(preScaler<<6)|(channel<<3); //setup channel 1<<14使能预分频器
delay(1000); //(preScaler<<6)设置预分频值 channel<<3选择通道0
}
int ReadAdc(int channel)
{
/*开启AD转换*/
rADCCON |= 0x01; //start ADC
while(rADCCON & 0x1); //检测是否已启动转换
while(!(rADCCON & 0x8000)); //判断转换是否结束
return ( (int)rADCDAT0 & 0x3ff );//读取转换后的值
}
/*开启AD转换*/
rADCCON |= 0x01; //start ADC
while(rADCCON & 0x1); //检测是否已启动转换
while(!(rADCCON & 0x8000)); //判断转换是否结束
return ( (int)rADCDAT0 & 0x3ff );//读取转换后的值
}