8 KEYS TOUCH PAD TTP226 Serial programing

I’ve spent a day to compose the program for serial management of TTP226, but at last it works!

// it has been checked for Arduino Pro Mini
//You need to connect OP1 pins

int clockPin = 12; //SCL pin goes here
int sdoPin = 2;  //SDO pin goes there
int DV = 3;  //DV pin goes there
int BUTTONS = 4;  //Buttons pin goes there
boolean irp=false;


void setup()
{pinMode(BUTTONS, OUTPUT); 
Serial.begin(9600);
pinMode(clockPin,OUTPUT);
pinMode(sdoPin,INPUT);
pinMode(DV,INPUT);
digitalWrite(BUTTONS,HIGH);
attachInterrupt(1, intrp, RISING);  //to DV pin
}

void loop()
{
if( irp==true){  
int val = fetchData();
if(val > 0)Serial.println(val);else irp=false;
}; 
Serial.println(" delay  "); 
delay(500);
}

void intrp(){irp=true;};

int fetchData(){
int Key=0;int Ziro=0;
for(int i = 1; i < 9; i++){       //send pulse
digitalWrite(clockPin,1);
delayMicroseconds(1000);
if(digitalRead(sdoPin) == HIGH) Key=i; else Ziro++;
digitalWrite(clockPin,0);
delayMicroseconds(1000);
}
if(Key>0&&Ziro==7)return Key;
return 0;
}

This code works. Be aware that you have to jumper OP1 on the TTP226 module to put it into “Serial Mode”. Once in serial mode, there are only 4 signal pins you need to worry about. These are marked on the TTP226 module as follows:

DV (Data Valid) used to generate the interrupt, so must be routed to Arduino pin D2 or D3.
In this example we used D3 which corresponds to hardware interrupt 1 on the Arduino.

OUT8 (clock, ck, or scl). This output pin on the TTP226 turns into an input pin when the chip
is set to serial mode. The actual clock signal is generated by the Arduino sketch. A series of
8 clock pulses are required to convert the parallel data within the TTP226 chip into a stream of 8 data bits which will appear on the OUT1 pin of this module. In this example Arduino pin D12 was used.

RST (Reset). In the sketch its inexplicably called “BUTTONS”. This pulse from the Arduino is used to re-sync the TTP226’s shift register so that all the subsequent clock pulses on OUT8 will be in sync with the sketch running on the Arduino. In this example Arduino pin D4 was used.

OUT1 (Data Out, DO, sdo). This is the serial data line that goes from the TTP226 over to
one of the Arduino digital input lines. In this example, Arduino pin D2 was used.

Once you get the pins figured out, working though the code is pretty easy if you understand how interrupts work on the Arduino Uno.

// Here is my first pass at cleaning up the code…
//
// This code has been checked on an Arduino Pro Mini and an Arduino Uno
// You need to jumper the OP1 pins in order to put the TTP226 chip into serial mode.

int clockPin = 12; // “SCL” (Serial CLock) pin goes here. On TTP226 module, its called “OUT8”.
int sdoPin = 2; // “SDO” (Serial Data Out) pin goes here. On module its called “OUT1”.
int dvPin = 3; // “DV” (Data Valid) pin goes here. On module its called “DV”.
int rstPin = 4; // “RST” (Reset) pin goes here. On module its called “OUT7”.

boolean irp=false;

void setup() {

Serial.begin(9600);
pinMode(dvPin, INPUT);
pinMode(sdoPin, INPUT);
pinMode(rstPin, OUTPUT);
pinMode(clockPin, OUTPUT);

// Generate RST signal to sync TTP226 to this sketch.
digitalWrite(rstPin, HIGH);
attachInterrupt(1, intrp, RISING); // The DV signal generates an Arduino H/W interrupt 1
delay(10);
digitalWrite(rstPin, LOW);
}

void loop() {
if( irp == true){
int val = fetchData();
if(val > 0)
Serial.println(val);
// else
irp=false; // Shouldn’t this also be done for case where val > 0?
}

// Put the rest of your “main line” code here:
Serial.println(" delay ");
delay(500);
}

void intrp(){
irp = true; // When interrupt occurs, set a flag and then process it later in loop()
}

int fetchData() {
int Key = 0;
int Ziro = 0;
for(int i = 1; i < 9; i++) { // Send 8 clock pulses and check each data bit as it arrives
digitalWrite(clockPin,1);
delayMicroseconds(1000);
if(digitalRead(sdoPin) == HIGH) // If data bit, high, then that key was pressed.
Key=i;
else
Ziro++;
digitalWrite(clockPin,0);
delayMicroseconds(1000); // Don’t use delay(1) as it will mess up interrupts
}
if(Key>0 && Ziro==7) return Key;
return 0;
}