Serial transmission and display

I am trying to read a SRAM chip and send binary data from my Arduino to PC but I am having a problem. When I try to send and display 00000001 from the pc to the arduino seems to display 1 not 00000001 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<=0x07FF;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("\t");
  Serial.println((u8)(index&0x00FF),HEX);
  Serial.print("The received data pattren is: ");
  Serial.println(message,BIN);  
}

If there's only one significant bit, the standard print overload will print only one bit.
In fact, none of them will print leading zeroes, whatever the base.

It's the way it is :wink: Leading zeroes are omitted and I doubt it will ever have 0b in front of it.

If you're happy with hexadecimal,

byte buffer[5];
sprintf(buffer, "0x%02X", message);
Serial.println(buffer);

Else you have to compose it manually; something in the line of below

Serial.print("0b");
Serial.print((message&0x80) == 0x80 ? 1 : 0);
Serial.print((message&0x40) == 0x40 ? 1 : 0);
Serial.print((message&0x20) == 0x20 ? 1 : 0);
Serial.print((message&0x10) == 0x10 ? 1 : 0);
Serial.print((message&0x08) == 0x08 ? 1 : 0);
Serial.print((message&0x04) == 0x04 ? 1 : 0);
Serial.print((message&0x02) == 0x02 ? 1 : 0);
Serial.println((message&0x01) == 0x01 ? 1 : 0);

Codes not tested.

Or perhaps

  for (int bit = 7; bit >= 0; bit--)
  {
    Serial.print(bitRead(aByte, bit));
  }

Take your pick.

Neat; keep forgetting about bitRead(). I was considering a shift operation though.

UKHeliBob:
Or perhaps

  for (int bit = 7; bit >= 0; bit--)

{
    Serial.print(bitRead(aByte, bit));
  }



Take your pick.

I have tried this method and it worked. Thank you very much!

Be aware that it is not as fast as other methods but is very readable, and can be made into a function if such output is required in more than one place in your program.