⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adcts.c

📁 samsung 最新芯片2450 的测试程序.
💻 C
字号:
/*****************************************
  NAME: Adcts.c
  DESC: S3C2450 ADC & Touch screen test
  HISTORY:
  2008.3.15: Tark draft ver 1.0
 *****************************************/

#include "system.h"
#include "adcts.h"

int ReadAdc(int ch);    //Return type is int, Declare Prototype function
void __irq AdcTsSep(void);
void __irq AdcTsAuto(void);

int count=0;			// check interrupt
volatile int xdata, ydata;
int adcpsr=0;			// ADCPSR		
int method=0;			// auto/seperate/read_start/manual_start
int xtal=12000000;		// X-tal clock
int adcclock=5000000;	// ADC clock


void * func_adc_test[][2]=
{	
	(void *)Test_Adc,   			"ADC test       ",
	(void *)Test_AdcTs,   			"ADC TS test    ",
	(void *)Test_AdcTimerDrag,		"ADC drag test  ",
	0,0
};

void Test_ADC(void)
{  
	int i;

	while(1)
	{
		i=0;
		printf("\n\n");
		while(1)
		{   //display menu
	   		printf("%2d:%s",i,func_adc_test[i][1]);
	    	i++;
	    	if((int)(func_adc_test[i][0])==0)
	    	{
				printf("\n");
				break;
	    	}
	    	if((i%3)==0)
	    	printf("\n");
		}

		printf("\nSelect (\"-1\" to exit) : ");
		i = GetIntNum();
		if(i==-1) 
	   		break;		// return.
		if( (i<((sizeof(func_adc_test)-1)/8)) )	// select and execute...
	    	( (void (*)(void)) (func_adc_test[i][0]) )();
	}
	
}

void Test_Adc(void)
{
	int i;
	int a0=0,a1=0,a2=0,a3=0,a4=0; 	//Initialize variables
	int a5=0,a6=0,a7=0, a8=0, a9=0; //Initialize variables

	Delay(0);

	adcpsr=(int)((PCLK/adcclock)-1);
	rADCCON = (1<<14) | (adcpsr<<6)|(1<<3);
	printf("The ADC_IN are adjusted to the following values.\n");        
	printf("Press any key to exit!!!\n");    
	printf("ADC conv. freq.=%d(Hz)\n",(int)(PCLK/(adcpsr+1))); // ADC Freq. = 2.5MHz, ADC conversion time = 5CYCLES*(1/(ADC Freq.))

	//while(Uart_GetKey()==0)
	for(i=0;i<100;i++)
	{
		a0=ReadAdc(0);
		a1=ReadAdc(1);
		a2=ReadAdc(2);
		a3=ReadAdc(3);
		a4=ReadAdc(4);    
		a5=ReadAdc(5);
		a6=ReadAdc(6);
		a7=ReadAdc(7);    
		a8=ReadAdc(8);
		a9=ReadAdc(9);

		printf("%04d\t%04d\t%04d\t%04d\t%04d\t%04d\t%04d\t%04d\t%04d\t%04d\n",a0,a1,a2,a3,a4,a5,a6,a7,a8,a9);
//		printf("AIN0:%04d AIN1:%04d AIN2:%04d AIN3:%04d AIN4:%04d ", a0,a1,a2,a3,a4);
//		printf("AIN5:%04d AIN6:%04d AIN7:%04d AIN8:%04d AIN9:%04d \n", a5,a6,a7,a8,a9);
	}

	rADCCON &= ~(1<<14);// disable prescaler
	rADCCON |= (1<<2);	// Standby

	printf("rADCCON = 0x%x\n", rADCCON);
}

int ReadAdc(int ch)
{
	static int prevCh=-1;

	if(prevCh!=ch)
	{
		rADCMUX = ch;
		Delay(5);
		prevCh=ch;
	}
	rADCMUX = ch;
	rADCTSC = rADCTSC & 0xfb;     //Normal ADC conversion & No TS operation
	rADCCON|=0x1;   //start ADC

	while(!(rADCCON & 0x8000));	//check if EC(End of Conversion) flag is high
	return (rADCDAT0&0xfff);
}

