工程当中需要添加 S3C2440A.s ;2440lib.c;main.c;touchpanel.c这四个程序。
主程序:
/*********************************************************************************************
* File name: main.c
* Author: ZXL
* Description: 通过中断方式,返回触摸屏上某触点的X,Y坐标,并用串口打印出来
* History: 2013.5.10
*********************************************************************************************/
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
/*
void dely(U32 tt) //本程序使用中断方式查询,故不需要延时函数
{
U32 i;
for(;tt>0;tt--)
for(i=0;i<10000;i++);
}
*/
void init()
{
U32 i;
U8 key;
U32 mpll_val=0;
i = 2 ;
switch ( i ) //设置2440的时钟频率
{
case 0:
//200
key = 12;
mpll_val = (92<<12)|(4<<4)|(1);
break;
case 1:
//300
key = 13;
mpll_val = (67<<12)|(1<<4)|(1);
break;
case 2:
//400
key = 14;
mpll_val = (92<<12)|(1<<4)|(1);
break;
case 3:
//440!!!
key = 14;
mpll_val = (102<<12)|(1<<4)|(1);
break;
default:
key = 14;
mpll_val = (92<<12)|(1<<4)|(1);
break;
}
//MDIV=92,PDIV=1,SDIV=1 通过配置MPLLCON,MPLL确定,即FCLK=400MHZ确定
ChangeMPllValue((mpll_val>>12)&0xff, (mpll_val>>4)&0x3f, mpll_val&3);
ChangeClockDivider(key, 12);
//FCLK:HCLK:PCLK = 1:1/4:1/8 =400M:100M:50M
Port_Init();
Uart_Init(0,115200);
Uart_Select(0);
Uart_Printf("\n ADC Test!!!\n");
}
int main(int argc, char **argv)
{
init();
//系统频率、端口、串口初始化
touchpanel_init(); //触摸屏初始化
while(1);
return 0;
}
触摸屏程序:
/*********************************************************************************************
* File name: touchpanel.c
* Author: ZXL
* Description: 通过中断方式,返回触摸屏上某触点的X,Y坐标,并用串口打印出来
* History: 2013.5.10
*********************************************************************************************/
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#define ADC_FREQ 2500000 //AD转换频率选为2.5MHZ
U32 preScaler; //设置AD为多少分频
U32 count = 0;
U32 xdata,ydata;
/*
void delay(int times) //本程序使用中断方式查询,故不需要延时函数
{
int i,j;
for(i=0;i<times;i++)
for(j=0;j<400;j++);
}
*/
void __irq ADC_Auto_ISR()
{
U32 saveAdcdly;
/**************** stylus down
************************/
/*检测子中断源,判断是否是触摸屏(INT_TC)中断,且触摸笔按下*/
if(rSUBSRCPND & (BIT_SUB_TC)) //检查是否完成了中断位ADC
{
if( !(rADCDAT0&0x8000))
Uart_Printf("\n触摸笔按下!\n");
else
Uart_Printf("\n触摸笔提起!\n");
}
rADCTSC |= (1<<3)|(1<<2); /*XP上拉无效,自动连续X,Y坐标转换*/
saveAdcdly=rADCDLY;
rADCDLY=40000; //正常转换模式延时 (1/50M)*40000=0.8ms
rADCCON |= 0x1; /*设置AD开始转换*/
while(rADCCON & 0x1); //检查AD转换是否已经开始
while(!(rADCCON & 0x8000)); //等待AD转换结束
while(!(rSRCPND & 0x80000000)); //检查AD中断标志位是否产生中断请求
xdata=(rADCDAT0&0x3ff);
/*获取X,Y坐标*/
ydata=(rADCDAT1&0x3ff);
ClearSubPending(BIT_SUB_TC); //清中断
//ClearPending(BIT_ADC);
rSRCPND = 0x80000000;
rINTPND = 0x80000000;
EnableSubIrq(BIT_SUB_TC); //使能中断
//EnableIrq(BIT_ADC);
rINTMSK = 0x7fffffff;
/**************** stylus up
**************************/
/*设置触摸屏为等待中断模式,等待触摸笔抬起*/
rADCTSC =0xd3; //设置为等待中断模式 Wfait,XP_PU,XP_Dis,XM_Dis,YP_Dis,YM_En
rADCTSC=rADCTSC|(1<<8);
//检查光标提起中断信号
while(1)
//检查抬笔状态
{
if(rSUBSRCPND & (BIT_SUB_TC))
//抬笔时再次触发中断
{
Uart_Printf("触摸笔提起!\n");
break;
}
}
Uart_Printf("count=%3d , XP=%4d, YP=%4d\n", count++, xdata, ydata);
rADCDLY=saveAdcdly;
rADCTSC =0xd3; /*设置触摸屏为等待中断模式,等待下次触摸笔按下*/
ClearSubPending(BIT_SUB_TC);
//清中断
//ClearPending(BIT_ADC);
rSRCPND = 0x80000000;
rINTPND = 0x80000000;
EnableSubIrq(BIT_SUB_TC); //使能中断
//EnableIrq(BIT_ADC);
rINTMSK = 0x7fffffff;
}
void touchpanel_init() /*触摸屏初始化*/
{
rADCDLY=50000; //正常转换模式延迟 (1/3.6864M)*50000=13.56ms
preScaler = ADC_FREQ;
/*设置分频时钟*/
Uart_Printf("\n AD转换频率为:%dHZ\n",preScaler);
preScaler = 50000000/ADC_FREQ - 1; //PCLK=50M 设置AD分频系数,最大频率为2.5MHz
Uart_Printf("\n AD分频系数PRSCVL为:%d\n",preScaler);
rADCCON = (1<<14)|(preScaler<<6); //ADCPRS En,PRSCVL
/*设置触摸屏为等待中断模式,等待触摸笔被按下*/
rADCTSC=0xd3; //Wfait,XP_PU,XP_Dis,XM_Dis,YP_Dis,YM_En
Uart_Printf("\n ADC touch screen test!\n");
ClearSubPending(BIT_SUB_TC);
/*清中断*/
//ClearPending(BIT_ADC);
rSRCPND = 0x80000000;
rINTPND = 0x80000000;
EnableSubIrq(BIT_SUB_TC); //使能中断
//EnableIrq(BIT_ADC);
rINTMSK = 0x7fffffff;
pISR_ADC = (U32)ADC_Auto_ISR; //将中断执行函数如入地址赋给INT_TC中断
}