1.其中的一种是2440test中自带的Touchpanel.c,但是其中的很多代码可以省略,修改后如下:
void __irq AdcTsAuto(void)
{
rADCTSC=0x0C; //设置ADCTSC进入自动(连续)x/y轴坐标转换模式
rADCCON|=0x1; //启动AD转换
while(rADCCON & 0x1); //检测AD转换开始有效位是否是低电平,当真正开始转换时,位[0]会自动清0,加上有助于去抖
while(!(rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high, This line is necessary~!!,检测是否转换完,加上有助于去抖
while(!(rSUBSRCPND & (BIT_SUB_ADC))); //检测转换完后产生的AD中断信号,仅用这一个while作为判断ADC转换结束结束中断很不好,去抖不好
xdata=(rADCDAT0&0x3ff); //获取x,y的坐标
ydata=(rADCDAT1&0x3ff);
//YH 0627, To check Stylus Up Interrupt.
rSUBSRCPND|=BIT_SUB_TC; //对中断未决寄存器相应的ADC、TC(按下)中断位清0,避免多次中断
rSUBSRCPND|=BIT_SUB_ADC;
rSRCPND|=BIT_ADC;
rINTPND|=BIT_ADC;
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)) //如果Pen-up中断产生
{
Uart_Printf("Stylus Up Interrupt~!\n");
break; //if Stylus is up(1) state
}
}
Uart_Printf("count=d XP=d, YP=d\n", count++, xdata, ydata); //X-position Conversion data
rADCTSC=rADCTSC&~(1<<8); // Detect stylus Down interrupt signal.
rSUBSRCPND|=BIT_SUB_TC; //清除TC(Pen-up)中断未决位
rSRCPND|=BIT_ADC;
rINTPND|=BIT_ADC;
}
void Test_Touchpanel(void)
{
//普通转换模式延迟(1/3.6864M)*50000=13.56ms,x,y转换延迟(1/50M)*50000=1ms
rADCDLY=50000;
//设置A/D转换预分频使能,分频值为19
rADCCON=(1<<14)+(19<<6);
Uart_Printf("ADC touch screen test\n");
rADCTSC=0xd3; //等待模式设置,XP_PU,XP_Dis,XM_Dis,YP_Dis,YM_En
pISR_ADC = (int)AdcTsAuto; //设置中断处理函数函数入口,中断向量注册
rINTMSK &= ~BIT_ADC; //不屏蔽ADC中断位及子中断位
rINTSUBMSK &= ~(BIT_SUB_TC);
rINTSUBMSK &= ~(BIT_SUB_ADC);
Uart_Printf("\nType any key to exit!!!\n");
Uart_Printf("\nStylus Down, please...... \n");
Uart_Getch(); //等待键盘输入
rINTSUBMSK|=BIT_SUB_TC; //屏蔽使能ADC中断位及子中断位
rINTSUBMSK|=BIT_SUB_ADC;
rINTMSK|=BIT_ADC;
Uart_Printf("Touch Screen Test is Finished!!!\n");
}
这个代码时间处理INC_ADC和INC_TC的两个信号放在一个中断函数中进行处理,注意一定要在处理完后对相应的未决寄存器清零。
2.另一种是基于两种信号分别处理的信号,贴码如下:
void ADC_Handle(void)
{
xdata=(rADCDAT0&0x3FF); //获取x的坐标
ydata=(rADCDAT1&0x3FF); //获取y的坐标
Uart_Printf("count=d XP=d, YP=d\n", count++, xdata, ydata); //在超级终端显示触屏次数以及x,y的坐标
rADCTSC=0xD3; //转换完后进入等待中断模式,等待触摸屏被松开的中断
rADCTSC|=(1<<8); //设置此时检测触点松开中断信号,Detect Stylus Up Interrupt Signal
rSUBSRCPND|=BIT_SUB_ADC; //对中断未决寄存器相应的ADC中断位清0,避免多次中断
rSRCPND|=BIT_ADC;
rINTPND|=BIT_ADC;
}
void TC_Handle(void)
{
if(rADCDAT0&0x8000) //在等待中断模式时,若触屏被松开,Stylus up
{
Uart_Printf("\nStylus up\n");
rADCTSC=0xD3; //检测到松开后,继续进入等待中断模式,等待触摸屏被按下
}
else //若触摸屏被按下
{
Uart_Printf("\nStylus dowm\n");
rADCTSC = 0x0C; //设置ADCTSC进入自动(连续)x/y轴坐标转换模式
rADCCON |=0x01; //启动AD转换
}
// while(rADCCON&0x1);
// whule(rADCCON&0x8000);
rSUBSRCPND|=BIT_SUB_TC; //对中断未决寄存器相应的TC中断位清0,避免多次中断
rSRCPND|=BIT_ADC;
rINTPND|=BIT_ADC;
}
void __irq AdcTsHandle(void)
{
if(rSUBSRCPND & BIT_SUB_ADC) //若ADC转换完发生的中断信号,则进入ADC_Handle()函数
ADC_Handle();
if(rSUBSRCPND & BIT_SUB_TC) //若触屏被按下或松开时产生的中断信号,则进入TC_Handle()函数
TC_Handle();
}
void Test_Touchpanel(void)
{
//普通转换模式延迟(1/3.6864M)*50000=13.56ms,x,y转换延迟(1/50M)*50000=1ms
rADCDLY=50000;
//设置A/D转换预分频使能,分频值为19
rADCCON=(1<<14)+(19<<6);
Uart_Printf("ADC touch screen test\n");
rADCTSC=0xd3; //等待模式设置,XP_PU,XP_Dis,XM_Dis,YP_Dis,YM_En
pISR_ADC = (int)AdcTsHandle; //设置中断处理函数函数入口,中断向量注册
rINTMSK &= ~BIT_ADC; //不屏蔽ADC中断位及子中断位
rINTSUBMSK &= ~(BIT_SUB_TC);
rINTSUBMSK &= ~(BIT_SUB_ADC);
Uart_Printf("\nType any key to exit!!!\n");
Uart_Printf("\nStylus Down, please...... \n");
Uart_Getch(); //等待键盘输入
rINTSUBMSK|=BIT_SUB_TC; //屏蔽使能ADC中断位及子中断位
rINTSUBMSK|=BIT_SUB_ADC;
rINTMSK|=BIT_ADC;
Uart_Printf("Touch Screen Test is Finished!!!\n");
}
总结:分析可得,第二种方案更具可操作性,装订仪即时基于此进行显示
FROM:http://blog.sina.com.cn/s/blog_5a150227010156ab.html