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 CCS Technical Support

reading a 4 wire touch panel
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah
Guest







PostPosted: Sun Jun 28, 2009 2:18 am     Reply with quote

The point being made to you, is that delays are _required_, between selecting an ADC channel, and reading the ADC.

Electrically, the ADC circuit, is seen as a small capacitor inside the chip, in series with the resistances of the adc multiplexer, andother parts of the input circuitry. When a reading is made, the capacitor is _disconnected_ from the external source, and it is the voltage stored in the capacitor, that is read.

If (for example), you perform:

select adc channel 1
read adc
perform some other code

select adc channel 2
read adc

The second reading, wll return something very close to the voltage present on adc channel _1_, not what is present on adc channel 2. The reason is that the capacitor will have charged to the channel 1 voltage while the 'other code' is executing, then when channel 2 is selected, no time is allowed for it to change, so you won't get the channel 2 value.
What you get on the first reading, will depend on what the adc channel was set to, before you arrive at this point.

Charge delays for the ADC are _required_, not optional.

One thing that is worth considering, is selecting the next channel, immediately _after_ reading the ADC. That way, all the time taken printing messages, is available to charge the capacitor.

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 3:21 am     Reply with quote

Quote:
If I do not put the data-direction directive in this order, it won't work
As Titelmah, I don't see another plausible explanation except missing delay for the AD measurement.

Quote:
Can I put an internal pullup on only one pin of the B port?
Yes, the weak pullup can be controlled per pin by the output register. I 'm not sure, how it's supported by CCS built-in function, but the PIC hardware can do.
sliders_alpha



Joined: 03 Mar 2008
Posts: 55

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 8:42 am     Reply with quote

indeed, it was that, a simple 2us delay betwen changing and reading the channel was needed

now i can draw on my screen (not wat i wanted to do, but still, fun) and sometime i'm getting wrong reading

example, i'm tracing a line, there is some "parasite pixel" around (1-3 for a 100 pix line)

but sometime there is one or two parasite pixel 50-60 pixel away from it

it could be a problem, it could tigger some button (i want to do a touch-interface with touch-button)

what do you think about my code?

4 wire lib
Code:

#define YM  Pin_A0            // Y- plate
#define XM  Pin_A1            // X- plate
#define YP  Pin_A2            // Y+ plate
#define XP  Pin_A3            // X+ plate

#define ADCYM  0            // Y- plate
#define ADCXM  1            // X- plate
#define ADCYP  2            // Y+ plate
#define ADCXP  3            // X+ plate

#define  TS_UNTOUCHED 35

int1 TS_touched(void)
        {
        long Ym_value, Yp_value;
       
        output_Float(YM);
        output_high(XP);
        output_low(YP);
        output_high(XM);
       
        set_adc_channel (ADCYM);
        delay_us(5);
        Ym_value = read_adc();
       
        //delay_us(4);
       
        output_Float(YP);
        output_high(XP);
        output_low(YM);
        output_high(XM);
       
        set_adc_channel (ADCYP);
        delay_us(5);
        Yp_value = read_adc();
       
        output_low(XP);
        output_low(XM);
        output_low(YP);
        output_low(YM);       
       
        if(Yp_value > TS_UNTOUCHED || Ym_value > TS_UNTOUCHED)
          {
           return(1);
          }
       return(0);
        }
   

   
int1 TS_readRaw(int16 *x, int16 *y)
   { 
      int16 buffer[5];
   int8 counter;
    if(!TS_TOUCHED()){return(1);}
   //lecture abscisse
   output_float(XP);
   output_float(XM);
   output_low(YM);
   output_high(YP);

   set_adc_channel(ADCXP);
   delay_us(4);
   counter = 0;
   while(counter<5)
      {
      buffer[counter] = read_adc();
      counter++;
      delay_us(1);
      }
   *y = (Buffer[0]+Buffer[1]+Buffer[2]+Buffer[3]+Buffer[4])/5; 
   
   //lecture ordonĂ©e
   output_float(YP);
   output_float(YM);
   output_low(XM);
   output_high(XP);

   set_adc_channel(ADCYP);
   delay_us(4);
   counter = 0;
   while(counter<5)
      {
      buffer[counter] = read_adc();
      counter++;
      delay_us(1);
      }
   *x = (Buffer[0]+Buffer[1]+Buffer[2]+Buffer[3]+Buffer[4])/5;

   output_low(XP);
   output_low(XM);
   output_low(YP);
   output_low(YM);
   
//converting raw data to display data
            if (*x<90) *x = 0;
            else *x = *x-90;
            *x = *x*0.43+2;
            if(*x>319) *x = 319;
   
            if (*y<170) *y = 0;
            else *y = *y-170;
            *y = *y*0.47;
            *y = 240-*y;
            if (*y>239) *y = 239;
           
   
   return(0);
   }


Code:

#include <16F876A.H>
#fuses HS, NOWDT, NOPROTECT
#DEVICE ADC=10
#use delay(clock = 20000000)


#define GLCD_WIDTH         320
#define GLCD_HEIGHT        240
#define GLCD_CHAR_WIDTH    8
#define GLCD_CHAR_HEIGHT   8

#define LARGE_LCD 1
#include <RA883516F876A.c>
#include <graphics.c>
#include <Touchlib.c>

void main(void)
   {
   
   int16 x, y;
   setup_adc_ports( ALL_ANALOG );
   setup_adc(ADC_CLOCK_DIV_32);
   
   output_low(YM);
   output_low(XP);
   output_low(YP);
   output_low(XM);
   
   glcd_RAinit();
   
      while(1)
         {
            {
            if(!TS_readRaw(&x,&y))
               {
               GLCD_Pixel(x,y,1);
               }
            }
         }
   }


as you can see, i'm doing an average of each coordinate in order to get a fine reading

but am i doing something wrong?

thanks
_________________
yup, i know, i don't speak english very well

CCS V4.057
Ttelmah
Guest







PostPosted: Sun Jun 28, 2009 2:02 pm     Reply with quote

2uS, is not enough.
The time needed, depends on the source impedance from your circuit, and how far the value has to change. The _least_ required, to guarantee that the capacitor can charge from one extreme to the other (so one channel sitting at one Vref, and the next at the other extreme), to get within 0.5 bit (how close you need to be, to get the 'right' reading), is for your chip, just under 10uiSec, with a source impedance of 0R. For the recommended 'maximum' of 10KR, the time rises to just under 20uSec....
Read section 11.1 of the data sheet.

Best Wishes
sliders_alpha



Joined: 03 Mar 2008
Posts: 55

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 3:16 pm     Reply with quote

Well its a little better, but it's still doing it.

And especially when I'm releasing the touch panel.

You know, when the panel isn't touched, if you read it, you will get coordinate of the middle (320/2 ; 240/2).

I guess that when I release it, the point "is going back" to the middle and my software read it and print it.

plus i got regular dead spot, dunno if it's software related (math correction wich do not use float) or hardware realated tho

I don't see how to correct that.

here you can see my "parasite pixel" and my dead spot


_________________
yup, i know, i don't speak english very well

CCS V4.057
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 3:38 pm     Reply with quote

You're hardly able to touch the panel shorter than 100 ms. So there should be plenty of time to perform multiple measurements and validate the individual readings. This is however a matter of software details rather than basic AD issues. Any touch panel decoding software has to face this problems, and apparently most developers of existing products managed them somehow.
sliders_alpha



Joined: 03 Mar 2008
Posts: 55

View user's profile Send private message

PostPosted: Mon Jun 29, 2009 12:25 am     Reply with quote

succes

i was what i though, the softwere detect a touch but then, the touch is release "tu touch cursor" is then "going back" to the center, readings are done at this moment and then the pixel il turned on.

i've corrected this just by reading TS_touched(); after doing the reading (and before turning on a pixel) if TS_touched(); does not detect a touch after his reding, then the reading is disgarded

and now on a full screnn draw i got 2-3 parasite pixel very close of their line

-for the dead spot

this is a hardware probleme, because more i press the thouch screen and more the little they are
_________________
yup, i know, i don't speak english very well

CCS V4.057
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Mon Jun 29, 2009 6:46 am     Reply with quote

Sorry to be coming in late with this, but I've been away from home for a few days.

Forget the Maxim chip etc! Microchip provides everything you need.

I had to solve this problem, and to me it screamed "Use a state machine" so that's the way I did it. It's set up as a freestanding function that is called repeatedly by some other part of the program, most likely a timer interrupt. Just about any frequency would work. The maximum rate is limited by the read time of the A/D, but for something operated by a human, there's no need to run it very fast.

To my limited mind, it seems impossible to test for contact versus no contact without using a pullup of some kind. The processor provides that feature, so I used it.

You can see that my output only has 8 bits resolution. That was enough for what I needed, but it should be easy to see how to modify the code for the full 10 bits.

I am now seeing a weak point in the code. The test for no contact may indicate that the panel is in use, but in the time between that test and the reading of position, the user may release the contact (i.e. between state 3 and state 5), and that would leave an inaccurate last reading. I don't recall seeing this problem in use, but it seems as if it should happen occasionally.

