TFT Touch Shield V1.0 with Leonardo?

Hi everyone,

I bought a Touch Shield from RadioShack for a steal of $15 this afternoon, and it’s been fun to play with. After playing with it on my Uno, I decided I wanted to use it with my Leonardo as a small touch-screen keyboard.

It didn’t work, and I found mention on Adafruit’s site about it not being compatible with the Leonardo.

I was wondering, why isn’t it compatible? Is there something I can do to make it work with the Leonardo, or is it impossible?

Hello,

Could you please check if the example code mentioned in seeedstudio.com/wiki/2.8’’_T … hield_V1.0 . It looks like the necessary pin re-mapping for Leonardo is mentioned in that code :

#elif defined(__AVR_ATmega32U4__) // leonardo
#define YP A2   // must be an analog pin, use "An" notation!
#define XM A1   // must be an analog pin, use "An" notation!
#define YM 18   // can be a digital pin, this is A0
#define XP 21   // can be a digital pin, this is A3 

Thanks

Pick up a Touch Shield v1 at Radio Shack and got it to work with a Leonardo. It was a bit more involved than changing just the resistive pins (i.e. XM, XP, YM, YP). The pin mappings from D2 through D9 to the microcontroller pins is very different. Here are the pin mappings for Uno, Mega, and Leonardo:

Pin d9 d8 d7 d6 d5 d4 d3 d2 Uno PB1 PB0 PD7 PD6 PD5 PD4 PD3 PD2 Mega PH6 PH5 PH4 PH3 PE3 pg5 PE5 PE4 Leon PB5 PB4 PE6 PD7 PC6 PD4 PD0 PD1

The code changes to TFT.cpp are shown below. The first set of changes are at the start of the file and the second at the end.

Change TFT::pushData and TFT::getData as follows:

[code]void TFT::pushData(unsigned char data)
{
all_pin_low();
#ifdef ARDUINO_AVR_LEONARDO
PORTD |= ((data<<1) & (0x02));
PORTD |= ((data>>1) & (0x01));
PORTD |= ((data<<2) & (0x10));
PORTC |= ((data<<3) & (0x40));
PORTD |= ((data<<3) & (0x80));
PORTE |= ((data<<1) & (0x40));
PORTB |= ((data>>2) & (0x30));
#else ifdef SEEEDUINO
PORTD |= (data<<2);
PORTB |= (data>>6);
#endif

#ifdef MEGA
PORTE |= ((data<<4) & (0x30));
PORTG |= ((data<<3) & (0x20));
PORTE |= ((data & 0x08));
PORTH |= ((data>>1) & (0x78));
#endif

#ifdef MAPLE
#endif
}

unsigned char TFT::getData(void)
{
unsigned char data=0;
delay(1);
#ifdef ARDUINO_AVR_LEONARDO
data |= ((PIND&0x02)>>1); //PD1
data |= ((PIND&0x01)<<1); //PD0
data |= ((PIND&0x10)>>2); //PD4
data |= ((PINC&0x40)>>3); //PC6
data |= ((PIND&0x80)>>3); //PD7
data |= ((PINE&0x40)>>1); //PE6
data |= ((PINB&0x30)<<2); //PB4,5
#else ifdef SEEEDUINO
data |= ((PIND&0xfc)>>2);
data |= ((PINB&0x03)<<6);
#endif

return data;

}[/code]

And at the end, change the following

[code]void TFT::all_pin_input(void)
{

#ifdef ARDUINO_AVR_LEONARDO
DDRD &=~ 0x93;
DDRC &=~ 0x40;
DDRE &=~ 0x40;
DDRB &=~ 0x30;
#else ifdef SEEEDUINO
DDRD &=~ 0xfc;
DDRB &=~ 0x03;
#endif
#ifdef MEGA
DDRE &=~ 0x38;
DDRG &=~ 0x20;
DDRH &=~ 0x78;
#endif

#ifdef MAPLE

#endif

}

void TFT::all_pin_output(void)
{
#ifdef ARDUINO_AVR_LEONARDO
DDRD |= 0x93;
DDRC |= 0x40;
DDRE |= 0x40;
DDRB |= 0x30;
#else ifdef SEEEDUINO
DDRD |= 0xfc;
DDRB |= 0x03;
#endif

#ifdef MEGA
DDRE |= 0x38;
DDRG |= 0x20;
DDRH |= 0x78;
#endif

#ifdef MAPLE

#endif
}

void TFT::all_pin_low(void)
{

#ifdef ARDUINO_AVR_LEONARDO
PORTD &=~ 0x93;
PORTC &=~ 0x40;
PORTE &=~ 0x40;
PORTB &=~ 0x30;
#else ifdef SEEEDUINO
PORTD &=~ 0xfc;
PORTB &=~ 0x03;
#endif
#ifdef MEGA
PORTE &=~ 0x38;
PORTG &=~ 0x20;
PORTH &=~ 0x78;
#endif

#ifdef MAPLE

#endif
}

TFT Tft=TFT();[/code]

And lastly, make changes in TFT.h file. Replace line that starts with #ifdef SEEEDUINO with

[code]#ifdef ARDUINO_AVR_LEONARDO

//======================================== PB6
#define DDR_CS DDRB
#define PORT_CS PORTB
#define CS_BIT 0x40
#define CS_OUTPUT {DDR_CS|=CS_BIT;}
#define CS_HIGH {PORT_CS|=CS_BIT;}
#define CS_LOW {PORT_CS&=~CS_BIT;}

//----------------------------------------- PB7

#define DDR_RS DDRB
#define PORT_RS PORTB
#define RS_BIT 0x80
#define RS_OUTPUT {DDR_RS|=RS_BIT;}
#define RS_HIGH {PORT_RS|=RS_BIT;}
#define RS_LOW {PORT_RS&=~RS_BIT;}

//----------------------------------------- PD6

#define DDR_WR DDRD
#define PORT_WR PORTD
#define WR_BIT 0x40
#define WR_OUTPUT {DDR_WR|=WR_BIT;}
#define WR_HIGH {PORT_WR|=WR_BIT;}
#define WR_LOW {PORT_WR&=~WR_BIT;}
#define WR_RISING {PORT_WR|=WR_BIT;PORT_WR&=~WR_BIT;}

//----------------------------------------- PC7

#define DDR_RD DDRC
#define PORT_RD PORTC
#define RD_BIT 0x80
#define RD_OUTPUT {DDR_RD|=RD_BIT;}
#define RD_HIGH {PORT_RD|=RD_BIT;}
#define RD_LOW {PORT_RD&=~RD_BIT;}
#define RD_RISING {PORT_RD|=RD_BIT;PORT_RD&=~RD_BIT;}
//========================================

#else ifdef SEEEDUINO[/code]

If the code maintainer could incorporate these changes, that would be great.