View previous topic :: View next topic |
Author |
Message |
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
4-Wire resistive touch driver |
Posted: Thu Mar 08, 2012 3:51 am |
|
|
Code: |
#ifndef RTOUCH_H
#define RTOUCH_H
///////////////////////////////////////////////////////////////////////////////
#bit XpIO = PORTA.0
#bit YpIO = PORTA.1
#bit XnIO = PORTA.2
#bit YnIO = PORTA.3
#bit YpPU = PORTA.6 //pullup source (O)
#bit RTsns = PORTB.0 //sense pin (I)
#bit XpTrs = TRISA.0
#bit YpTrs = TRISA.1
#bit XnTrs = TRISA.2
#bit YnTrs = TRISA.3
#bit YpPUTrs = TRISA.6
#bit RTsnsTrs = TRISB.0
#define XpADC sAN0
#define YpADC sAN1
#define XnADC sAN2
#define YnADC sAN3
#define XpCh 0
#define YpCh 1
#define XnCh 2
#define YnCh 3
///////////////////////////////////////////////////////////////////////////////
#define DriveXpLow() {XpTrs=0; XpIO=0;}
#define DriveXpHigh() {XpTrs=0; XpIO=1;}
#define FLoatXp() XpTrs=1
#define DriveXnLow() {XnTrs=0; XnIO=0;}
#define DriveXnHigh() {XnTrs=0; XnIO=1;}
#define FLoatXn() XnTrs=1
#define DriveYpLow() {YpTrs=0; YpIO=0;}
#define DriveYpHigh() {YpTrs=0; YpIO=1;}
#define FLoatYp() YpTrs=1
#define DriveYnLow() {YnTrs=0; YnIO=0;}
#define DriveYnHigh() {YnTrs=0; YnIO=1;}
#define FLoatYn() YnTrs=1
#define EnableYpPU() {YpPUTrs=0; YpPU=1;}
#define DisableYpPU() YpPUTrs=1
#define RTouched() (RTsns==0)
#define RT_kbhit() RTouched()
///////////////////////////////////////////////////////////////////////////////
typedef struct _RT_Coordinate
{
int16 X;
int16 Y;
} RT_Coordinate;
///////////////////////////////////////////////////////////////////////////////
void InitRtouch()
{
FLoatYp();
FLoatXp();
FLoatYn();
DriveXnLow();
EnableYpPU();
}
///////////////////////////////////////////////////////////////////////////////
int16 ReadYRaw()
{
int16 p, n;
// Read Xp
FloatXp();
setup_adc_ports(XpADC);
set_adc_channel(XpCh);
FloatXn();
DriveYpHigh();
DriveYnLow();
delay_ms(5); //tunable
p = read_adc();
// Read Xn
FloatXn();
setup_adc_ports(XnADC);
set_adc_channel(XnCh);
//DriveYpLow();
//DriveYnHigh();
delay_ms(5); //tunable
n = read_adc();
return ((n+p) >> 1); //return average
}
///////////////////////////////////////////////////////////////////////////////
int16 ReadXRaw()
{
int16 p, n;
// Read Yp
FloatYp();
setup_adc_ports(YpADC);
set_adc_channel(YpCh);
FloatYn();
DriveXpHigh();
DriveXnLow();
delay_ms(5); //tunable
p = read_adc();
// Read Yn
FloatYn();
setup_adc_ports(YnADC);
set_adc_channel(YnCh);
//DriveXpLow();
//DriveXnHigh();
delay_ms(5); //tunable
n = read_adc();
return ((n+p) >> 1); //return average
}
///////////////////////////////////////////////////////////////////////////////
RT_Coordinate GetRTRawXY(void)
{
RT_Coordinate localXY={0,0};
InitRtouch();
while(!RTouched()); //Not touched yet?
DisableYpPU();
while (RTouched())
{
localXY.X = ReadXRaw();
localXY.Y = ReadYRaw();
#ifdef _DEBUG_
printf("\fX: %Lu Y: %Lu", localXY.X, localXY.Y);
#endif
InitRtouch();
delay_ms(250); //tunable
}
return localXY;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#endif
|
Circuit:
[img]
http://myr.smoothdesign.net/downloads/rtouch.bmp
[/img]
Usage:
Code: |
RT_Coordinate XY;
if (RT_kbhit())
{
XY = GetRTRawXY();
}
|
|
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
Updates |
Posted: Thu Aug 02, 2012 5:13 am |
|
|
Here's an update:
Info: bug fixes, touch detection selection (on push or on release), etc...
Code: |
#define MAXPOINTS 4
#define MAXPOINTSMSK MAXPOINTS-1
#define PICKPOINTS (MAXPOINTS/2)
RT_Coordinate GetRTRawXY(void)
{
RT_Coordinate localXY={INVALIDXY,INVALIDXY};
signed int16 XY[MAXPOINTS][2];
int8 PointPtr=0, Tcount=0;
InitRtouch();
while(!RTouched()); //Not touched yet?
do {
DisableYpPU();
XY[PointPtr][0] = ReadXRaw();
XY[PointPtr][1] = ReadYRaw();
#ifdef _DEBUG_TC_
//fprintf(DEBUG_PORT, "\f%u - X: %Ld Y: %Ld\n\r", PointPtr, XY[PointPtr][0], XY[PointPtr][1]);
#endif
PointPtr = (PointPtr+1) & MAXPOINTSMSK; //circular buffer
InitRtouch();
delay_ms(15); //tunable
Tcount++;
restart_wdt(); //reset WDT
#ifdef RETURN_ON_TOUCH
}while (Tcount < MAXPOINTS);
#else
}while (RTouched());
if (Tcount >= MAXPOINTS)
#endif
{
//extract sample delayed by PICKPOINTS readings
PointPtr = (PointPtr - PICKPOINTS) & MAXPOINTSMSK;
LocalXY.X = XY[PointPtr][0];
LocalXY.Y = XY[PointPtr][1];
}
#ifdef _DEBUG_TC_
fprintf(DEBUG_PORT, "X: %Ld Y: %Ld\n\r", LocalXY.X, LocalXY.Y);
#endif
return localXY;
}
|
Usage:
Code: |
RT_Coordinate XY;
if (RT_kbhit())
{
XY = GetRTRawXY();
//Beep();
#ifdef RETURN_ON_TOUCH
while(RT_kbhit());
#endif
}
|
|
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Wed Sep 18, 2013 11:06 am |
|
|
Dear PICoHolic,
Hi,
were you able to use this program completely?
for example could you draw any picture or write anything you want every where in your LCD?
I have used another circuit that I have design it by myself because I could not do these with the circuit you mentioned .I have added it some ICs!
before I do it the X and Y were related together. but now they are working great _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Wed Sep 18, 2013 2:20 pm |
|
|
Hello Mohammad,
This is a working code and it has nothing to do with LCDs. It's just a TOUCH PANEL driver.
The driver reads RAW data, in order to map it to an LCD you need to calibrate it.
Cheers |
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Wed Sep 18, 2013 3:17 pm |
|
|
Well , my problem with this circuit was : increasing y with the increasing of x (and vice versa ). I never understand what was the problem but the problem solved when I change the circuit.
I watched this problem when I made a painting program for my LCD.
then I decided to print x and y on LCD.
the output for four corner of LCD should be below (after calibration):
(1,1)-------------------------------------------------(128,1)
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
(1,240)---------------------------------------------(128,240)
but it was something this:
(1,1)-------------------------------------------------(128,10)
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------
(15,240)---------------------------------------------(140,270)
So you say the circuit (simple circuit with direct connecting touch to ADC) have no problem, yes? _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Thu Sep 19, 2013 2:10 am |
|
|
Yes no problem |
|
|
|