Go Down

Topic: how to get digital 8bits data in the same and group the data in a digital value (Read 1 time) previous topic - next topic

chwp

i uses HCTL-2020 for decoding the INCREMENTAL ROTARY encoder signal . it has 8 pins to provide a 8 bits data. i don't know how to get the data in the same time and group the data in a DEC value

johnwasser

Code: [Select]

int dataPins[8] = {2,3,4,5,6,7,8,9};

void setup()
    {
    for (int i = 0; i< 8; i++)
         pinMode(dataPins[i], INPUT);
   }

void loop()
   {
   int value = 0;

   for (int i=0; i<8; i++)
       {
       value <<= 1;  // shift value left one bit
       value |= digitalRead(dataPins[i]);  // get a bit from a data pin
      }

// Do something with value
   }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

chwp

johnwasser thx for your help . i found the result was 1111111 which was not the correct value from the decoder IC output. is it the shift occurred any problem?  My latest program is following: is it have any problem?

int dataPins[8] = {30,31,32,33,34,35,36,37};
int SELPin = 38; //set SEL pin
int OEPin = 39; //set OE pin
int value;
int valuelow ;
int valuehigh ;
void setup()
    {
      pinMode (SELPin, OUTPUT); //set SEL is output to control the decoder ic
   pinMode (OEPin, OUTPUT); // set OE?is output to control the decoder ic

    for (int i = 30; i<= 37; i++)
         pinMode(dataPins, INPUT);
         Serial.begin (9600);
   }

void loop()
   {
     digitalWrite(SELPin, HIGH); // SEL = 1    when SEL and OE are 1 that means tell the ic ready to give data
   digitalWrite(OEPin, HIGH); // OE = 1

   digitalWrite(SELPin, LOW); // SEL = 0   when SEL and OE are 0 that means tell the ic send the low 8 bits data
   digitalWrite(OEPin, LOW); // OE = 0

   valuelow=0;
   valuehigh=0;

   for (int i=30; i<=37; i++)
       {
       valuelow << 1;  // shift value left one bit
       valuelow |= digitalRead(dataPins);  // get a bit from a data pin
       }
/*     digitalWrite(SELPin, HIGH); // SEL = 1     when SEL is 1 and OE are 0 that means tell the ic send the high 8 bits data
   digitalWrite(OEPin, LOW); // OE = 0
     
      for (int i=30; i<=37; i++)
      {
        valuehigh <<= 1;  // shift value left one bit
       valuehigh |= digitalRead(dataPins);  // get a bit from a data pin
    }
   // value=(valuehigh*128)+valuelow;       */
    Serial.println(valuelow,BIN);
   }

johnwasser

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

vegeaur


What are you using to provide the clock to the HCTL-2020?


hello,I was using HCTL-2020 for decode 100 pulse per round encoder on maxon motor.

Code: [Select]

//int Data_Pins[8] = {46,47,48,49,50,51,52,53};
int SEL_Pin = 44; //set SEL pin
int OE_Pin = 43; //set OE pin
int RST_Pin = 42;
int CLK_Pin = 11;
//int value;
//int valuelow ;
//int valuehigh ;

void setup(){
    int pin; 
   
    pinMode (SEL_Pin, OUTPUT);
    pinMode (OE_Pin, OUTPUT);
    pinMode (RST_Pin, OUTPUT);
   
    digitalWrite(RST_Pin, LOW);
    delay(100);
    digitalWrite(RST_Pin, HIGH);

    pinMode (CLK_Pin, OUTPUT);
    TCCR1A = B01000011; // Fast PWM change at OCR1A
    TCCR1B = B11001;  // System clock
    OCR1A = 0; // 8 MHz
       
    for (pin=46; pin <= 53; pin++){
        pinMode (pin, INPUT);
    }
   
    Serial.begin (9600);
    Serial.print ("Start");
   
}

void loop(){
    int i;
int j;
        int s;


digitalWrite(SEL_Pin, LOW); // SEL = 0
digitalWrite(OE_Pin, LOW); // OE = 0

delay(100);

String StrCombine ;

for (i=46; i <= 53; i++){
    StrCombine += digitalRead(i);
}

digitalWrite(SEL_Pin, HIGH); // SEL = 1
digitalWrite(OE_Pin, LOW); // OE = 0

        delay(100);

for (j=46; j <= 53; j++){
StrCombine += digitalRead(j);
}
    //   StrCombine=s;

        digitalWrite(SEL_Pin, HIGH); // SEL = 1
digitalWrite(OE_Pin, HIGH); // OE = 1

Serial.println(StrCombine);

}


I got the code in this Forum.But it can not run on my mega 2560.
the output is 0000000011111111.
Could you help me,thanks a lot!

CrossRoads

The data is sitting on 8 pins waiting to be read?
Do you have an entire port available?

incoming Value = PINB;
for instance reads all the pins of Port B at one time.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

johnwasser

Try this version:
Code: [Select]

const int Data_Pins[8] = {46,47,48,49,50,51,52,53};
int SEL_Pin = 44; //set SEL pin
int OE_Pin = 43;  //set OE pin
int RST_Pin = 42;
int CLK_Pin = 11;

void setup() {
  pinMode (SEL_Pin, OUTPUT);
  pinMode (OE_Pin, OUTPUT);
  pinMode (RST_Pin, OUTPUT);

  // Reset the chip
  digitalWrite(RST_Pin, LOW);
  delay(100);
  digitalWrite(RST_Pin, HIGH);

  pinMode (CLK_Pin, OUTPUT);
  TCCR1A = B01000011; // Fast PWM change at OCR1A
  TCCR1B = B11001;  // System clock
  OCR1A = 0; // 8 MHz

for (int i=0; i<8; i++) {
    pinMode(Data_Pins[i], INPUT);
  }

  Serial.begin (9600);
  Serial.print ("Start");
}

void loop() {
  unsigned long value = 0;
 
  // Read the high 8 bits
  digitalWrite(SEL_Pin, LOW); // SEL = 0
  digitalWrite(OE_Pin, LOW); // OE = 0
  delay(100);

  for (int i=0; i<8; i++){
    value = (value << 1) | digitalRead(Data_Pins[i]);
  }

  // Read the low 8 bits
  digitalWrite(SEL_Pin, HIGH); // SEL = 1
  digitalWrite(OE_Pin, LOW); // OE = 0
  delay(100);

  for (int i=0; i<8; i++){
    value = (value << 1) | digitalRead(Data_Pins[i]);
  }

  digitalWrite(SEL_Pin, HIGH); // SEL = 1
  digitalWrite(OE_Pin, HIGH); // OE = 1

  Serial.println(value, BIN);
  delay(1000);  // Keep the data rate reasonable
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

vegeaur


Try this version:...

Thank you for help!

I have tried your code, and i get output like this, which is like some random integer:
11111111
1000000011011111
11111111
11001111
1000000011101111
1000000011001111
1000000011001111
1000000011001111
11001111
1000000011111111
1000000011111111
11001111
1000000011111111
11101111

And is my clock set right?

Go Up