void Test_AdcTs(void)
{ 
	printf("[ADC touch screen test.]\n");

	printf("Select measue/start method\n");
	printf("0.Manual start/ Seperate measure\n");
	printf("1.Read   start/ Seperate measurement\n");
	printf("2.Manual start/ Auto     measure\n");
	printf("3.Read   start/ Auto     measurement\n");

	method = GetIntNum();

	adcpsr=(int)((PCLK/adcclock)-1);
	printf("PSR=%d, ADC freq.=%dHz\n", adcpsr, (int)(PCLK/(adcpsr+1)));
	
	rADCDLY=(int)(xtal/1000)*2;	// 2ms delay(filter effect)
	printf("rADCDLY = %d\n", rADCDLY);

	if((method==1)|(method==3))//read start
		rADCCON = rADCCON & ~(0xffff) | (1<<14) | (adcpsr<<6) |(1<<3)|(0<<2)|(1<<1);//read start
	else
		rADCCON = rADCCON & ~(0xffff) | (1<<14) | (adcpsr<<6) |(1<<3)|(0<<2);//manual start

	printf("rADCCON = 0x%x\n", rADCCON);

	rADCTSC = 0xd3;  //Wfint,XP_PU_dis,XP_Dis,XM_Dis,YP_Dis,YM_En

	if((method==0)|(method==1))
		pISR_ADC = (int)AdcTsSep;	// Seperate measurement
	else
		pISR_ADC = (int)AdcTsAuto;	// Auto measurement

	rSUBSRCPND |= BIT_SUB_ADC;// Clear previous pending bit
	rINTMSK &=~BIT_ADC;       // ADC Touch Screen Mask bit clear
	rINTSUBMSK &=~(BIT_SUB_ADC | BIT_SUB_TC);

	printf("\nType any key to exit!!!\n");
	printf("\nWating Stylus Down... \n\n");
	
	Uart_getc();
	count=0;
	
	rINTSUBMSK |= (BIT_SUB_ADC|BIT_SUB_TC);
	rINTMSK |= (U32)BIT_ADC;
	rADCCON &= ~(1<<14);// disable prescaler
	rADCCON |= (1<<2);	// Standby
	printf("[Touch Screen Test.]\n");
}

void __irq AdcTsSep(void)
{
	rINTMSK |= (U32)BIT_ADC;	// ADC Touch Screen Mask bit set
	rINTSUBMSK |= (BIT_SUB_ADC | BIT_SUB_TC);

	if( (rSUBSRCPND & (0x1<<10)) & ((count&1)<<10) ) // ADC interrupt(end of Y-posion conversion)
	{
		rSUBSRCPND |= BIT_SUB_ADC;	// Clear ADC pending bit
		
		if((method==1)|(method==3))	// read start
			rADCCON &= ~(1<<1);		// Disable read_start

		ydata=(rADCDAT1&0xfff);		// Read Y-position Conversion data
		printf("count=%d YP=%04d\n", count++, ydata);

		rADCDLY=(int)(xtal/1000)*2;	// 2ms delay(filter effect)
		rADCTSC = 0x1d3;			// Up detect, Waiting for interrupt
	}
	else if( (rSUBSRCPND & (0x1<<10)) & (((count+1)&1)<<10) )// ADC interrupt(end of X-posion conversion)
	{
		rSUBSRCPND |= BIT_SUB_ADC;	// Clear ADC pending bit

		rADCTSC = 0x2;				// Y-position measurement state
		xdata=(rADCDAT0&0xfff);		// Read X-position Conversion data
		printf("count=%d XP=%04d\n", count++, xdata);
		
		if((method==0)|(method==2))	// manual start
			rADCCON |= 0x1;
	}
	else if(rSUBSRCPND & (0x1<<9))	// TC interrupt
	{
		rSUBSRCPND |= BIT_SUB_TC;	// Clear TC pending bit
		
		if(rADCUPDN == 0x1)			// Stylus Down
		{
			rADCUPDN &= ~(0x1);		// Clear down state
			rADCDLY=(PCLK/1000000)*500;// 500us delay(input stable)
 			rADCTSC = 0x1;			// X-position measurement state
 			
			if((method==1)|(method==3))	// read start
			{
				rADCCON |= (1<<1);		// Enable read_start
				rADCDAT0;				// Read_start
			}
			else						// Manual start
				rADCCON |= 0x1;			// Start ADC
			printf("Stylus Down\n");	
		}
		else if(rADCUPDN == 0x2)// Stylus Up
		{
			rADCUPDN &= ~(0x1<<1);	// Cleart Up state
			printf("Stylus Up\n\n");

			rADCTSC = 0xd3;			// Down detect, Waiting for interrupt
		}
	}

	ClearPending((U32)BIT_ADC);
	
	rINTMSK &= ~BIT_ADC;       //ADC Touch Screen Mask bit clear
	rINTSUBMSK &= ~(BIT_SUB_ADC | BIT_SUB_TC);
}

void __irq AdcTsAuto(void)
{
	
	rINTMSK |= (U32)BIT_ADC;		//ADC Touch Screen Mask bit clear
	rINTSUBMSK |= (BIT_SUB_ADC | BIT_SUB_TC);

	if(rSUBSRCPND & (0x1<<10))		// ADC interrupt
	{
		rSUBSRCPND |= BIT_SUB_ADC;

		if((method==1)|(method==3))	// read start
			rADCCON &= ~(1<<1);	// Disable read_start

		xdata=(rADCDAT0&0xfff);		// Read XY-position Conversion data
		ydata=(rADCDAT1&0xfff);
		printf("count=%d XP=%04d, YP=%04d\n", count++, xdata, ydata);

		rADCTSC = 0x1d3;			// Up detect, Waiting for interrupt
	}
	else if(rSUBSRCPND & (0x1<<9))	// TC interrupt
	{
		if(rADCUPDN == 0x1)			// Stylus Down
		{
			rADCUPDN &= ~(0x1);		// Clear Down state
			rSUBSRCPND |= BIT_SUB_TC;
			printf("Stylus Down\n");
			rADCDLY=(PCLK/1000000)*500;// 500us delay(input stable)
			rADCTSC = 1<<2;			// Auto conversion mode

			if((method==1)|(method==3))	// read start
			{
				rADCCON |= (1<<1);		// Enable read_start
				rADCDAT0;				// Read_start
			}
			else
				rADCCON |= 0x1;			// Start ADC
		}
		else if(rADCUPDN == 0x2)	// Stylus Up
		{
			rADCUPDN &= ~(0x1<<1);	// Clear Up state
			printf("Stylus Up\n\n");
			
			rADCTSC = 0xd3;    		// Down detect, Waiting for interrupt
		}
	}

	ClearPending((U32)BIT_ADC);

	rINTMSK &= ~BIT_ADC;       //ADC Touch Screen Mask bit clear
	rINTSUBMSK &= ~(BIT_SUB_ADC | BIT_SUB_TC);
}


