Strange sprintf behavior

Hi! I'm new here! If this has been addressed already, please forgive me. I've looked through other posts and can't find one that covers this problem I have.

Here's what's happening. I have a big pile of ROMs of different types and sizes, and want to look at the contents. I'm using a Mega 2560 since it has enough digital pins to avoid needing shift registers to keep things a little easier. Here's the code:

#include <stdint.h>

#define A0 26
#define Q0 2

// Chip-specific values
#define NDB 8  // number of data bits (word length)
#define NAB 14  // number of address bits used
uint32_t MAX_ADDR = 32768; // depends on size of ROM


void setup() {
    for (int i = A0; i < A0+NAB; i++) {
    digitalWrite(i,LOW);
    pinMode(i, OUTPUT);
  }
  for (int i = Q0; i < Q0+NDB; i++) {
    digitalWrite(i,HIGH);
    pinMode(i, INPUT);
  }
  Serial.begin(115200);

  // for contents of ROM, read 16 bytes at a time and print as a formatted string

  uint8_t data[16];  // array for a line of bytes
  char buf[80];  // biffer for line data
   
  for (uint32_t addr = 0; addr <= MAX_ADDR; addr += 16) {
    for (int offset = 0; offset <= 15; offset++) {
      writeAddr(addr+offset);
      data[offset] = readByte();
      Serial.print(data[offset],HEX);
      Serial.print(" ");
    }
    Serial.println("");
    // format the data and print it
    sprintf(buf,"%06x:   %02x %02x %02x %02x %02x %02x %02x %02x  %02x %02x %02x %02x %02x %02x %02x %02x",
      addr,data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],data[11],data[12],data[13],data[14],data[15]);
    Serial.println(buf);
  }
}

void writeAddr(uint32_t addr) {
  uint32_t mask = 0x01;
  for (int i = A0; i < A0+NAB; i++) {
    if ((mask & addr) != 0) {
      digitalWrite(i,HIGH);
    } else { 
      digitalWrite(i,LOW);
    }
    mask = mask << 1;
  }
}


uint8_t readByte() {
  uint8_t data = 0;
  uint8_t mask = 0x1;
  for (int i = Q0; i < Q0+NDB; i++) {
    if (digitalRead(i) == HIGH) {
      data |= mask;
    }
    mask = mask << 1;
  }
  return data;
}

void loop() {
  // no loop in this version
}

The troible I have is that it looks like the data being read is correct, but when I try printing it using sprintf it's coming out wrong. The first element is always a 00 and everything else is shifted over one position. The Serial.print statements in the loop show the data as read, and you can see the wrong output that follows. I basically copied the code Ben Eater used on his EEPROM programmer Youtube vid. It's very simple but I just can't figure out why his vesrion worked and mine isn't!

6 1F 66 C6 AC 36 24 8C 7 5 17 14 3D F7 5 9C 
000000:   00 06 1f 66 c6 ac 36 24  8c 07 05 17 14 3d f7 05
24 AD F6 6 1F 4F 6 74 67 4 74 B6 97 85 44 8C 
000010:   00 24 ad f6 06 1f 4f 06  74 67 04 74 b6 97 85 44
36 6 14 14 9D 1C 14 9C 2F 5 17 14 CE 7 5 17 
000020:   00 36 06 14 14 9d 1c 14  9c 2f 05 17 14 ce 07 05

(...and so on for the contents...)

I'm sure I'm doing something very simple wrong, but just not seeing it. Help?

addr is uint32_t (unsigned long), so try %06lx in your format string. I think your extra 0 at the front could be the high word of addr,

That was it! Thanks to van_der_decken. It's been a long time since I've done any real programming. Now my next phase is to add some code to show the associated characters if printable. Maybe someday I'll be able to return the favor by helping someone else. Meanwhile I'll keep reading forums.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.