I’ve manage to get this to work!
Here is working code for both TFT Screen and touch – read comments for settings and connections.
// @konczakp
//
// TFT settings
// in Adafruit_ILI9341_8bit_STM.h
//
// #define TFT_CNTRL GPIOA
// #define TFT_DATA GPIOB
// #define TFT_RD PA4
// #define TFT_WR PA5
// #define TFT_RS PA6
// #define TFT_CS PA7
// #define TFT_RST PA8
//
// Connection
// Port data |D7 |D6 |D5 |D4 |D3 |D2 |D1 |D0 |
// Pin stm32 |PB7|PB6|PB5|PB4|PB3|PB2|PB1|PB0|
// Control pins |RD |WR |RS |CS |RST|
// Pin stm32 |PA4|PA5|PA6|PA7|PA8|
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include "Adafruit_ILI9341_8bit_STM.h"
#define YP PA6 // LCD_RS
#define XM PA7 // LCD_CS
#define YM PB0 // LCD_D0
#define XP PB1 // LCD_D1
#define pressureThreshhold 20
#define NUMSAMPLES 12
Adafruit_ILI9341_8bit_STM tft = Adafruit_ILI9341_8bit_STM();
void testLines(uint16_t color)
{
int x1, y1, x2, y2,
w = tft.width(),
h = tft.height();
tft.fillScreen(ILI9341_BLACK);
x1 = 0;
y1 = h - 1;
y2 = 0;
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
}
void setup() {
Serial.begin (115200);
tft.begin();
tft.setRotation(1);
testLines(ILI9341_CYAN);
}
void loop()
{
getPoint_new(XP, YP, XM, YM, 300);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
}
void getPoint_new(byte xp, byte yp, byte xm, byte ym, int rxplate) {
int x, y, z;
int samples[NUMSAMPLES];
byte i, valid;
gpio_dev* xp_port = digitalPinToPort(xp);
gpio_dev* yp_port = digitalPinToPort(yp);
gpio_dev* xm_port = digitalPinToPort(xm);
gpio_dev* ym_port = digitalPinToPort(ym);
byte xp_pin = digitalPinToBitMask(xp);
byte yp_pin = digitalPinToBitMask(yp);
byte xm_pin = digitalPinToBitMask(xm);
byte ym_pin = digitalPinToBitMask(ym);
valid = 1;
pinMode(yp, INPUT);
pinMode(ym, INPUT);
*portOutputRegister(yp_port) &= ~yp_pin;
*portOutputRegister(ym_port) &= ~ym_pin;
pinMode(xp, OUTPUT);
pinMode(xm, OUTPUT);
*portOutputRegister(xp_port) |= xp_pin;
*portOutputRegister(xm_port) &= ~xm_pin;
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
for (i=0; i<NUMSAMPLES; i++) {
samples[i] = analogRead(yp);
}
insert_sort(samples, NUMSAMPLES);
x = (4096-samples[NUMSAMPLES/2]);
pinMode(xp, INPUT);
pinMode(xm, INPUT);
*portOutputRegister(xp_port) &= ~xp_pin;
pinMode(yp, OUTPUT);
*portOutputRegister(yp_port) |= yp_pin;
pinMode(ym, OUTPUT);
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
for (i=0; i<NUMSAMPLES; i++) {
samples[i] = analogRead(xm);
}
insert_sort(samples, NUMSAMPLES);
y = (4096-samples[NUMSAMPLES/2]);
// Set X+ to ground
pinMode(xp, OUTPUT);
*portOutputRegister(xp_port) &= ~xp_pin;
// Set Y- to VCC
*portOutputRegister(ym_port) |= ym_pin;
// Hi-Z X- and Y+
*portOutputRegister(yp_port) &= ~yp_pin;
pinMode(yp, INPUT);
int z1 = analogRead(xm);
int z2 = analogRead(yp);
if (rxplate != 0) {
// now read the x
float rtouch;
rtouch = z2;
rtouch /= z1;
rtouch -= 1;
rtouch *= x;
rtouch *= rxplate;
rtouch /= 4096;
z = rtouch;
} else {
z = (1023-(z2-z1));
}
if (! valid) {
z = 0;
}
if (z > pressureThreshhold && z < 100){
Serial.print("X = "); Serial.print(x);
Serial.print("\tY = "); Serial.print(y);
Serial.print("\tPressure = "); Serial.println(z);
}
}
static void insert_sort(int array[], byte size) {
byte j;
int save;
for (int i = 1; i < size; i++) {
save = array[i];
for (j = i; j >= 1 && save < array[j - 1]; j--)
array[j] = array[j - 1];
array[j] = save;
}
}