Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: trying to set pins to control an 11 bit parallel bus. Bit fiddle?

  1. #1

    trying to set pins to control an 11 bit parallel bus. Bit fiddle?

    I am working on replacing a Z80 controller board in an analog recording console with a teensy replacement. The z80 board handles some input from switches and sets electronic switches.

    I have sucessfully attached to the data busses. For now I am using an arduino Mega 2560 Rev 3 merely because I had one handy and it has plenty of pins.

    Bus lines:
    data 8 bits
    address 11 bits
    control lines 4 bits

    I am now working on a new sketch to pump addresses in and attaching a jumper wire to the relevant CS pins on the hardware to sniff out addresses. This will help me build up a table of addresses to access each part of the console.

    I know this is just a small part of what I am trying to acheive.

    So my problem. I am trying to cycle through all possible addresses, 11 bits a total of 2048 addresses.

    I have based this on an example @PaulStoffregen gave me for controlling MUX lines - https://forum.pjrc.com/threads/67322...ght=bit+fiddle

    This code runs in a for loop incrementing j upto 2048:

    Code:
        
        digitalWrite(ADDR[0], ((j & 0x01) ? HIGH : LOW));               // bit-fiddle to set the i-th pin to HIGH IFF binary version of j
        digitalWrite(ADDR[1], ((j & 0x02) ? HIGH : LOW));
        digitalWrite(ADDR[2], ((j & 0x04) ? HIGH : LOW));
        digitalWrite(ADDR[3], ((j & 0x08) ? HIGH : LOW));
        digitalWrite(ADDR[4], ((j & 0x16) ? HIGH : LOW));
        digitalWrite(ADDR[5], ((j & 0x32) ? HIGH : LOW));
        digitalWrite(ADDR[6], ((j & 0x64) ? HIGH : LOW));
        digitalWrite(ADDR[7], ((j & 0x128) ? HIGH : LOW));
        digitalWrite(ADDR[8], ((j & 0x256) ? HIGH : LOW));
        digitalWrite(ADDR[9], ((j & 0x512) ? HIGH : LOW));
        digitalWrite(ADDR[10], ((j & 0x1024) ? HIGH : LOW));
    
    
        Serial.print(F("pin output:---- "));     // order reversed for visual
        Serial.print((j & 0x1024) ? "1" : "0");
        Serial.print((j & 0x512) ? "1" : "0");
        Serial.print((j & 0x256) ? "1" : "0");
        Serial.print((j & 0x128) ? "1" : "0");
        Serial.print((j & 0x64) ? "1" : "0");
        Serial.print((j & 0x32) ? "1" : "0");
        Serial.print((j & 0x16) ? "1" : "0");
        Serial.print((j & 0x8) ? "1" : "0");
        Serial.print((j & 0x04) ? "1" : "0");
        Serial.print((j & 0x02) ? "1" : "0");    
        Serial.print((j & 0x01) ? "1" : "0");
        Serial.print(F(" ---- "));
    my code then continues to print out the data direct from j:

    Code:
        Serial.print(F(" ----- "));
        Serial.print(j, BIN);
        Serial.println(F(" ----- "));
    I have made the assumption the technique of comparing j to its binary position, each time doubled is the correct approach, but I get odd results when using this "bit fiddle". Serial.print(j, BIN) however gives me the correct visualisation in serial monitor that I am expecting. What am I doing wrong? I want to be sure that my pins are outputting the correct address before I go further!
    Last edited by snowsh; 09-20-2021 at 11:02 PM.

  2. #2
    doh!

    of course it should be written in hex....

    Code:
        digitalWrite(ADDR[0], ((j & 0x01) ? HIGH : LOW));               // bit-fiddle to set the i-th pin to HIGH IFF binary version of j
        digitalWrite(ADDR[1], ((j & 0x02) ? HIGH : LOW));
        digitalWrite(ADDR[2], ((j & 0x04) ? HIGH : LOW));
        digitalWrite(ADDR[3], ((j & 0x08) ? HIGH : LOW));
        digitalWrite(ADDR[4], ((j & 0x10) ? HIGH : LOW));
        digitalWrite(ADDR[5], ((j & 0x20) ? HIGH : LOW));
        digitalWrite(ADDR[6], ((j & 0x40) ? HIGH : LOW));
        digitalWrite(ADDR[7], ((j & 0x80) ? HIGH : LOW));
        digitalWrite(ADDR[8], ((j & 0x100) ? HIGH : LOW));
        digitalWrite(ADDR[9], ((j & 0x200) ? HIGH : LOW));
        digitalWrite(ADDR[10], ((j & 0x400) ? HIGH : LOW));
        //    digitalWrite(ADDR[11], ((j & 0x08) ? HIGH : LOW));
    
    
    
        Serial.print(F("pin output:---- "));        // reversed for visual
        Serial.print((j & 0x400) ? "1" : "0");
        Serial.print((j & 0x200) ? "1" : "0");
        Serial.print((j & 0x100) ? "1" : "0");
        Serial.print((j & 0x80) ? "1" : "0");
        Serial.print((j & 0x40) ? "1" : "0");
        Serial.print((j & 0x20) ? "1" : "0");
        Serial.print((j & 0x10) ? "1" : "0");
        Serial.print((j & 0x8) ? "1" : "0");
        Serial.print((j & 0x04) ? "1" : "0");
        Serial.print((j & 0x02) ? "1" : "0");    
        Serial.print((j & 0x01) ? "1" : "0");
        Serial.print(F(" ---- "));

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,750
    Code:
        digitalWrite(ADDR[6], ((j & 0x60) ? HIGH : LOW));
        digitalWrite(ADDR[7], ((j & 0x40) ? HIGH : LOW));
    Presumably should be:
    Code:
        digitalWrite(ADDR[6], ((j & 0x40) ? HIGH : LOW));
        digitalWrite(ADDR[7], ((j & 0x80) ? HIGH : LOW));
    Pete

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,095
    The number bases are mixed up

    The values doubled are in DECIMAL - but prepended with "0x" turning them into HEX.

    The Decimal values {1,2,4,8,16,...} are good - remove the '0x' before them.

    Code:
    void setup() {
      Serial.begin(115200);
      while (!Serial && millis() < 4000 );
      Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
      for ( int jj=0; jj<2048; jj++ ) {
        showJ( jj );
      }
    }
    
    void loop() {
    
    }
    
    void showJ( int j )
    {
      Serial.print(F("pin output:---- "));     // order reversed for visual
      Serial.print((j & 1024) ? "1" : "0");
      Serial.print((j & 512) ? "1" : "0");
      Serial.print((j & 256) ? "1" : "0");
      Serial.print((j & 128) ? "1" : "0");
      Serial.print((j & 64) ? "1" : "0");
      Serial.print((j & 32) ? "1" : "0");
      Serial.print((j & 16) ? "1" : "0");
      Serial.print((j & 8) ? "1" : "0");
      Serial.print((j & 04) ? "1" : "0");
      Serial.print((j & 02) ? "1" : "0");
      Serial.print((j & 1) ? "1" : "0");
      Serial.print(F(" ---- "));
    
      Serial.print(F(" ----- "));
      Serial.print(j, BIN);
      Serial.println(F(" ----- "));
    }

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,095
    Posts Crissed and Crossed ...

    But as demonstrated it was easy to write a sketch to visualize the problem and correction to see correct behavior.

  6. #6
    yes thanks guys! All a bit mixed up, but the confusion was writing decimal when it should have been hex......

    getting there.

  7. #7
    correct:

    Code:
        digitalWrite(ADDR[0], ((j & 0x01) ? HIGH : LOW));               // bit-fiddle to set the i-th pin to HIGH IFF binary version of j
        digitalWrite(ADDR[1], ((j & 0x02) ? HIGH : LOW));
        digitalWrite(ADDR[2], ((j & 0x04) ? HIGH : LOW));
        digitalWrite(ADDR[3], ((j & 0x08) ? HIGH : LOW));
        digitalWrite(ADDR[4], ((j & 0x10) ? HIGH : LOW)); 
        digitalWrite(ADDR[5], ((j & 0x20) ? HIGH : LOW));
        digitalWrite(ADDR[6], ((j & 0x40) ? HIGH : LOW));
        digitalWrite(ADDR[7], ((j & 0x80) ? HIGH : LOW));
        digitalWrite(ADDR[8], ((j & 0x100) ? HIGH : LOW));
        digitalWrite(ADDR[9], ((j & 0x200) ? HIGH : LOW));
        digitalWrite(ADDR[10], ((j & 0x400) ? HIGH : LOW));
    
    
    
        Serial.print(F("pin output:---- "));        // reversed for visual
        Serial.print((j & 0x400) ? "1" : "0");
        Serial.print((j & 0x200) ? "1" : "0");
        Serial.print((j & 0x100) ? "1" : "0");
        Serial.print((j & 0x80) ? "1" : "0");
        Serial.print((j & 0x40) ? "1" : "0");
        Serial.print((j & 0x20) ? "1" : "0");
        Serial.print((j & 0x10) ? "1" : "0");
        Serial.print((j & 0x8) ? "1" : "0");
        Serial.print((j & 0x04) ? "1" : "0");
        Serial.print((j & 0x02) ? "1" : "0");    
        Serial.print((j & 0x01) ? "1" : "0");
        Serial.print(F(" ---- "));

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •