/*
Code made by "gorgeous"
http://forum.arduino.cc/index.php?topic=78634.0
Most of the i2c commands here presented were copied and then modified
the original source is the Arduino I2cMaster Library
written by William Greiman
the parity check code is by Claas Anders "CaScAdE" Rathje
pins for sda,scl,mrq are connected directly to their correspondant
on the TID connector, no extra circuitry needed
*/
// pin numbers
#define sda 7
#define scl 5
#define mrq 6
#define tid_delay 150 //this is a working timing value, if you change this, TID can stop working 180
#define delay_300 150
#define delay_100 100
void setup () {
pinMode(mrq, OUTPUT);
pinMode(sda, OUTPUT);
pinMode(scl, OUTPUT);
digitalWrite(mrq, HIGH);
digitalWrite(sda, HIGH);
digitalWrite(scl, HIGH);
}
void loop () {
start_tid();
tid_address(0x9B);
tid_data(0); //first symbols byte
tid_data(0);
tid_data(0);
tid_data(0); //first letter
tid_data(0);
tid_data(0);
tid_data('O');
tid_data('p');
tid_data('e');
tid_data('l');
tid_data(0);
tid_data(0);
tid_data(0); //10th letter
stop_tid();
}
void tid_byte(byte data) {
pinMode(scl, OUTPUT);
for (uint8_t m = 0X80; m != 0; m >>= 1) { //2 lines from
digitalWrite(sda, m & data); //William Greiman's I2cMaster Library
delayMicroseconds(5);
digitalWrite(scl, HIGH);
delayMicroseconds(50);
digitalWrite(scl, LOW);
delayMicroseconds(50);
}
delayMicroseconds(delay_300);
pinMode(sda,INPUT); //3
delayMicroseconds(tid_delay);
pinMode(scl,INPUT); //4
delayMicroseconds(delay_100);
while(digitalRead(scl)==0); //6
delayMicroseconds(delay_300);
pinMode(scl,OUTPUT);
digitalWrite(scl, LOW); //7
while(digitalRead(sda)==0); //8
pinMode(sda,OUTPUT);
}
void tid_data(byte data) { //bytes of data
byte val=data; //this parity check code
byte pari; //comes from
val = val ^ (val >> 4); //Claas Anders "CaScAdE" Rathje http://www.mylifesucks.de/oss/c-tid/
val = val ^ (val >> 2); //from a post in the
val = val ^ (val >> 1); //mikrocontroller.net forum
val &= 0x01; //http://www.mikrocontroller.net/topic/19516
data = (data<<1)|pari;
tid_byte(data);
}
void tid_address(byte address) { //address completed with the extra 0 for write mode on the right
tid_byte(address);
digitalWrite(mrq, LOW);
}
void start_tid() {
pinMode(sda,INPUT);
digitalWrite(mrq, LOW);
delayMicroseconds(100);
while(digitalRead(sda)==1);
digitalWrite(mrq, HIGH);
delayMicroseconds(100);
while(digitalRead(sda)==0);
pinMode(sda,OUTPUT);
pinMode(scl,OUTPUT);
digitalWrite(sda, LOW);
delayMicroseconds(100);
digitalWrite(scl, LOW);
delayMicroseconds(100);
}
void stop_tid() {
digitalWrite(sda, LOW);
delayMicroseconds(100);
digitalWrite(mrq, HIGH);
delayMicroseconds(500);
digitalWrite(scl, HIGH);
delayMicroseconds(100);
digitalWrite(sda, HIGH);
delayMicroseconds(100);
}