arduino i2c logic reader - Page 1
Forum section: electronics / general

author: carbon dude oxide
author_rank: Frequent Contributor
timestamp: July 20, 2013, 05:52:35 pm
content: hello, i m currently in the process of making an i2c (will expand to other buses) reader/analyser on the arduino. its just a small project to pass the time and keep me busy when im not at work.im thinking of using a Schmitt trigger on the inputs to remove the rise/fall time but i have a few questions regarding this. would i even need a Schmitt trigger on an arduino digital input if so would they work at i2c max frequency (100kHz , yes i know some can do 400kHz but lets not go into that).would i need a high resistor to reduce the current going into the arduino and preent increasing the rise/fall timescan an arduino sample accuratly at 1 - 10us intervals or is it not able to read the inputs that quickly? (that gives it the full ability to read a 100kHz i2c signal)your thoughts?

author: Jon Chandler
author_rank: Frequent Contributor
timestamp: July 20, 2013, 06:13:25 pm
content: Dangerous Prototypes' Bus Pirate? At least take a look at its features.

author: oPossum
author_rank: Super Contributor
timestamp: July 20, 2013, 06:55:23 pm
content: My thoughts?Use a PIC...http://compendiumarcana.com/iic/Up to 400 kHz real time monitoring.

author: carbon dude oxide
author_rank: Frequent Contributor
timestamp: July 20, 2013, 08:14:27 pm
content: Quote from: oPossum on July 20, 2013, 06:55:23 pmi have no money to get new partsim stuck with what i have in my la currently which is 4 arduinos and a load of other standard chips (such as cmos schmitt's)i do have a pic in my draws but i cant program it currently as the part has not arrived yeti have not has any requirement for 400kHz at the moment but i do have some i2c stuff that does 100kHzQuote from: Jon Chandler on July 20, 2013, 06:13:25 pm

author: Chet T16
author_rank: Frequent Contributor
timestamp: July 20, 2013, 08:50:30 pm
content: I came across an arduino to serial i2c reader thing which I modified for my purposes although I've only tested it on a 7khz clock where it worked very well

author: Chet T16
author_rank: Frequent Contributor
timestamp: July 20, 2013, 08:58:39 pm
content: Code:#define MAX_BITS 300int SCLpin = 2;int SDApin = 3;int bytes = {}; //Array to store sniffed byteschar data;int bitCount = 0;void setup() {pinMode(SCLpin, INPUT);pinMode(SDApin, INPUT);Serial.begin(115200);}void loop(){unsigned char samplebit;/* SDA and SCL should be high in idle state, wait until both are high */while((PIND & 0b00001100) != 0b00001100);/* Wait for i2c start - SDA going low while SCL is high */while((PIND & 0b00001100) != 0b00000100);/* We have out start marker, write to array then increment counter */data = 'S';bitCount++;/* Wait until SCL goes LOW */while((PIND & 0b00000100) != 0b00000000);/* Read bits until bitcounter is reset */while(bitCount > 0) {/* Wait if SCL isn't LOW*/while((PIND & 0b00000100) != 0);/* Wait for SCL to go HIGH - we have a clock transition */while((PIND & 0b00000100) == 0);/* Sample SDA to get a bit value */samplebit = (PIND & 0b00001000);/* Write bit based on sample value and increment bitcounter */data = (samplebit == 0 ? '0' : '1');bitCount++;// Wait for SCLpin to transition low while looking// for start or stop condition. A START or STOP// means the previous bit isn't a data bit. So will// write START, STOP condition into the same memory slotif (samplebit == 0) {while ( ((PIND & 0b00000100) != 0) && (bitCount > 0) ) {if ( (PIND & 0b00001000) != 0) {// detected STOP conditiondata[bitCount-1] = 'P';writeData();}}}}}void writeDatax(){int numbytes = ((bitCount-2)/9)-1;Serial.println (bitCount);bitCount = 0;}void writeData () {Serial.println();int i;int numbytes = ((bitCount-2)/9)-1;for (i = 0; i < bitCount; i++) {Serial.print (data);if (data == 'P') {Serial.println ("");}}if(data=='1'){Serial.print("Read from address: ");}else{Serial.print("Write to address: ");}for(int n = 0; n <= numbytes; n++){for (int m = 1 , i = 1+(n*9); i <= 8+(n*9); i++,m++) {if(data == '1'){bitWrite(bytes, abs(m-8),1);}else{bitWrite(bytes, abs(m-8),0);}}}Serial.print (bytes>>1,HEX);Serial.print (" Number of Bytes: ");Serial.println (numbytes);for(int n = 1;n <= numbytes; n++){Serial.print ("Byte ");Serial.print (n);Serial.print (": ");Serial.print (bytes,HEX);Serial.print (" - ");Serial.write (bytes);Serial.print (" - ");Serial.println (bytes,DEC);}bitCount = 0;}Apologies for the commentless wonder!I'll see if I can find the original that it's based on