触摸屏工作原理
触摸屏分为:电阻式,电容式,声表面波式,红外线扫描式等类型,使用的最多的是4线电阻式的触摸屏。
触摸屏工作流程
(1)、设置触摸屏接口为等待中断模式,等待触摸屏被按下。
(2)、如果中断(INT_TC)发生,选择X,Y坐标转换模式(X/Y坐标分别转换模式,X/Y坐标自动转换模式),启动AD转换。
(3)、当AD转换完毕后,通过中断(INT_ADC),获取X/Y坐标,ADCDAT0 bit[9:0]-x坐标,ADCDAT1 bit[9:0]-Y坐标。
(4)、设置触摸屏接口为等待中断模式,等待触摸笔离开触摸屏。
(5)、返回步骤1,等待下次触摸笔被按下。
实验主要代码
main,c
/*主函数*/
int Main(void)
{
int Scom=0;
Set_Clk();
Uart_Init(0,115200);
Uart_Select(Scom);
Test_Touchpanel();
while(1);
return 0;
}
/*触摸屏初始化*/
void Test_Touchpanel(void)
{
rADCDLY=50000; //Normal conversion mode delay about (1/3.6864M)*50000=13.56ms
/*设置AD转频率*/
preScaler = ADC_FREQ;
preScaler = 50000000/ADC_FREQ - 1; //PCLK=50M
rADCCON = (1<<14)|(preScaler<<6); //ADCPRS En,PRSCVL
/*设置触摸屏为等待中断模式,等待触摸笔被按下*/
rADCTSC=0xd3; //Wfait,XP_PU,XP_Dis,XM_Dis,YP_Dis,YM_En
/*clear irq*/
rSRCPND = 0x80000000;
rINTPND = 0x80000000;
ClearSubPending(BIT_SUB_TC); //子中断也要清除
pISR_ADC = (U32)AdcTsAuto;
/*enable INT_TC irq*/
rINTMSK = 0x7fffffff;
EnableSubIrq(BIT_SUB_TC);
}
/*************************************************
Function name: AdcTsAuto
Parameter : void
Description : 中断服务程序
Return : void
Argument : void
Autor & date : Daniel
**************************************************/
static void __irq AdcTsAuto(void)
{
U32 saveAdcdly;
/****************stylus down************************/
/*检测子中断源,判断是否是INT_TC中断,且触摸笔按下*/
if(rSUBSRCPND & (BIT_SUB_TC))
{
if( !(rADCDAT0&0x8000))
Uart_Printf("\nStylus down\n");
else
Uart_Printf("\nStylus up\n");
}
/*pull-up disable,自动连续X,Y坐标转换*/
rADCTSC = (1<<3)|(1<<2);
saveAdcdly=rADCDLY;
rADCDLY=40000; //Normal conversion mode delay about (1/50M)*40000=0.8ms
/*开始AD转换*/
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, This line is necessary~!!
while(!(rSRCPND & 0x80000000)); //check if ADC is finished with interrupt bit
/*获取X,Y坐标*/
xdata=(rADCDAT0&0x3ff);
ydata=(rADCDAT1&0x3ff);
ClearSubPending(BIT_SUB_TC);
//ClearPending(BIT_ADC);
rSRCPND = 0x80000000;
rINTPND = 0x80000000;
EnableSubIrq(BIT_SUB_TC);
//EnableIrq(BIT_ADC);
rINTMSK = 0x7fffffff;
/****************stylus down************************/
/****************stylus up**************************/
/*设置触摸屏为等待中断模式,等待触摸笔抬起*/
rADCTSC =0xd3; //Waiting for interrupt
rADCTSC=rADCTSC|(1<<8); // Detect stylus up interrupt signal.
while(1) //to check Pen-up state
{
if(rSUBSRCPND & (BIT_SUB_TC)) //check if ADC is finished with interrupt bit
{
Uart_Printf("Stylus Up Interrupt~!\n");
break; //if Stylus is up(1) state
}
}
/****************stylus up**************************/
Uart_Printf("count=%03d XP=%04d, YP=%04d\n", count++, xdata, ydata);
rADCDLY=saveAdcdly;
/*设置触摸屏为等待中断模式,等待下次触摸笔按下*/
rADCTSC =0xd3; //Waiting for interrupt
ClearSubPending(BIT_SUB_TC);
//ClearPending(BIT_ADC);
rSRCPND = 0x80000000;
rINTPND = 0x80000000;
EnableSubIrq(BIT_SUB_TC);
//EnableIrq(BIT_ADC);
rINTMSK = 0x7fffffff;
}