📄 main.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzhou ZHIYUAN electronics Co.,LTD.
**
** http://www.zyinside.com
**
**--------------File Info-------------------------------------------------------------------------------
** File Name: main.c
** Last modified Date: 2006-01-13
** Last Version: v1.0
** Description: MagicARM2410实验箱的基础实验---触摸屏实验。
** 读取触摸屏的触摸点坐标值,然后通过串口发送到PC机显示。
** Note: 请短接实验箱上的JP1、JP7、JP8跳线,使用串口延长线把实验箱的CZ11与PC机的COM1口相连。
** PC机端使用"超级终端"接收数据,设置波持率为115200,8位数据位,1位停止位,无奇偶校验位。
**------------------------------------------------------------------------------------------------------
** Created By: 黄绍斌
** Created date: 2006-01-13
** Version: v1.0
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
********************************************************************************************************/
#include "config.h"
// 是否使用IRQ方式
#define USE_IRQ_TOUCH 1
// 定义ADC转换时钟 (2MHz)
#define ADC_FREQ (2*1000000)
// 定义显示缓冲区(中断服务程序用)
char disp_buf[50];
/*********************************************************************************************************
** Function name: DelayNS
** Descriptions: 长软件延时。
** 延时时间与系统时钟有关。
** Input: dly 延时参数,值越大,延时越久
** Output: 无
** Created by: 黄绍斌
** Created Date: 2005-12-31
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void DelayNS(uint32 dly)
{
uint32 i;
for(; dly>0; dly--)
for(i=0; i<50000; i++);
}
#if USE_IRQ_TOUCH!=0
/*********************************************************************************************************
** Function name: IRQ_AdcTouch
** Descriptions: 触摸屏中断服务程序。进行ADC转换后输出显示。
** Input: 无
** Output: 无
** Created by: 黄绍斌
** Created Date: 2006-01-13
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void IRQ_AdcTouch(void)
{
uint32 point_adc;
int i;
rINTSUBMSK |= (BIT_SUB_ADC|BIT_SUB_TC); // 禁止ADC中断和触摸屏中断
if(rADCTSC & 0x100)
{
UART_SendStr("\n ##### Stylus Up! \n");
rADCTSC &= 0x0ff; // 设置等待下笔中断
}
else
{
UART_SendStr("\n ##### Stylus Down! \n");
rADCTSC = (0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(1<<0); // 先测试x方向
for(i=0; i<10; i++);
point_adc = 0;
for(i=0;i<4;i++) // 进行4次转换操作
{
rADCCON = rADCCON | (1<<0); // 启动ADC
while(rADCCON & 0x01); // 等待ADC启动
while(!(rADCCON & 0x8000)); // 等待ADC完成
point_adc = point_adc + (rADCDAT0 & 0x3FF);
}
point_adc = point_adc>>2; // 计算平均值
sprintf(disp_buf, "X-Posion[AIN5] is %04d \n", point_adc);
UART_SendStr(disp_buf);
rADCTSC = (0<<8)|(1<<7)|(0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(2<<0); // 测试y方向
for(i=0; i<10; i++);
point_adc = 0;
for(i=0;i<4;i++) // 进行转换操作
{
rADCCON = rADCCON | (1<<0); // 启动ADC
while(rADCCON & 0x1); // 等待ADC启动
while(!(rADCCON & 0x8000)); // 等待ADC完成
point_adc = point_adc + (rADCDAT1 & 0x3FF);
}
point_adc = point_adc>>2; // 计算平均值
sprintf(disp_buf, "Y-Posion[AIN7] is %04d \n", point_adc);
UART_SendStr(disp_buf);
// 再次进入等待模式 (设置抬笔中断)
rADCTSC = (1<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3<<0);
}
// 清除中断标志
rSUBSRCPND = rSUBSRCPND | BIT_SUB_TC;
rINTSUBMSK =~ (BIT_SUB_TC);
ClearPending(BIT_ADC);
}
#else
/*********************************************************************************************************
** Function name: WaitStylusDown
** Descriptions: 等待触摸事件发生(下笔)。不清除中断标志。
** Input: 无
** Output: 无
** Created by: 黄绍斌
** Created Date: 2006-01-13
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void WaitStylusDown(void)
{
rADCTSC = (0<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3<<0);
while(!(rSUBSRCPND&BIT_SUB_TC));
}
/*********************************************************************************************************
** Function name: WaitStylusUP
** Descriptions: 等待触摸事件发生(抬笔)。清除中断标志。
** Input: 无
** Output: 无
** Created by: 黄绍斌
** Created Date: 2006-01-13
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void WaitStylusUP(void)
{
rADCTSC = (1<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3<<0);
while(!(rSUBSRCPND&BIT_SUB_TC));
rSUBSRCPND = rSUBSRCPND | BIT_SUB_TC;
}
/*********************************************************************************************************
** Function name: AdcTouch
** Descriptions: 触摸屏读取程序。进行ADC转换后通过串口输出显示。
** Input: 无
** Output: 无
** Created by: 黄绍斌
** Created Date: 2006-01-13
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void AdcTouch(void)
{
uint32 point_adc;
int i;
rADCTSC = (0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(1<<0); // 先测试x方向
for(i=0; i<100; i++);
point_adc = 0;
for(i=0;i<4;i++) // 进行转换操作
{
rADCCON = rADCCON | (1<<0); // 启动ADC
while(rADCCON & 0x01); // 等待ADC启动
while(!(rADCCON & 0x8000)); // 等待ADC完成
point_adc = point_adc + (rADCDAT0 & 0x3FF);
}
point_adc = point_adc>>2; // 计算平均值
sprintf(disp_buf, "X-Posion[AIN5] is %04d \n", point_adc);
UART_SendStr(disp_buf);
rADCTSC = (0<<8)|(1<<7)|(0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(2<<0); // 测试y方向
for(i=0; i<100; i++);
point_adc = 0;
for(i=0;i<4;i++) // 进行转换操作
{
rADCCON = rADCCON | (1<<0); // 启动ADC
while(rADCCON & 0x01); // 等待ADC启动
while(!(rADCCON & 0x8000)); // 等待ADC完成
point_adc = point_adc + (rADCDAT1 & 0x3FF);
}
point_adc = point_adc>>2; // 计算平均值
sprintf(disp_buf, "Y-Posion[AIN7] is %04d \n\n\n", point_adc);
UART_SendStr(disp_buf);
rSUBSRCPND = rSUBSRCPND | BIT_SUB_TC;
}
#endif
/*********************************************************************************************************
** Function name: TouchInit
** Descriptions: 触摸屏初始化设置(包括ADC和控制I/O)
** Input: 无
** Output: 无
** Created by: 黄绍斌
** Created Date: 2006-01-13
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void TouchInit(void)
{
// 设置nYPCON、YMON、nXPCON、XMON引脚连接
rGPGUP = rGPGUP | 0xF000;
rGPGCON = rGPGCON | 0xFF000000;
// 设置ADC切换延时值,ADC时钟频率,禁止读启动,正常工作模式,选用AIN0
rADCDLY = 20000;
rADCCON = (1<<14)|((PCLK/ADC_FREQ - 1)<<6)|(0<<3)|(0<<2)|(0<<1)|(0<<0);
// 使能XP端的上拉电阻,YM=GND,YP->AIN5,XM高阻,XP->AIN7,等待模式 (设置下笔中断)
rADCTSC = (0<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3<<0);
#if USE_IRQ_TOUCH!=0
// 设置中断服务程序,允许中断
VICVectAddr[31] = (uint32) IRQ_AdcTouch;
rINTMSK = ~BIT_ADC;
rINTSUBMSK = ~BIT_SUB_TC;
#endif
}
/*********************************************************************************************************
** Function name: main
** Descriptions: 读取触摸屏的触摸点坐标值,然后通过串口发送到PC机显示。
** Input: 无
** Output: 系统返回值0
** Created by: 黄绍斌
** Created Date: 2005-12-31
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int main(void)
{
DispDesktop(); // 显示桌面背景图片
UART_Select(0); // 选用UART0
UART_Init(); // 初始化UART0
UART_SendStr("Touch Screen Test. \n");
UART_SendStr("Separate X/Y position conversion mode test. \n");
TouchInit(); // 初始化触摸屏(ADC)
#if USE_IRQ_TOUCH!=0
IRQEnable(); // 使能IRQ中断(CPSR)
while(1);
#else
while(1)
{
WaitStylusDown();
AdcTouch();
WaitStylusUP();
DelayNS(1);
}
#endif
return(0);
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -