Logic Analyzer Problems

Hi All,
I was trying to determine the workings of an IC in an old universal remote, and I saw that people had created arduino logic analyzers, so I figured i’d roll my own, and ran into some problems compiling. I haven’t written a computer app to interpret the data, but let me know if you see any obvious mistakes.

Thanks!

//DIGITAL PIN 2

byte dat1 = B00000000;
byte dat2 = B00000000;
byte dat3 = B00000000;
byte dat4 = B00000000;
int index = 0;
void setup(){

attachInterrupt(0, readdat, CHANGE);
}
void readdat()
{
if (index < 8) {
int READ = PINB
bitWrite(dat1, index, bitRead(READ, 2));
index = index + 1;
      }

elseif (index < 16) {
READ = PINB
bitWrite(dat2, index - 8, bitRead(READ, 2));
index = index + 1;
      }

elseif (index < 24) {
READ = PINB;
bitWrite(dat3, index - 16, bitRead(READ, 2));
index = index + 1;
      }

elseif (index < 32) {
READ = PINB;
bitWrite(dat4, index - 24, bitRead(READ, 2));
index = index + 1;
      }
}

void loop() {

      if (index = 32)
      {
            Serial.write(dat1);
            Serial.write(dat2);
            Serial.write(dat3);
            Serial.write(dat4); 
      }
}
int READ = PINB

missing semicolon?

READ = PINB

trying to reuse a variable that has fallen out of scope, and another missing semicolon?

It would be easier to help if you posted the compiler's error message.

Ok, thanks, i got it to compile by removing a few other sloppy errors. Unfortunately, my arduino and equipment are at home, where I wont be until monday …any bets on whether it will work?

Heres the compilable version:

//DIGITAL PIN 2

byte dat1 = B00000000;
byte dat2 = B00000000;
byte dat3 = B00000000;
byte dat4 = B00000000;
int index = 0;
void setup(){

attachInterrupt(0, readdat, CHANGE);
}
void readdat()
{
if (index < 8) {
int READ = PINB;
bitWrite(dat1, index, bitRead(READ, 2));
index = index + 1;
}
else if (index < 16) {
int READ = PINB;
bitWrite(dat2, index - 8, bitRead(READ, 2));
index = index + 1;
      }

else if (index < 24) {
int READ = PINB;
bitWrite(dat3, index - 16, bitRead(READ, 2));
index = index + 1;
      }

else if (index < 32) {
int READ = PINB;
bitWrite(dat4, index - 24, bitRead(READ, 2));
index = index + 1;
      }
}

void loop() {

      if (index = 32)
      {
            Serial.write(dat1);
            Serial.write(dat2);
            Serial.write(dat3);
            Serial.write(dat4); 
      }
}

Is your goal to read 32 bit changes on pin 2 then stop? BTW, your check in your loop for reaching 32 bit changes is an assignment statement, and not a comparison statement (==) like you want.

Also - it seems to me that you could vastly simplify the interrupt routine (and indeed, you want to keep it as simple as possible, so-as not to miss interrupts while the interrupt routine is processing); if you notice, all of the code inside the routine (in each if-then) is doing the equivalent processing; you can reduce it down drastically…

thanks for the correction...i see what you mean about simplifying. The reason for the byte storage is for easy transfer and small size, and four bytes at a time just seemed like a nice number. I think that says something about my mathematical talent :-)

i see what you mean about simplifying. The reason for the byte storage is for easy transfer and small size, and four bytes at a time just seemed like a nice number.

What you could do, is keep increasing index until it hits 32, but use the modulo operator in the read to keep it in the range of 0-7 (index % 8 - something like that). Write the bit reads to a single register, but in the loop print that single register four times (instead of building four different registers).

Hope this makes sense. Ultimately, you want the code in your interrupt to be as tight as possible; the fewer lines, the better (indeed, if you can code it using inline assembler, even better).

Good luck!

:)

Heres my revised version…still havent tested yet. If anyone’s near their arduino, would you mind testing this with like a button or something and looking at the output in the serial log after 32 toggles, I would reaaaaally appreciate it. And if it doesn’t work…well, thats what programming is all about! :wink:

//ATTACH PROBE TO DIGITAL PIN 2
volatile byte dat[4] = {B00000000, B00000000, B00000000, B00000000};
volatile byte bufferdat[4] = {B00000000, B00000000, B00000000, B00000000};
volatile int index = 0;
short i;
void setup(){
Serial.begin(115200);
attachInterrupt(0, readdat, CHANGE);
}
void readdat()
{
byte READ = bitRead(PINB, 2);
bitWrite(dat[index / 8], index % 8, READ);
index = index + 1;
}
void loop() {
if (index == 31)
{
for(i=0; i<4; i++)
{
bufferdat[i]=dat[i];
}
index = 0;
Serial.write(bufferdat[0]);
Serial.write(bufferdat[1]);
Serial.write(bufferdat[2]);
Serial.write(bufferdat[3]); 

}
}

As for your assembler suggestion, I know a bit of AVR-assembly, but I have no idea as for the equivalent of the bitWrite function in ASM, and can I access high level variables from in-line assembler? Sorry for all the questions!
Also sorry bout the sloppiness of the original code, i guess thats what happens when you write a program when your half asleep.