📄 adcts.c
字号:
/*****************************************
Version : 0.1 (by Y.H.Lee)
Date : 2006.10.09
Description : Following points are modified from Ver.0.0
- void __irq AdcTsSepar(void) is modified to test separate X/Y position conversion function
NAME: Adcts.c
DESC: ADC & Touch screen test
HISTORY:
*****************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "def.h"
#include "option.h"
#include "2413addr.h"
#include "Console.h"
#include "System.h"
#include "adcts.h"
#define PRSCVL 19
// A/D converter freq.=50MHz/(PRSCVL+1)
// Conversion Time = 1/(Freq./5cycle)
// A/D's Max. clcok is 2.5MHz
#define REQCNT 30
#define LOOP 1
#define SepXPos (0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(1)
#define SepYPos (1<<7)|(0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(2)
int ReadAdc(int ch); //Return type is int, Declare Prototype function
void __irq AdcTsAuto(void);
void Test_TsSeparateConv(void);
void __irq AdcTsSepar(void);
void Measure_ADC_current(void);
void Test_Conversion_AIN0(void);
void Test_AdcWavIn(void);
void __irq DDone(void);
int ADCcount=0;
volatile int xdata, ydata;
volatile int dDone=0;
volatile int DataArr[1024];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void * adc_function[][2]=
{
// (void *)Test_Adc, "ADC Each Ch Test [0~7] ",
(void *)Test_AdcTs, "Touch Screen Test (Auto) ",
// (void *)Test_TsSeparateConv, "Touch Screen Test (Separate) ",
// (void *)Measure_ADC_current , "ADC Current Measure (PWRDN,OP)",
// (void *)Test_AdcWavIn , "ADC Test (Wave Input to AIN0)",
// (void *)Test_Conversion_AIN0 , "AIN0 AD Conversion ",
0,0
};
void AdcDisplayFunction(void)
{
int i;
i=0;
printf("\n\n");
while(1) { //display menu
printf("%2d:%s",i,adc_function[i][1]);
i++;
if((int)(adc_function[i][0])==0) {
printf("\n");
break;
}
//if((i%2)==0) printf("\n");
printf("\n");
}
}
void Ch15_Adcts(void)
{
int i;
while(1) {
AdcDisplayFunction();
printf("\nSelect(-1 to exit): ");
i = GetIntNum();
//printf("IN:%d.\n\n", i);
if(i==-1) break;
if(i>=0 && (i<(sizeof(adc_function)/8)) )
( (void (*)(void)) (adc_function[i][0]) )(); // execute selected function.
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Test_Adc(void) //Item #0
{
int a0=0,a1=0,a2=0,a3=0; //Initialize variables
int a4=0,a5=0,a6=0,a7=0; //Initialize variables
printf("The ADC_IN are adjusted to the following values.\n");
printf("Push any key to exit!!!\n");
printf("ADC conv. freq.=%d(Hz)\n",(int)(PCLK/(PRSCVL+1.))); // ADC Freq. = PCLK/(ADCPSR+1), ADC conversion time = 5CYCLES*(1/(ADC Freq.))
while(Uart_GetKey()==0)
{
#if 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);
printf("AIN0: %04d AIN1: %04d AIN2: %04d AIN3: %04d", a0,a1,a2,a3);
printf("AIN4: %04d AIN5: %04d AIN6: %04d AIN7: %04d \n", a4,a5,a6,a7);
#else
a2=ReadAdc(2);
printf("AIN2 : %04d\n", a2);
#endif
}
rADCCON=(0<<14)+(19<<6)+(7<<3)+(1<<2); //stand by mode to reduce power consumption
printf("rADCCON = 0x%x\n", rADCCON);
}
void Test_AdcTs(void) //Item #1
{
ADCcount=0;
rADCDLY=50000; //Normal conversion mode delay about (1/3.6864M)*50000=13.56ms
rADCCON=(1<<14)+(PRSCVL<<6); //PRSCVL En, PRSCVL Value
printf("[ADC touch screen test.]\n");
rADCTSC=0xd3; //Wfait,XP_PU,XP_Dis,XM_Dis,YP_Dis,YM_En
pISR_ADC = (int)AdcTsAuto;
rINTMSK=~BIT_ADC; //ADC Touch Screen Mask bit clear
rINTSUBMSK=~(BIT_SUB_TC);
printf("\nType any key to exit!!!\n");
printf("\nStylus Down, please...... \n");
Uart_getc();
rINTSUBMSK|=BIT_SUB_TC;
rINTMSK|=BIT_ADC;
printf("[Touch Screen Test.]\n");
}
void Test_TsSeparateConv(void) //#2
{
ADCcount=0;
rADCDLY=50000; //Normal conversion mode delay about (1/3.6864M)*50000=13.56ms
rADCCON=(1<<14)+(PRSCVL<<6); //PRSCVL En, PRSCVL Value
printf("[ADC touch screen test.]\n");
rADCTSC=0xd3; //Wfait,XP_PU,XP_Dis,XM_Dis,YP_Dis,YM_En, Auto
pISR_ADC = (int)AdcTsSepar;
rINTMSK=~BIT_ADC; //ADC Touch Screen Mask bit clear
rINTSUBMSK=~(BIT_SUB_TC);
printf("\nType any key to exit!!!\n");
printf("\nStylus Down, please...... \n");
Uart_getc();
rINTSUBMSK|=BIT_SUB_TC;
rINTMSK|=BIT_ADC;
printf("[Touch Screen Test.]\n");
}
void Measure_ADC_current(void) //#3
{
int a;
rADCCON=(1<<14)+(PRSCVL<<6)+(0<<3)+(1<<2); //setup channel, PRSCVL
rADCTSC = 0x58; //Normal ADC conversion & No TS operation
printf("Now ADC is in standby mode.\n");
printf("Press any key to change the mode.\n");
getchar();
rADCCON=(1<<14)+(PRSCVL<<6)+(0<<3); //setup channel, PRSCVL
rADCTSC = 0x58; //Normal ADC conversion & No TS operation
printf("Press any key to run converting\n");
getchar();
while(1) {
rADCCON|=0x1; //start ADC
while(!(rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high
a=rADCDAT0&0x3ff;
printf("ADC result : %d\n",a);
}
}
void Test_AdcWavIn(void) //#4
{
int i;
printf("The ADC_IN are adjusted to the following values.\n");
printf("Push any key to exit!!!\n");
printf("ADC conv. freq.=%d(Hz)\n",(int)(PCLK/(PRSCVL +1.))); // ADC Freq. = PCLK/(ADCPSR+1), ADC conversion time = 5CYCLES*(1/(ADC Freq.))
printf("Are you ready, press any key & go\n");
getchar();
printf("Capturing\n");
// rADCCON=(1<<14)|(PRSCVL<<6)|(0<<3); //setup channel, PRSCVL, AIN[0]
rADCCON=(1<<14)|(PRSCVL<<6)|(0<<3)|(1<<1); //setup channel, PRSCVL, AIN[0]
rADCTSC = rADCTSC & 0xfb; //Normal ADC conversion & No TS operation
i=0;
while(i<1025)
{
// 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
DataArr[i++]=(rADCDAT0&0x3ff);
while(!(rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high
}
for(i=1;i<1025;i++)
printf("%d\n",DataArr[i]);
rADCCON=(0<<14)+(19<<6)+(7<<3)+(1<<2); //stand by mode to reduce power consumption
printf("rADCCON = 0x%x\n", rADCCON);
}
void Test_Conversion_AIN0(void) //#5
{
int a[10000], dCnt, sum;
float ave,dev,stdev;
rADCCON=(1<<14)+(PRSCVL<<6)+(0<<3); //setup channel, PRSCVL
rADCTSC = 0x58; //Normal ADC conversion & No TS operation
#if 0
sum=0;
ave=0;
dCnt=0;
while(dCnt<10000) {
rADCCON|=0x1; //start ADC
while(!(rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high
a[dCnt]=rADCDAT0&0x3ff;
sum+=a[dCnt];
dCnt++;
}
ave=(float)sum/10000;
sum=0;
for(dCnt=0;dCnt<10000;dCnt++) {
sum+=((a[dCnt]-ave)*(a[dCnt]-ave));
}
dev=(float)sum/10000;
stdev=sqrt(dev);
printf("ADC float ave result : %f\n",ave);
printf("ADC float dev result : %f\n",dev);
printf("ADC float stdev result : %f\n",stdev);
#else
rADCCON|=0x1; //start ADC
while(!(rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high
sum=rADCDAT0&0x3ff;
printf("ADC result : %d\n",sum);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int ReadAdc(int ch)
{
int i;
static int prevCh=-1;
if(prevCh!=ch)
{
rADCCON=(1<<14)+(PRSCVL<<6)+(ch<<3); //setup channel, PRSCVL
for(i=0;i<LOOP;i++); //delay to set up the next channel
prevCh=ch;
}
rADCCON=(1<<14)+(PRSCVL<<6)+(ch<<3); //setup channel, PRSCVL
rADCTSC = rADCTSC & 0xfb; //Normal ADC conversion & No TS operation
rADCCON|=0x1; //start ADC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -