Serial data display

I am trying to read a SRAM chip and send HEX SRAM cell address from my Arduino to PC but I am having a problem. When I try to send and display the address of SRAM cell in HEX such as 0000 from the pc to the arduino seems to display 0 not 0000 the byte. Is there any solution can be implemented to let it work? Thank you!

typedef unsigned short u16;
typedef unsigned char u8;
int CE = 50;
int OE = 51;
int WE = 52;
void setup() {
  // put your setup code here, to run once:
  pinMode(CE, OUTPUT);//Set PB3(50) as outputs(chip enable)
  pinMode(OE, OUTPUT);//Set PB2(51) as outputs(output enable)
  pinMode(WE, OUTPUT);//Set PB1(52) as outputs(write enable)
  DDRA = B11111111;//Set PortA as outputs(Data)
  DDRC = B11111111;//Set PortC as outputs(Address)
  DDRL = B11111111;//Set PortL as outputs(Address)
  Serial.begin(9600);
}

void loop() {
  u8 r_data = 0;
  u8 w_data = 0xFF;
  u16 addr; //SRAM address
  /*SRAM Init*/
  SRAM_Init( );
  /*Write the SRAM*/
  //SRAM_Write(addr,w_data);
  for(addr=0;addr<=0x7FFF;addr++){
    /*Read the SRAM*/
    
    r_data = SRAM_Read(addr);
    displayInfo(addr,r_data);
  }
  while(1){
    }
  
}

/*SRAM Init*/
void SRAM_Init(void){
  digitalWrite(CE, HIGH);
  digitalWrite(WE, HIGH);
  digitalWrite(OE, HIGH);
}

/*Write 8-bit data to SRAM. Write cycle No.1 (/WE controlled)*/
void SRAM_Write( u16 addr, u8 data){
  /*Put the address lines 9X6*/
 
  PORTC = (u8)(addr&0x00FF);
  PORTL = (u8)((addr&0x7F00)>>8);
  
  /*Begin the write cycle*/
  digitalWrite(WE, HIGH);
  digitalWrite(OE, HIGH);
  digitalWrite(CE, LOW);//Select the chip
  /*Send data to SRAM*/
  PORTA = (u8)data;
  digitalWrite(WE, LOW);
  digitalWrite(WE, HIGH);
  digitalWrite(CE, HIGH);
  
}

/*Read from SRAM. Read cycle No.2 (WE = HIGH)*/
unsigned char SRAM_Read(u16 addr){
  unsigned char val = 0;//Store data
  
  /*Setup for data input*/
  DDRA = B00000000;//Set PortA as inputs(Data)
  digitalWrite(CE, HIGH);//Deselect chip

  /*Put the address lines 9X6*/
  PORTC = (u8)(addr&0x00FF);
  PORTL = (u8)((addr&0x7F00)>>8);
  /*Begin read cycle*/
  digitalWrite(WE, HIGH);
  digitalWrite(OE, HIGH);
  digitalWrite(CE, LOW);//Select the chip

  digitalWrite(OE, LOW);//Output enable
  val = PINA;
  digitalWrite(OE, HIGH);

  /*End read cycle*/
  digitalWrite(CE, HIGH);//Deselect chip
  return val;
}

/*Display the information*/
void displayInfo(u16 index, u8 message){
  //Serial.println("The testing address is: ");
  Serial.print((u8)((index&0x7F00)>>8),HEX);
  
  Serial.print((u8)(index&0x00FF),HEX);
  Serial.print("\t");
  //Serial.print("The received data pattren is: ");
  for(int b = 0;b<=7;b++)
  { //Serial.print((u8)((index&0x7F00)>>8),HEX);
    //Serial.print((u8)(index&0x00FF),HEX);
    //Serial.print(b,DEC);
    //Serial.print("\t");
    Serial.print(bitRead(message,b)); 
  }
  Serial.println();
}

While I'm not a big fan of sprintf(), it does have a format specifier for hex. You could use it to dump the data to a buffer in hex and then display that.

or use the safer snprintf()

void setup()
{
  Serial.begin(9600);
  uint8_t myByte = 0xAE;
  int myInt = 0xFF00;
  char myBuffer[64] = "";
  snprintf(myBuffer, sizeof(myBuffer), "My hex value byte = 0x%x and int =0x%x", myByte, myInt);
  Serial.println(myBuffer);
}
 
void loop()
{
  
}

A solution using sprintf was given in reply #2 in your post http://forum.arduino.cc/index.php?topic=406736.0.

It only displays 2 digits minimal (one leading zero), you can easily modify it to make it 4 digits minimal (3 leading zeroes).

@BulldogLowell: you forgot to specify the size and the leading zeroes :wink:

sterretje:
@BulldogLowell: you forgot to specify the size and the leading zeroes :wink:

Ha! I'll try to be more thorough! I meant to show that snprintf was friendlier and I guess I'll leave it to the OP to do research on C Format Specifiers!!