On the dangers of ignoring the basics

Just a quick post on what happens when you miss the forest for the trees. I’ve been having trouble getting the MTCH6102 to work well with my touchpad setup. I was having trouble reading the TOUCHX,TOUCHY,TOUCHLSB registers and displaying them on the LCD.

At first I thought I wasn’t converting the values correctly, but that wasn’t it. The code below shows how to get an 8 bit and a 4 bit number into a twelve bit variable. This code is correct, if a bit wasteful.

   uint16_t touchx,touchy;
   static volatile uint8_t buff[4],touchx_bf[4],touchy_bf[4];
   buff[0]=twi_read(0x11);
   buff[1]=twi_read(0x12);
   buff[2]=twi_read(0x13);
   
   touchx=((buff[0] << 4) | ((0xF0 & buff[2]) >> 4));
   touchy=((buff[1] << 4) | ((0x0F & buff[2])));
 
   itoa(touchx,touchx_bf,10);
   itoa(touchy,touchy_bf,10);

Then I thought maybe I was running out of memory and overflowing. But there’s no reason to only have this happen when you touch a certain part of the touchpad (duh).

Then I though maybe there’s some kind of data corruption because of my bad layout, so I looked with my oscilloscope at SCL and SDA and manually decoded the values sent back and forth (that was fun). There was no data corruption. Turns out it was a PEBCAC.

Compare the first (incorrect) listing below with the second (correct) one and see if you can figure out what the problem was.

      lcd_gotoxy(0,0);
      lcd_puts(touchx_bf);
      lcd_gotoxy(0,1);
      lcd_puts(touchy_bf);
      lcd_gotoxy(0,0);
      lcd_puts("             ");
      lcd_gotoxy(0,0);
      lcd_puts(touchx_bf);
      lcd_gotoxy(0,1);
      lcd_puts("             ");
      lcd_gotoxy(0,1);
      lcd_puts(touchy_bf);

The goddamn LCD kept displaying part of the previous value and made it look like it was displaying 900 (for example) instead of 9. *FACEPALM*

Let this be a lesson to you kids: always clean your display, the smudges might drive you to premature hair loss.

Leave a Reply

Your email address will not be published.