How to display digital values in Serial Monitor with HEX format

Hello everyone,
I just got in touch with C and Arduino, now I need to use Arduino UNO digital pointer to read / write SPI flash (using GPIO mode, can't use SPI pins). At present, I have successfully sent 9Fh and got the correct response from DO pin.
But now I need to display the result in "Serial Monitor", how can I achieve it?

The following is my current code. Please help me, thank you all.

int CS = 2;
int DI = 3;
int DO = 4;
int CLK = 5;
//int HOLD = 6;
//int WP = 7;
int VCC = 8;

int val = 0;
int i;

int CLK1(int a) {
  for(i=0;i<a;i++) {
    digitalWrite (CLK, LOW);      /*set clock*/
    delayMicroseconds(200);
    digitalWrite (CLK, HIGH);
    delayMicroseconds(200);
  }
  digitalWrite (CLK, LOW);
  delayMicroseconds(100);
}

void CLK_H(){
  delayMicroseconds(100);
  digitalWrite (CLK, HIGH);
  delayMicroseconds(200);
}

void CLK_L(){
  digitalWrite (CLK, LOW);
  delayMicroseconds(100);
}

void instruction(int b, int c){   //checking bit is high or low, using % to achieve.
  int a[4], i;
  for(i = 0; i < 4; i++){
    a[i] = b % 2;
    b = b / 2;
  }
  for(i = 3; i >= 0; i--){
    if(a[i] == 1){
      digitalWrite(DI, HIGH);
      CLK_H();
      CLK_L();
    }
    else{
      digitalWrite(DI, LOW);
      CLK_H();
      CLK_L();
    }
  }
  for(i = 0; i < 4; i++){
    a[i] = c % 2;
    c = c / 2;
  }
  for(i = 3; i >= 0; i--){
    if(a[i] == 1){
      digitalWrite(DI, HIGH);
      CLK_H();
      CLK_L();
    }
    else{
      digitalWrite(DI, LOW);
      CLK_H();
      CLK_L();
    }
  }
}


void setup() {
  Serial.begin(2400);
  pinMode (CS, OUTPUT);
  pinMode (DI, OUTPUT);
  pinMode (DO, INPUT);
  pinMode (CLK, OUTPUT);
  //pinMode (HOLD, OUTPUT);
  //pinMode (WP, OUTPUT);
  pinMode (VCC, OUTPUT);

}

void loop() {
  int a, b;
  digitalWrite(VCC, HIGH);
  //digitalWrite(HOLD, HIGH);
  //digitalWrite(WP, HIGH);
  digitalWrite(CS, LOW);
  a = 0x9;
  b = 0xF;
  instruction(a, b);
  for (i = 0; i < 24; i++)  {
    CLK_H();
    CLK_L();
  val = digitalRead(DO);  //this is my current questions, it can not print any values in Serial Monitor.
  Serial.print(" ");
  }
  digitalWrite(CS, HIGH);
  digitalWrite(VCC, LOW);
  delay(100);

}

sprintf()

Serial.print(val, HEX);

If you want leading zeros you'll have to print them yourself, or use sprintf.

Why only 2400 baud serial?
Do you have the monitor window set to 2400 also?

/this is my current questions, it can not print any values in Serial Monitor.

Because you're just printing a space?

Serial.print(" ");

digitalRead will only yield you a single bit (1 or 0). You need to shift them into a variable a bit at a time...

val <<= 1;
val |= digitalRead(D0) ? 1 : 0;

Although I don't know if your SPI sends MSB or LSB first.
And also val needs to be bigger than int type (16 bit) as you are reading 24 bits.

gcjr:
sprintf()

Hi Greg,
thanks for your information, I will learn more about sprintf, many thanks.

pcbbc:

Serial.print(val, HEX);

If you want leading zeros you'll have to print them yourself, or use sprintf.

Why only 2400 baud serial?
Do you have the monitor window set to 2400 also?
Because you're just printing a space?

Serial.print(" ");

digitalRead will only yield you a single bit (1 or 0). You need to shift them into a variable a bit at a time...

val <<= 1;

val |= digitalRead(D0) ? 1 : 0;



Although I don't know if your SPI sends MSB or LSB first.
And also val needs to be bigger than int type (16 bit) as you are reading 24 bits.

Hi sir,
Thanks for your info. Actually I did try to set 2400 baud rate in monitor window. My SPI sends MSB. I will try your recommends, many thanks for your replying.