void __irq TimerSampling(void);
void __irq TouchDown(void);

volatile int upDetect=0;

void Test_AdcTimerDrag(void)
{
	printf("[ADC touch screen drag test.]\n");

	// ADC initialize
	pISR_ADC = (int)TouchDown;			// Pen Down ISR
	rADCDLY=(int)(xtal/1000)*2;	// 2ms delay(filter effect)
	adcpsr=(int)((PCLK/2500000)-1);
	rADCCON = rADCCON & ~(0xffff) | (1<<14) | (adcpsr<<6) | (1<<3)|(0<<2);
	rADCUPDN = 0;	// Clear Pen state
	rADCTSC = 0xd3;  // Wfint,XP_PU_dis,XP_Dis,XM_Dis,YP_Dis,YM_En

	// Timer0 Initialize
	pISR_TIMER0 = (int)TimerSampling;	// Timer ISR for ADC sampling
	rTCFG0 = 0xff; 	// Dead zone=0, Prescaler0=255(0xff)
	rTCFG1 = 0x0; 	// Interrupt, Mux0=1/2
	rTCNTB0 = 30000;         
    rTCMPB0 = 15000;
    rTCON  = 0x1<<1; // Manual update 	

	rINTMSK &=~(BIT_ADC|BIT_TIMER0);    // ADC TS & Timer0 Interrupt Mask bit clear
	rINTSUBMSK &=~(BIT_SUB_TC);			// ADC Sub mask clear

	printf("\nType any key to exit!!!\n");
	printf("\nDrag the screen, please...... \n\n");
	
	Uart_getc();

	rINTSUBMSK|=BIT_SUB_TC;
	rINTMSK|=(U32)(BIT_ADC|BIT_TIMER0);
	rADCCON |= (1<<2);// Standby
	printf("[Finish Touch Screen Drag Test.]\n");
}

void __irq TimerSampling(void)
{
    rSRCPND = BIT_TIMER0;       //Clear pending bit
    rINTPND = BIT_TIMER0;
    rINTPND;                    //Prevent an double interrupt pending

	if( !upDetect )// Pen up check, if it is still down state, start ADC sampling
	{
		rADCTSC = (rADCTSC&~(0xf))|(6<<4)|(1<<3)|(1<<2);// auto
		rADCCON |= 0x1;	// Start
		while(rADCCON & (1<<15));//check end of conversion
		xdata=(rADCDAT0&0xfff);
		ydata=(rADCDAT1&0xfff);
		printf("XP=%04d, YP=%04d\n", xdata, ydata);    //X-position Conversion data
		rADCTSC = 0xd3;  	// Wfint,XP_PU_dis,XP_Dis,XM_Dis,YP_Dis,YM_En
		rADCTSC |= 1<<8;	// Detect UP
	}
	else
	{
		rADCUPDN &= ~(0x1<<1); 
		rTCON = 0x0; //Stop Timers
		printf("Stylus Up\n");
		upDetect=0;
		rADCTSC = 0xd3;  // Wfint,XP_PU_dis,XP_Dis,XM_Dis,YP_Dis,YM_En
	}
		
}

void __irq TouchDown(void)
{
	rINTMSK |= (U32)BIT_ADC;    // Set ADC Touch Screen Mask bit
	rINTSUBMSK |= (BIT_SUB_TC);	// Set ADC Touch Screen Sub Mask bit

	rSUBSRCPND |= BIT_SUB_TC;
	ClearPending((U32)BIT_ADC);

	if(rADCUPDN == 0x1)// Stylus Down
	{
		rADCUPDN &= ~(0x1);	// Clear down state
		printf("Stylus Down\n");

		rTCON  = 0x1 | 0x1<<3 ; //Timer 0 Start, Auto-reload
	}
	else if(rADCUPDN == 0x2)// Stylus Up
	{
		rADCUPDN &= ~(0x1<<1);

		upDetect=1;
	}

	rINTMSK &= ~BIT_ADC;       //ADC Touch Screen Mask bit clear
	rINTSUBMSK &= ~(BIT_SUB_TC);
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -