Receiving coordinate information over USART

Questions and discussions are relating to using current firmware.

sdx

Receiving coordinate information over USART

Post by sdx »

I have the TC553/852 control module and I am struggling to get the coordinates from my touches on my AVR. I have a feeling I'm getting data-type conflicts.

here's my code:

Code: Select all

usart_putc(0x05); 
usart_putc(0x01); 
wait(); //activates touchscreen 

while(1){ 
   if(!uart_buffer_empty()){ //recieved touchscreen input 
      _delay_ms(100.0); 
      input_decode(); 
   }    
} 

//decodes the input x,y from the LCD 
void input_decode(){ 
      unsigned char tempin[10]; 
      unsigned char xin[10]; 
      unsigned char yin[10]; 
      unsigned char temp; 

      int i = 0; 
      int x, y, tmp; 

      while(!uart_buffer_empty()){ 
         temp = usart_getc(); //read the buffer 
         if(temp == ','){ 
            break; 
         } 
         else{ 
            tempin[i++] = temp; 
         } 
      } 
      tmp = i; 
      for(int k = 0; k <= tmp; k++){ 
         xin[k] = tempin[i--];  //reverses the character order 
      } 

      i = 0; 

      while(!uart_buffer_empty()){ 
         temp = usart_getc(); //read the buffer 
         if(temp == ' '){ 
            break; 
         }    
         else{ 
            tempin[i++] = temp; 
         } 
      } 
      tmp = i; 
      for(int k = 0; k <= tmp; k++){ 
         yin[k] = tempin[i--];  //reverses the character order 
      } 

               //code continues to atoi the char arrays and use them as coordinates
As you can see, I'm trying to read in the data as characters; when I was in debug mode in my AVR dragon I step by stepped through the USART input from touching the screen and I was getting anywhere from 4-5characters up to 7-8 characters...

I know in the documentation its only supposed to respond with 4 bytes:
"X,Y "
'X' = byte value of x coord
',' = a char comma
'Y' = byte value of y coord
' ' = stop character....

What am I doing wrong here at I'm receiving so many bytes? Are they being sent in characters (what I assumed when the int values weren't reading in properly)

Please help!



User avatar
vivat
Site Admin
Posts: 70
Joined: Sun Nov 28, 2004 5:24 pm
First Name: Nick
Last Name: G.
Location: USA
Contact:

Re: Receiving coordinate information over USART

Post by vivat »

This was already answered once on AVR forum.
The space character is the separator. I wouldn't use scanf.

Collect characters in a buffer until a space is received, then process the string. Replace the comma with a null character, remember where the comma is and you have two separate strings that can be converted to an integer using atoi(). Then restart collecting characters again.

Or something along the lines of this:

Code: Select all

// 
// Wait for a response from the touchscreen and store the x and y coordinates in the integers pointed to by x and y 
// 
void wait_for_response(int *x, int *y) 
{ 
 char x_buffer[8],y_buffer[8],*p; 
 char no_chars; // number of characters received, to prevent buffer overflows 
 char done; 

 // Start receiving response strings 
 p=x_buffer; 
 no_chars=0; 
 done=0; 
 do 
  { 
  ch=getchar(); // assuming blocking until something is received 

  if (ch==',')  // when a comma is received the x field is complete 
   { 
   *p=0; // terminate string 
   p=y_buffer; // switch pointer to y_buffer 
   no_chars=0; 
   } 
  else if (ch==' ') // when a space is received, the y_field is complete 
   { 
   *p=0; // terminate string 
   done=1; 
   } 
  else { // In all other cases store the character in the buffer 
       if (i<8) // Make sure we don't go past end of buffer 
        { 
        *p++=ch; 
        i+=1; 
        }        
       } 
  } while (!done); 

 *x=atoi(x_buffer); 
 *y=atoi(y_buffer); 
}
Of course this codesnippet just blocks the whole processor until the touchscreen sends a complete message. In practice you could place this code inside the UART ISR, except for the atoi() of course. Or make all variables static, and when getchar() indicates there's no character bail out of the function. Also, a bit of extra error checking should be added.
Hope this helps.


Support Group,
TVI Electronics

sdx

Yes

Post by sdx »

That looks pretty similar to what I was trying to do. I was able to simply read in the string and spit it back out to the LCD and it looked like it was in the correct format, thanks!

p.s. can I get a link to that AVR forum where that was answered?



User avatar
vivat
Site Admin
Posts: 70
Joined: Sun Nov 28, 2004 5:24 pm
First Name: Nick
Last Name: G.
Location: USA
Contact:

Post by vivat »

I'm glad it worked for you. Sorry, I can't recall the link right now. Just do a search on AVR Freaks forum, and you'll find it for sure.

Good Luck!


Support Group,
TVI Electronics

sdx

Further problems - Touchscreen not responding

Post by sdx »

So, I'm no longer receiving a response when I touch the touchscreen. This occurs when its plugged into the control panel software as well as when I'm controlling it with the USART.

The controller will enter into the calibration mode, but won't respond to me hitting the corners. I can use the control panel to draw, type, etc. but it won't respond with coordinates when I enter into coordinates mode. The ribbon cable appears to be making good contact with its socket.

I've tried running the firmware update tool several times to no avail.

Any suggestions, comments, things that I should try?



User avatar
vivat
Site Admin
Posts: 70
Joined: Sun Nov 28, 2004 5:24 pm
First Name: Nick
Last Name: G.
Location: USA
Contact:

Re: Further problems - Touchscreen not responding

Post by vivat »

Please verify that your touch screen is properly connected to the J8 connector on the TC553/852 controller. Carefully pull the latch on connector J8 out. Insert the flex cable into J8 being sure it's seated, push the latch back in.

Also, please make sure that the flex cable is not bent or damaged in any place. The damage can be caused by sharp objects, excessive bending of the flex cable or excessive insertion of the flex cable's conductor into its receptacle during assembly. Some failure modes are not obvious to the naked eye.

If you have an ohmmeter, pull the flex cable out of the J8 connector and measure resistance between PIN's 1 & 3 and 2 & 4, it should show resistance. Measuring resistance between PIN's 1 & 2, 1 & 4, 3 & 2 or 3 & 4 shouldn't show any resistance (open).


Support Group,
TVI Electronics

Bosman
Posts: 1
Joined: Sat Dec 02, 2017 3:41 am
First Name: Mark
Last Name: Bosman
Location: Seattle, WA

Re: Further problems - Touchscreen not responding

Post by Bosman »

vivat wrote:
Fri May 06, 2011 6:08 pm
Also, please make sure that you get these bingo bonus codes now in any place. The damage can be caused by sharp objects, excessive bending of the flex cable or excessive insertion of the flex cable's conductor into its receptacle during assembly. Some failure modes are not obvious to the naked eye.
I've gone through about 4 cables now as they stop working if they get bent or twisted. You gotta be careful with them.



Post Reply