📄 adc.c
字号:
//====================================================================
// File Name : Adc.c
// Function : S3C2410 ADC Test
// Program : Kang, Weon Tark
// Date : May 22, 2002
// Version : 0.0
// History
// 0.0 : Programming start (March 29,2002) -> KWT
// ADC Test -> May 15, 2002 SOP
// 10/28/2004 SOP
//====================================================================
#include <string.h>
#include "2410addr.h"
#include "2410lib.h"
#include "adc.h"
#include "def.h"
#define REQCNT 100 //May 08, 2002 SOP
//#define REQCNT 600 // 10/28/2004 SOP
//#define REQCNT 3000 // 10/30/2004 SOP
//#define ADC_FREQ 2500000 //500KSPS
//#define ADC_FREQ 2000000 //400KSPS
//#define ADC_FREQ 1250000 //250KSPS
#define ADC_FREQ 111000 // Test Vector , PCLK=1MHz, Prescaler 8 11/06/2004 SOP
//Conversion Time 500KSPS : 2.0us
// 400KSPS : 2.5us
// 22KSPS : 45us
//#define LOOP 1
#define LOOP 10000
void __irq DMA0_Done(void); //Declare Prototype function
int ReadAdc(int ch); //Return type is int, Declare Prototype function
volatile U32 preScaler;
//==================================================================================
void Test_Adc(void)
{
#if 1 // 10/21/2004 SOP
int i,key;
//int a0=0,a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0; //Initialize variables
int a0=0,a1=0,a2=0,a4=0,a5=0,a7=0; //Initialize variables
int a3[REQCNT], a6[REQCNT];
Uart_Printf("[ ADC_IN Test ]\n");
Uart_Printf("0. Dispaly Count %d 1. Continued ...\n\n",REQCNT);
Uart_Printf("Selet : ");
key = Uart_GetIntNum();
Uart_Printf("\n\n");
Uart_Printf("The ADC_IN are adjusted to the following values.\n");
Uart_Printf("ADC conv. freq. = %dHz\n",ADC_FREQ);
preScaler = PCLK/ADC_FREQ -1; //PCLK:50.7MHz (PreScaler=19), PCLK:67.5MHz (PreScler=26)
//ADC conversion time = 5CYCLES*(1/(ADC Freq.)), ADC Freq. = PCLK/(ADCPSR+1)
//ADC Conversion Time = 1/(ADC Freq./5 Cycles)
Uart_Printf("A/D Converter Prescaler Value (PRSCVL)= %d\n",preScaler);
Uart_Printf("Maximum Conversion Rate = %dKSPS\n",(ADC_FREQ/5)/1000);
Uart_Printf("\nPush any key to exit!!!\n");
if (key == 0)
{
//Uart_Printf("[ AIN2 ]\n"); //May 08, 2002 SOP
Uart_Printf("[ AIN3 ]\n");
//Uart_Printf("[ AIN6 ]\n");
for(i=0;i<REQCNT;i++) //May 08, 2002 SOP
{
// a0=ReadAdc(0);
// a1=ReadAdc(1);
// a2=ReadAdc(2);
a3[i]=ReadAdc(3);
// a4=ReadAdc(4);
// a5=ReadAdc(5);
// a6[i]=ReadAdc(6);
// a7=ReadAdc(7);
//Delay(100);
//Delay(1000);
//Delay(2000);
//Uart_Printf("%04d\n",a2); //May 08, 2002 SOP
//Uart_Printf("%04d\n",a6);
}
}
else if(key == 1)
{
while(Uart_GetKey()==0)
{
a0=ReadAdc(0);
a1=ReadAdc(1);
a2=ReadAdc(2);
a3[i]=ReadAdc(3);
a4=ReadAdc(4);
a5=ReadAdc(5);
//a6[i]=ReadAdc(6);
a7=ReadAdc(7);
Uart_Printf("AIN0: %04d AIN1: %04d AIN2: %04d AIN3: %04d AIN4: %04d AIN5: %04d AIN6: %04d AIN7: %04d\n", a0,a1,a2,a3,a4,a5,a6,a7);
}
}
for(i=0;i<REQCNT;i++)
{
//Uart_Printf("%04d\n",a6[i]);
Uart_Printf("%04d\n",a3[i]);
}
rADCCON=(0<<14)|(19<<6)|(7<<3)|(1<<2); //stand by mode to reduce power consumption
Uart_Printf("\nrADCCON = 0x%x\n", rADCCON);
#else // Orginal Code
int i,key;
int a0=0,a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0; //Initialize variables
Uart_Printf("[ ADC_IN Test ]\n");
Uart_Printf("0. Dispaly Count 100 1. Continued ...\n\n");
Uart_Printf("Selet : ");
key = Uart_GetIntNum();
Uart_Printf("\n\n");
Uart_Printf("The ADC_IN are adjusted to the following values.\n");
Uart_Printf("ADC conv. freq. = %dHz\n",ADC_FREQ);
preScaler = PCLK/ADC_FREQ -1; //PCLK:50.7MHz (PreScaler=19), PCLK:67.5MHz (PreScler=26)
//ADC conversion time = 5CYCLES*(1/(ADC Freq.)), ADC Freq. = PCLK/(ADCPSR+1)
//ADC Conversion Time = 1/(ADC Freq./5 Cycles)
Uart_Printf("A/D Converter Prescaler Value (PRSCVL)= %d\n",preScaler);
Uart_Printf("Maximum Conversion Rate = %dKSPS\n",(ADC_FREQ/5)/1000);
Uart_Printf("\nPush any key to exit!!!\n");
if (key == 0)
{
//Uart_Printf("[ AIN2 ]\n"); //May 08, 2002 SOP
Uart_Printf("[ AIN6 ]\n");
for(i=0;i<REQCNT;i++) //May 08, 2002 SOP
{
// a0=ReadAdc(0);
// a1=ReadAdc(1);
// a2=ReadAdc(2);
// a3=ReadAdc(3);
// a4=ReadAdc(4);
// a5=ReadAdc(5);
a6=ReadAdc(6);
// a7=ReadAdc(7);
//Delay(1000);
//Uart_Printf("%04d\n",a2); //May 08, 2002 SOP
Uart_Printf("%04d\n",a6);
}
}
else if(key == 1)
{
while(Uart_GetKey()==0)
{
a0=ReadAdc(0);
a1=ReadAdc(1);
a2=ReadAdc(2);
a3=ReadAdc(3);
a4=ReadAdc(4);
a5=ReadAdc(5);
a6=ReadAdc(6);
a7=ReadAdc(7);
Uart_Printf("AIN0: %04d AIN1: %04d AIN2: %04d AIN3: %04d AIN4: %04d AIN5: %04d AIN6: %04d AIN7: %04d\n", a0,a1,a2,a3,a4,a5,a6,a7);
}
}
rADCCON=(0<<14)|(19<<6)|(7<<3)|(1<<2); //stand by mode to reduce power consumption
Uart_Printf("\nrADCCON = 0x%x\n", rADCCON);
#endif
}
//==================================================================================
int ReadAdc(int ch)
{
int i;
static int prevCh=-1;
rADCCON = (1<<14)|(preScaler<<6)|(ch<<3); //setup channel
if(prevCh!=ch)
{
rADCCON = (1<<14)|(preScaler<<6)|(ch<<3); //setup channel
for(i=0;i<LOOP;i++); //delay to set up the next channel
prevCh=ch;
}
rADCCON|=0x1; //start ADC
while(rADCCON & 0x1); //check if Enable_start is low
while(!(rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high
return ( (int)rADCDAT0 & 0x3ff );
}
//==================================================================================
volatile char end_test;
void Test_DMA_Adc(void)
{
unsigned int *dst,*tmp,i;
int key;
end_test=0;
pISR_DMA0=(unsigned)DMA0_Done; //Regist interrupt service routine
rINTMSK=~(BIT_DMA0); //Clear int mask bit.
Uart_Printf("Test of the 'Start by read' in ADC block.\n");
//Uart_Printf("Change the voltage of AIN2..(Sampling 100)\n");
Uart_Printf("Change the voltage of AIN6..(Sampling %d)\n",REQCNT);
dst = (unsigned int *)0x31000000; //Non-cacheable Area
tmp = dst;
for(i=0;i<REQCNT;i++)
*tmp++=i;
/***ADC init***/
Uart_Printf("The ADC_IN are adjusted to the following values.\n");
Uart_Printf("ADC conv. freq. = %dHz\n",ADC_FREQ);
preScaler = PCLK/ADC_FREQ -1; //PCLK:50.7MHz (PreScaler=19), PCLK:67.5MHz (PreScler=26)
//ADC conversion time = 5CYCLES*(1/(ADC Freq.)), ADC Freq. = PCLK/(ADCPSR+1)
//ADC Conversion Time = 1/(ADC Freq./5 Cycles)
//rADCCON=(1<<14)|(preScaler<<6)|(2<<3)|0x2; //normal,ADCPRS,AIN2,enable start by read
rADCCON=(1<<14)|(preScaler<<6)|(6<<3)|0x2; //normal,ADCPRS,AIN6,enable start by read
Uart_Printf("A/D Converter Prescaler Value (PRSCVL)= %d\n",preScaler);
Uart_Printf("Maximum Conversion Rate = %dKSPS\n",(ADC_FREQ/5)/1000);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -