📄 touchscreen.c
字号:
/*********************************************************************************************
* File: touchscreen.c
* Author: embest
* Desc: LCD touch screen control function
* History:
*********************************************************************************************/
#include "44b.h"
#include "44blib.h"
#include "touchscreen.h"
#include "lcd.h"
/*------------------------------------------------------------------------------------------*/
/* global variables */
/*------------------------------------------------------------------------------------------*/
unsigned int f_unMaxX, f_unMaxY;
unsigned int f_unMinX, f_unMinY;
unsigned int f_unTouched;
unsigned int f_unPosX, f_unPosY;
void lcd_rectangle(void)
{
lcd_init(); // initial LCD controller
lcd_clr(); // clear screen
/* draw rectangle pattern */
lcd_draw_box(0,0,80,60,15);
lcd_draw_box(80,0,160,60,15);
lcd_draw_box(160,0,240,60,15);
lcd_draw_box(240,0,320,60,15);
lcd_draw_box(0,60,80,120,15);
lcd_draw_box(80,60,160,120,15);
lcd_draw_box(160,60,240,120,15);
lcd_draw_box(240,60,320,120,15);
lcd_draw_box(0,120,80,180,15);
lcd_draw_box(80,120,160,180,15);
lcd_draw_box(160,120,240,180,15);
lcd_draw_box(240,120,320,180,15);
lcd_draw_box(0,180,80,240,15);
lcd_draw_box(80,180,160,240,15);
lcd_draw_box(160,180,240,240,15);
lcd_draw_box(240,180,320,240,15);
/* output ASCII symbol */
lcd_disp_ascii6x8(37,26,RED,"0");
lcd_disp_ascii6x8(117,26,RED,"1");
lcd_disp_ascii6x8(197,26,RED,"2");
lcd_disp_ascii6x8(277,26,RED,"3");
lcd_disp_ascii6x8(37,86,RED,"4");
//lcd_disp_ascii6x8(117,86,RED,"5");
//lcd_disp_ascii6x8(197,86,RED,"6");
lcd_disp_ascii6x8(277,86,RED,"6");
lcd_disp_ascii6x8(37,146,RED,"7");
lcd_disp_ascii6x8(117,146,RED,"8");
lcd_disp_ascii6x8(197,146,RED,"9");
lcd_disp_ascii6x8(277,146,RED,"A");
lcd_disp_ascii6x8(37,206,RED,"B");
lcd_disp_ascii6x8(117,206,RED,"C");
lcd_disp_ascii6x8(197,206,RED,"D");
lcd_disp_ascii6x8(277,206,RED,"E");
}
/*********************************************************************************************
* name: touchscreen_int
* func: TouchScreen interrupt handler function
* para: none
* ret: none
* modify:
* comment:
********************************************************************************************/
void touchscreen_test(void)
{
unsigned int unX=0, unY=0;
unsigned int unTmpX, unTmpY;
unsigned char sbuf[20];
touchscreen_init();
lcd_rectangle();
uart_printf("Please touch LCD's left up corner:");
f_unTouched = 0;
while(f_unTouched == 0);
uart_printf("left=%04d, up=%04d\n", f_unPosX, f_unPosY);
unTmpX = f_unPosX;
unTmpY = f_unPosY;
uart_printf("Please touch LCD's right bottom corner:");
f_unTouched = 0;
while(f_unTouched == 0);
uart_printf("right=%04d, bottom=%04d\n", f_unPosX, f_unPosY);
f_unMaxX = f_unPosX>unTmpX? f_unPosX : unTmpX;
f_unMaxY = f_unPosY>unTmpY? f_unPosY : unTmpY;
f_unMinX = f_unPosX>unTmpX? unTmpX : f_unPosX;
f_unMinY = f_unPosY>unTmpY? unTmpY : f_unPosY;
for(;;)
{
f_unTouched = 0;
while(f_unTouched == 0);
//uart_printf("PosX=%04d, PosY=%04d\n", f_unPosX, f_unPosY);
if(f_unPosX>f_unMaxX || f_unPosX<f_unMinX || f_unPosY>f_unMaxY || f_unPosY<f_unMinY)
continue;
// lcd_clr_rect(unX+2,unY+2,unX+40,unY+20,WHITE);
unX = (320*(f_unPosX - f_unMinX))/(f_unMaxX - f_unMinX);
unY = (240*(f_unPosY - f_unMinY))/(f_unMaxY - f_unMinY);
uart_printf("X=%04d, Y=%04d\n", unX, unY);
// lcd_clr_rect(81,121,239,59,BLACK);
// lcd_clr_rect(82,122,238,58,GREEN);
lcd_clr_rect(82,62,238,118,GREEN);
sprintf(sbuf," X=%04d, Y=%04d\n", unX, unY);
lcd_disp_ascii8x16(90,70,RED,sbuf);
}
while(1);
}
/*********************************************************************************************
* name: touchscreen_init
* func: initialize TouchScreen
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void touchscreen_init(void)
{
#ifndef S3CEV40
// TSPX(GPC1_Q4(-)) TSPY(GPC3_Q3(-)) TSMY(GPC0_Q2(-)) TSMX(GPC2_Q1(+))
// 1 1 0 1
rPCONC = (rPCONC & 0xffffff00) | 0x55;
rPUPC = (rPUPE & 0xfff0); // Pull up
rPDATC = (rPDATC & 0xfff0 ) | 0xe; // should be enabled
#else
// S3CEV40
// TSPX(GPE4_Q4(+)) TSPY(GPE5_Q3(-)) TSMY(GPE6_Q2(+)) TSMX(GPE7_Q1(-))
// 0 1 1 0
rPCONE = (rPCONE & 0x300ff) | 0x5500;
rPUPE = (rPUPE & 0xF);
rPDATE = 0xb8;
#endif
delay(100);
// set interrupt
#ifndef S3CEV40
rPUPG = (rPUPG & 0xFE) | 0x1;
pISR_EINT0=(int)touchscreen_int; // set interrupt handler
rEXTINT = (rEXTINT & 0x7FFFFFF0) | 0x2; // falling edge trigger
rI_ISPC |= BIT_EINT0; // clear pending_bit
rINTMSK =~(BIT_GLOBAL|BIT_EINT0);
#else
pISR_EINT2=(int)touchscreen_int; // set interrupt handler
rEXTINT = (rEXTINT & 0x7FFFF0FF) | 0x200; // falling edge trigger
rI_ISPC |= BIT_EINT2; // clear pending_bit
rINTMSK =~(BIT_GLOBAL|BIT_EINT2);
#endif
rCLKCON = (rCLKCON & 0x6FFF) | 0x1000; // enable clock
rADCPSR = 24; // A/D prescaler
}
/*********************************************************************************************
* name: touchscreen_close
* func: close TouchScreen
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void touchscreen_close(void)
{
// Mask interrupt
rINTMSK |=BIT_GLOBAL|BIT_EINT0;
pISR_EINT0 = (int)NULL;
}
/*********************************************************************************************
* name: touchscreen_int
* func: TouchScreen interrupt handler function
* para: none
* ret: none
* modify:
* comment:
********************************************************************************************/
void touchscreen_int(void)
{
UINT32T unPointX[5], unPointY[6];
UINT32T unPosX, unPosY;
rINTMSK |=BIT_EINT0;
int i;
delay(500);
#ifndef S3CEV40
// <X-Position Read>
// TSPX(GPC1_Q4(+)) TSPY(GPC3_Q3(-)) TSMY(GPC0_Q2(+)) TSMX(GPC2_Q1(-))
// 0 1 1 0
rPDATC = (rPDATC & 0xfff0 ) | 0x9;
rADCCON= 0x0014; // AIN5
#else
// TSPX(GPE4_Q4(+)) TSPY(GPE5_Q3(-)) TSMY(GPE6_Q2(+)) TSMX(GPE7_Q1(-))
// 0 1 1 0
rPDATE =0x68;
rADCCON=0x1<<2; // AIN1
#endif
delay(100); // delay to set up the next channel
for(i=0; i<5; i++)
{
rADCCON |= 0x1; // Start X-position A/D conversion
while(rADCCON & 0x1 == 1); // Check if AD conversion starts
while((rADCCON & 0x40) == 0); // Check end of AD conversion
unPointX[i] = (0x3ff&rADCDAT);
}
// read X-position average value
unPosX = (unPointX[0]+unPointX[1]+unPointX[2]+unPointX[3]+unPointX[4])/5;
f_unPosX = unPosX;
#ifndef S3CEV40
// <Y-Position Read>
// TSPX(GPC1_Q4(-)) TSPY(GPC3_Q3(+)) TSMY(GPC0_Q2(-)) TSMX(GPC2_Q1(+))
// 1 0 0 1
rPDATC = (rPDATC & 0xfff0 ) | 0x6;
rADCCON= 0x001C; // AIN70
#else
// TSPX(GPE4_Q4(-)) TSPY(GPE5_Q3(+)) TSMY(GPE6_Q2(-)) TSMX(GPE7_Q1(+))
// 1 0 0 1
rPDATE =0x98;
rADCCON=0x0<<2; // AIN0
#endif
delay(100); // delay to set up the next channel
for(i=0; i<5; i++)
{
rADCCON |= 0x1; // Start Y-position conversion
while(rADCCON & 0x1 == 1); // Check if AD conversion starts
while((rADCCON & 0x40) == 0); // Check end of AD conversion
unPointY[i] = (0x3ff&rADCDAT);
}
// read Y-position average value
unPosY = (unPointY[0]+unPointY[1]+unPointY[2]+unPointY[3]+unPointY[4])/5;
f_unPosY = unPosY;
#ifndef S3CEV40
rPDATC = (rPDATC & 0xfff0 ) | 0xe; // should be enabled
#else
rPDATE = 0xb8; // should be enabled
#endif
delay(1000);
f_unTouched = 1;
delay(1000);
rI_ISPC |= BIT_EINT0; // clear pending_bit
rINTMSK =~(BIT_GLOBAL|BIT_EINT0);
#ifndef S3CEV40
rI_ISPC |= BIT_EINT0; // clear pending_bit
#else
rI_ISPC |= BIT_EINT2; // clear pending_bit
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -