A logic analyzer can be an important tool when debugging a project, particularly when dealing with precise timing constraints. There are a few options available, including the code below. For a more full featured logic analyzer compatible with the open SUMP protocol check out this forum thread: http://arduino.cc/forum/index.php?topic=52881.0
This is a very crude logic analyzer. Output will be to the serial port. You will need to add 2040 missing lines as indicated below.
The idea is to capture everything into a .csv file and then display it using some spreadsheet --> no need for any specialized display software.
The output is formatted to allow quick analysis even in the terminal.
It will always stop in an endless loop --> push reset to restart.
//
// no rights, no warranty, no claims, just a quick hack
// Udo Klein August 2009
//
void setup() {
const uint8_t mask = 0xff;
const int max = 1024;
const int show = 1024;
uint8_t log[max];
// configure all port D pins for input
for (int pin = 0; pin < 8; ++pin) {
pinMode(pin,INPUT);
digitalWrite(pin, HIGH);
}
// capture 1024 at 1 byte each 3 cycles
// trigger if any of the bits in mask changes
uint8_t tmp; // dummy for temporary register
__asm__ __volatile__ (
" cli" "\n\t"
// wait for trigger condition
" in __tmp_reg__, %[pin_d]" "\n\t"
" and __tmp_reg__, %[mask]" "\n\t"
"1: in %[tmp], %[pin_d]" "\n\t"
" and %[tmp], %[mask]" "\n\t"
" cp %[tmp], __tmp_reg__" "\n\t"
" breq 1b" "\n\t"
// log 1024 bytes
" in %[tmp], %[pin_d]" "\n\t"
" st %a[log]+, %[tmp]" "\n\t"
" in %[tmp], %[pin_d]" "\n\t"
" st %a[log]+, %[tmp]" "\n\t"
--- snip --- 2040 similar lines deleted --- snip ---
" in %[tmp], %[pin_d]" "\n\t"
" st %a[log]+, %[tmp]" "\n\t"
" in %[tmp], %[pin_d]" "\n\t"
" st %a[log]+, %[tmp]" "\n\t"
" sei" "\n\t"
: // no output
: [log] "e" (log), // constraint: pointer register
[pin_d] "M" (_SFR_IO_ADDR(PIND)), // constraint: constant
[mask] "a" (mask), // constraint: simple upper register
[tmp] "a" (tmp) // constraint: simple upper register
: "memory"
);
Serial.begin(115200);
for (int line = 0; line < show; ++line) {
Serial.print(line);
for (int i = 7; i >= 0; --i) {
Serial.print(',');
Serial.print((log[line] >> i) & 1);
}
Serial.println("");
}
for (;;) {}
}
void loop() {
}