DigitalRead & Serial.println Questions

hi all, I am reading data from a memory. The memory has 8 address pins and 8 read pins. Now I only can read 1 address.

The question is when I print the data out, the outputs are :

0
1
1
0
1
1
0
0

by the following code:

Serial.println(digitalRead(RDPins42));
Serial.println(digitalRead(RDPins43));
Serial.println(digitalRead(RDPins44));
Serial.println(digitalRead(RDPins45));
Serial.println(digitalRead(RDPins46));
Serial.println(digitalRead(RDPins47));
Serial.println(digitalRead(RDPins48));
Serial.println(digitalRead(RDPins49));

I want the outputs like: 10101010 and displayed as HEXcode (FF) , so when I read 100 address, It will print 100 HEX outputs(FF 0A 34 EA..), not the binary number.

Do you have any ideal?
Thanks,

The question is when I print the data out, the outputs are

Given that code, that is what I'd expect.

I want the outputs like: 10101010

Then, use print(), not println().

and displayed as HEXcode (FF)

Then, you have to do much more than just read and print the values. You must store them, so the collection can be interpreted as a single value, not 8 discrete values.

Look at bitWrite() for inspiration.

Thanks man, do you have any ideal to save the data?

An array

doaway:
Thanks man, do you have any ideal to save the data?

In a byte using bitWrite()

you want to put the value of each of those bits into a single byte, and then print the hex value of the byte.

you mean let a[] = [digitalread(1), digitalread(2),...digitalread(8)];

Serial.println(a,hex);

?

Crude method

bitWrite(myVar,0,digitalRead(RDPins42));
bitWrite(myVar,1,digitalRead(RDPins43));
bitWrite(myVar,2,digitalRead(RDPins44));
bitWrite(myVar,3,digitalRead(RDPins45));
bitWrite(myVar,4,digitalRead(RDPins46));
bitWrite(myVar,5,digitalRead(RDPins47));
bitWrite(myVar,6,digitalRead(RDPins48));
bitWrite(myVar,7,digitalRead(RDPins49));

Serial.print(myVar,HEX);

Depending on the bit order of the data that you are reading you will need to reverse the order of the bits of written to.

A more elegant way would be to use a for loop and hold the source pins in an array or, if they are consecutive, derive them from the for loop index.

Thanks all, learned alot

You could try something like:

#include <stdint.h>
...
uint8_t value = 0;
for (uint8_t pin = RDPins42; pin <= RDPins49; pin++) {
  value <<= 1;
  value |= digitalRead(pin);
}
Serial.print(value, HEX);

I guess that what you really want to do is read the whole byte from the memory at once? This can be done if the data bus lines of the memory is connected to a single port.

uint8_t value = PINL;

If I do a wild guess that your RD pins 42-49 are on a Mega.
Cheers!

Yep! it is Mega, and I want to read whole byte from memory and displayed as HEX.

Is it possible to read 2 or more address data in a single sketch ? Example: address 00000000 and 00000001 data

And what does this "uint8_t value = PINL" mean ?

void loop {
byte i;
i=i+1;
PORTD = i;
uint8_t value = 0;
for (uint8_t pin = RDPins42; pin <= RDPins49; pin++) {
value <<= 1;
value |= digitalRead(pin);
}
Serial.print(value, HEX);
if (i==2){i=0;}

}

Ok, so what you need to do is write the address and then read the data. There should be a pin on the memory to control read/write. Guess we will come to that later.

PORTD = address;
uint8_t data = PINL;
Serial.println(data, HEX);

PINL is how you read the port L, all bits at once. And PORTD is where you write the address to.

doaway:
Is it possible to read 2 or more address data in a single sketch ? Example: address 00000000 and 00000001 data

Yes, you can make the variable i static in your example code (with the initial value 0).

void loop {
static byte i = 0;
PORTD = i;
uint8_t value = 0;
for (uint8_t pin = RDPins42; pin <= RDPins49; pin++) {
  value <<= 1;
  value |= digitalRead(pin);
}
Serial.println(value, HEX);
i = i + 1;
if (i ==2 ) i = 0;
}

Or as below, if I put it all together:

void loop()
{ 
  for (uint8_t addr = 0; addr < 2; addr++) {
    PORTD = addr;
    // You might need a short delay here
    uint8_t data = PINL;
    Serial.println(data, HEX);
  }
}

Cheers!

Thanks alot!!!!

There do have a ENb and RWb in my code, I use pwm to generate square wave.
I also face a problem, when I perform digitalRead(pin), I do not know when and where the data is read. I only want to read the data when RWb = 1 (risingedge) and ENb = 0. And only want to read it once because the rest data is same.
Otherwise the data I read is void (maybe wrong data). so my code is

  Timer1.initialize(200);         // initialize timer1, and set a 200 us period
  Timer1.pwm(12, 512);                // setup ENb on Pin12
  Timer3.initialize(1000);       // 1 ms  
  Timer3.pwm(2,512);           // setup RWb on pin2

void loop {
static byte i = 0;
PORTD = i;
uint8_t value = 0;
PORTC = B01011001;     // WRITE DATA to the above address
for (uint8_t pin = RDPins42; pin <= RDPins49; pin++) {
  value <<= 1;
  value |= digitalRead(pin); // how can I write a condition here to read the data here ? when ENb = 0, RWb = 1, value |= digitalRead(pin);
}
Serial.println(value, HEX);
i = i + 1;
if (i ==2 ) {i = 0;}
delay(200);   // I am not sure should I put a delay here or  time = millis() to perform a delay?
}

doaway:
Thanks alot!!!!

There do have a ENb and RWb in my code, I use pwm to generate square wave.
I also face a problem, when I perform digitalRead(pin), I do not know when and where the data is read. I only want to read the data when RWb = 1 (risingedge) and ENb = 0. And only want to read it once because the rest data is same.
Otherwise the data I read is void (maybe wrong data). so my code is

You are welcome!

But you started off with one problem and now we are looking at something else. And from what I am reading you/we need to understand how your memory circuit connected to your Mega works. The PWM usage is very strange.

I recommend you (and others) when requesting help to:

  1. Post the problem with example code.
  2. Add some schematic/circuit if you are connecting to hardware.
  3. And try to be clear on the problem that you want help with.
    Otherwise there is a high risk that people back off from trying to help out. By extending with a new problem it becomes difficult for others to reuse any solution that is presented as the title becomes more and more irrelevant to the contents of the posting. If you change the contents I recommend a new posting. Solve one problem - go forward - reuse solutions!

So please tell us about the schematics of your hardware and how you think the your code works.

For now I think we have fixed your initial problem: Read multiple pins to create an integer value.

Cheers!

Sorry for inconvenience. Here is the hardware connection. I am trying to read multiple address memory data when ENb=0 and RWb = 1 and print it as HEX.
And I want to stop reading when each address data is printed. With your help, I can read multiple pins :slight_smile: . Thanks
I want to put condition here in my code(ENb=0, RWb=1) to read the correct data. Otherwise, if write FF to a certain address, when read it during ENb =1, RWb =1, it returns 00, so it is incorrect.

 #include "TimerThree.h"
  #include "TimerOne.h"
  #include <Metro.h>

  int ADPins[] = {22,23,24,25,26,27,28,29};  // DEFINE THE ADDRESS PINS
  int WRPins[] = {30,31,32,33,34,35,36,37};  // WRITE PINS PORTC
  int RDPins49 = 49;    // READ PINS INPUT
  int RDPins48 = 48;
  int RDPins47 = 47;
  int RDPins46 = 46;
  int RDPins45 = 45;
  int RDPins44 = 44;
  int RDPins43 = 43;
  int RDPins42 = 42;
  unsigned long time;
  long interval = 2000;
  int RWb_state;

  void setup()
  {
  for(int i = 22; i < 30; i++)
  {
  pinMode (ADPins[i], OUTPUT); 
  }
  for(int i = 30; i < 38; i++)
  {
  pinMode (WRPins[i], OUTPUT); 
  }

  pinMode (RDPins49, INPUT);
  pinMode (RDPins48, INPUT);
  pinMode (RDPins47, INPUT);
  pinMode (RDPins46, INPUT);
  pinMode (RDPins45, INPUT);
  pinMode (RDPins44, INPUT);
  pinMode (RDPins43, INPUT);
  pinMode (RDPins42, INPUT);

  Serial.begin(9600);
  DDRA = B11111111;   // Set PORTA (digital ) to outputs
  DDRC = B11111111;

  Timer1.initialize(200);         // initialize timer1, and set a 200 us period
  Timer1.pwm(12, 512);                // setup ENb on Pin12
  Timer3.initialize(1000);       // 1 ms  
  Timer3.pwm(2,512);           // setup RWb on pin2

  void loop {
  static byte i = 0;
  PORTA = i;
  uint8_t value = 0;
  PORTC = B01011001;     // WRITE DATA to the above address
  for (uint8_t pin = RDPins42; pin <= RDPins49; pin++) {
  value <<= 1;
  value |= digitalRead(pin); // how can I write a condition here to read the data here ? when ENb = 0, RWb = 1, value |= digitalRead(pin);
}
  Serial.println(value, HEX);
  i = i + 1;
  if (i ==2 ) {i = 0;}
  delay(200);   // I am not sure should I put a delay here or  time = millis() to perform a delay?
}