Code:

// This routine is written for a PIC16F877A in 40-pin DIP package.
// It should be called by a timer-based process. Timing is not critical and does not need to be exactly regular.
// Output will be delivered (loaded into xpos and ypos global variables) at 1/6 of the basic clock rate.
// Output of 0xFF, 0xFF indicates that there is no contact to the touch screen.
// Y coordinate is read from resistance between PORTA.3 (pin 5) and PORTB.4 (pin 37), at PORTA.1 (pin 3)
// X coordinate is read from resistance between PORTA.1 (pin 3) and PORTB.3 (pin 36), at PORTA.3 (pin 5)

int ypos, xpos;

void handle_ad(void)
{   
      static int ad_state, hold_y;

      if (++ad_state >= 6)
         ad_state = 0;

      switch(ad_state)
      {
         case 0:
            ypos = hold_y;
            xpos = adresh;                           // Read X coordinate on AD ch 3

            bit_clear(trisa, 3);                  // Make pin 5 an output (low)
            bit_clear(trisb, 4);                  // Make pin 37 an output
            bit_set(portb, 4);                     // Pin 37 is high
            bit_set(trisa, 1);                     // Make pin 3 an input (AD ch 1)
            bit_set(trisb, 3);                     // Make pin 36 high-Z

            adcon0 = 0x81 + (1 << 3);            // Set up ch 1
            break;

         case 1:
            bit_set(adcon0, 2);                     // Start A/D for ch 1
            break;

         case 2:
            hold_y = adresh;                        // Read A/D for ch 1 (vertical)

            bit_clear(trisa, 1);                  // Make pin 3 an output (low)
            bit_clear(trisb, 3);                  // Make pin 36 an output
            bit_clear(portb, 3);                  // Pin 36 is low
            bit_set(trisb, 4);                     // Pin 37 is high-Z
            bit_clear(rbpu);                        // Port B pullups on, this pulls up 37 and thus 5 also
            bit_set(trisa, 3);                     // Make pin 5 an input (AD ch 3)

            adcon0 = 0x81 + (3 << 3);            // Set up ch 3
            break;

         case 3:
            bit_set(adcon0, 2);                     // Start A/D for ch 3
            break;

         case 4:
            if (adresh > 0x80)                     // Ch 3 reading; is it pulled high by Portb pullup?
            {                                                // No contact, force repeat of states 3 and 4
               ad_state = 2;                           // Next state is 3, after increasing ad_state
               ypos = 0xFF;
               xpos = 0xFF;                           // Set "no contact" marker
               break;
            }

            bit_set(portb, 3);                     // Touchscreen is active, pin 36 is high
            bit_set(rbpu);                           // Port B pullups off
            break;

         case 5:
            bit_set(adcon0, 2);                     // Start A/D for ch 3 for a second time
            break;
      }

}
sliders_alpha



Joined: 03 Mar 2008
Posts: 55

View user's profile Send private message

PostPosted: Mon Jun 29, 2009 11:33 am     Reply with quote

Quote:
To my limited mind, it seems impossible to test for contact versus no contact without using a pullup of some kind. The processor provides that feature, so I used it.


I have successfully done that (deleting a touch without a pull-up).

Quote:

I am now seeing a weak point in the code. The test for no contact may indicate that the panel is in use, but in the time between that test and the reading of position, the user may release the contact (i.e. between state 3 and state 5), and that would leave an inaccurate last reading. I don't recall seeing this problem in use, but it seems as if it should happen occasionally.

This is exactly the problem I had. I solved it by checking after a reading if the screen was still touched, and if it wasn't then the reading was disregarded.

I don't know what is a state machine. I'm going to look into your code. Thanks =D
_________________
yup, i know, i don't speak english very well

CCS V4.057
Guest








PostPosted: Wed Jul 01, 2009 3:25 pm     Reply with quote

Just translate it to the PIC:
http://www.atmel.com/dyn/resources/prod_documents/doc8091.pdf
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Jul 01, 2009 8:06 pm     Reply with quote

Here's Wikipedia on "State machine":
http://en.wikipedia.org/wiki/Finite-state_machine

Yes, you're right about detecting contact versus no contact without pullups. I just said "Use a pullup" without checking to see if it was really needed! But the processor provides it, so why not.

Maybe it's necessary to hold the measured value until it's certain that there is still contact with the screen. Then use the result only if contact is still seen. That means, do the measurement first, and then the check, rather than the other way round as I've done it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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