CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

4-Wire resistive touch driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

4-Wire resistive touch driver
PostPosted: Thu Mar 08, 2012 3:51 am     Reply with quote

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

View user's profile Send private message

Updates
PostPosted: Thu Aug 02, 2012 5:13 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 18, 2013 11:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 18, 2013 2:20 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 18, 2013 3:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 19, 2013 2:10 am     Reply with quote

Yes no problem
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group