Цепочка из 3 x 74hc165

Тема в разделе "Схемотехника, компоненты, модули", создана пользователем Black_Parrot, 24 фев 2014.

  1. Black_Parrot

    Black_Parrot Нуб

    Привет, ребят, помогите пожалуйста разобраться с регистрами ввода.
    Сначала все было отлично, когда их было 2, но когда я попытался подрубить 3-й, то все перестало работать.
    Вот такая вот схема(конденсаторы керамические добавил, так как была надежда, что они исправят ситуацию)
    Кнопки подключаю к разным регистрам для тестов.

    Код:
    Код (Text):
    /* How many shift register chips are daisy-chained.
    */
    #define NUMBER_OF_SHIFT_CHIPS  3

    /* Width of data (how many ext lines).
    */
    #define DATA_WIDTH  NUMBER_OF_SHIFT_CHIPS * 8

    /* Width of pulse to trigger the shift register to read and latch.
    */
    #define PULSE_WIDTH_USEC  5

    /* Optional delay between shift register reads.
    */
    #define POLL_DELAY_MSEC  20

    /* You will need to change the "int" to "long" If the
    * NUMBER_OF_SHIFT_CHIPS is higher than 2.
    */
    #define BYTES_VAL_T unsigned long

    int ploadPin        = 8;  // Connects to Parallel load pin the 165
    int clockEnablePin  = 9;  // Connects to Clock Enable pin the 165
    int dataPin        = 11; // Connects to the Q7 pin the 165
    int clockPin        = 12; // Connects to the Clock pin the 165

    BYTES_VAL_T pinValues;
    BYTES_VAL_T oldPinValues;

    /* This function is essentially a "shift-in" routine reading the
    * serial Data from the shift register chips and representing
    * the state of those pins in an unsigned integer (or long).
    */
    BYTES_VAL_T read_shift_regs()
    {
        byte bitVal;
        BYTES_VAL_T bytesVal = 0;

        /* Trigger a parallel Load to latch the state of the data lines,
        */
        digitalWrite(clockEnablePin, HIGH);
        digitalWrite(ploadPin, LOW);
        delayMicroseconds(PULSE_WIDTH_USEC);
        digitalWrite(ploadPin, HIGH);
        digitalWrite(clockEnablePin, LOW);

        /* Loop to read each bit value from the serial out line
        * of the SN74HC165N.
        */
        for(int i = 0; i < DATA_WIDTH; i++)
        {
            bitVal = digitalRead(dataPin);

            /* Set the corresponding bit in bytesVal.
            */
            bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

            /* Pulse the Clock (rising edge shifts the next bit).
            */
            digitalWrite(clockPin, HIGH);
            delayMicroseconds(PULSE_WIDTH_USEC);
            digitalWrite(clockPin, LOW);
        }

        return(bytesVal);
    }

    /* Dump the list of zones along with their current status.
    */
    void display_pin_values()
    {
        Serial.print("Pin States:\r\n");

        for(int i = 0; i < DATA_WIDTH; i++)
        {
            Serial.print("  Pin-");
            Serial.print(i);
            Serial.print(": ");

            if((pinValues >> i) & 1)
                Serial.print("HIGH");
            else
                Serial.print("LOW");

            Serial.print("\r\n");
        }

        Serial.print("\r\n");
    }

    void setup()
    {
        Serial.begin(9600);

        /* Initialize our digital pins...
        */
        pinMode(ploadPin, OUTPUT);
        pinMode(clockEnablePin, OUTPUT);
        pinMode(clockPin, OUTPUT);
        pinMode(dataPin, INPUT);

        digitalWrite(clockPin, LOW);
        digitalWrite(ploadPin, HIGH);

        /* Read in and display the pin states at startup.
        */
        pinValues = read_shift_regs();
        display_pin_values();
        oldPinValues = pinValues;
    }

    void loop()
    {
        /* Read the state of all zones.
        */
        pinValues = read_shift_regs();

        /* If there was a chage in state, display which ones changed.
        */
        if(pinValues != oldPinValues)
        {
            Serial.print("*Pin value change detected*\r\n");
            display_pin_values();
            oldPinValues = pinValues;
        }

        delay(POLL_DELAY_MSEC);
    }
    Пока стояло 2 регистра и в коде было
    NUMBER_OF_SHIFT_CHIPS 2
    Все работало.

    Как только я подключаю 3-й регистр и меняю код, получаю:

    Pin0(Последний пин последнего регистра) - ok
    pin1 - ok
    pin2 - ok
    pin3 - ok
    pin 4 - ok
    5- ok
    6 - Работает, будто одновременно нажаты 6,7
    7 - Работает, будто одновременно нажаты 6,7
    ---
    Второй регистр
    8 -ok
    9 - ok
    ...
    14 - ok
    15 - Работает будто 15-23 пины нажаты

    ---
    Первый регистр(с выходом на ардуино)
    14 - 23 пины вообще не работают.

    В чем может быть проблема? Причем не могу найти примеров кода с 3 и больше регистрами. Везде только фразы - можете хоть 1000 подключить. ))))
     

    Вложения:

    • shifti22n.jpg
      shifti22n.jpg
      Размер файла:
      258,7 КБ
      Просмотров:
      928
  2. Black_Parrot

    Black_Parrot Нуб

    Пока безуспешно пытаюсь решить проблему. Я попробовал другие сдвиговые регистры, купленные в амперке. До этого были от другого производителя(in74hc165AN) - Никакой разницы. Первый регистр не работает, некоторые пины у второго работают некорректно.
    При использовании 2-х регистров все работает.
    Код (Text):
    /*
    * SN74HC165N_shift_reg
    *
    * Program to shift in the bit values from a SN74HC165N 8-bit
    * parallel-in/serial-out shift register.
    *
    * This sketch demonstrates reading in 16 digital states from a
    * pair of daisy-chained SN74HC165N shift registers while using
    * only 4 digital pins on the Arduino.
    *
    * You can daisy-chain these chips by connecting the serial-out
    * (Q7 pin) on one shift register to the serial-in (Ds pin) of
    * the other.
    *
    * Of course you can daisy chain as many as you like while still
    * using only 4 Arduino pins (though you would have to process
    * them 4 at a time into separate unsigned long variables).
    *
    */

    /* How many shift register chips are daisy-chained.
    */
    #define NUMBER_OF_SHIFT_CHIPS  3

    /* Width of data (how many ext lines).
    */
    #define DATA_WIDTH  NUMBER_OF_SHIFT_CHIPS * 8

    /* Width of pulse to trigger the shift register to read and latch.
    */
    #define PULSE_WIDTH_USEC  5

    /* Optional delay between shift register reads.
    */
    #define POLL_DELAY_MSEC  1

    /* You will need to change the "int" to "long" If the
    * NUMBER_OF_SHIFT_CHIPS is higher than 2.
    */
    #define BYTES_VAL_T unsigned long

    int ploadPin        = 8;  // Connects to Parallel load pin the 165
    int dataPin        = 11; // Connects to the Q7 pin the 165
    int clockPin        = 12; // Connects to the Clock pin the 165

    BYTES_VAL_T pinValues;
    BYTES_VAL_T oldPinValues;

    /* This function is essentially a "shift-in" routine reading the
    * serial Data from the shift register chips and representing
    * the state of those pins in an unsigned integer (or long).
    */
    BYTES_VAL_T read_shift_regs()
    {
        byte bitVal;
        BYTES_VAL_T bytesVal = 0;

        /* Trigger a parallel Load to latch the state of the data lines,
        */
        digitalWrite(ploadPin, LOW);
        delayMicroseconds(PULSE_WIDTH_USEC);
        digitalWrite(ploadPin, HIGH);

        /* Loop to read each bit value from the serial out line
        * of the SN74HC165N.
        */
        for(int i = 0; i < DATA_WIDTH; i++)
        {
            bitVal = digitalRead(dataPin);

            /* Set the corresponding bit in bytesVal.
            */
            bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));

            /* Pulse the Clock (rising edge shifts the next bit).
            */
            digitalWrite(clockPin, HIGH);
            delayMicroseconds(PULSE_WIDTH_USEC);
            digitalWrite(clockPin, LOW);
        }

        return(bytesVal);
    }

    /* Dump the list of zones along with their current status.
    */
    void display_pin_values()
    {
        for(int i = 0; i < DATA_WIDTH; i++)
        {
            if((pinValues >> i) & 1)
                Serial.print("1");
            else
                Serial.print("0");

            Serial.print(",");
        }

        Serial.println();
    }

    void setup()
    {
        Serial.begin(9600);

        /* Initialize our digital pins...
        */
        pinMode(ploadPin, OUTPUT);
        pinMode(clockPin, OUTPUT);
        pinMode(dataPin, INPUT);

        digitalWrite(clockPin, LOW);
        digitalWrite(ploadPin, HIGH);

        /* Read in and display the pin states at startup.
        */
        pinValues = read_shift_regs();
        display_pin_values();
        oldPinValues = pinValues;
    }

    void loop()
    {
        /* Read the state of all zones.
        */
        pinValues = read_shift_regs();

        /* If there was a chage in state, display which ones changed.
        */
        if(pinValues != oldPinValues)
        {
        //  Serial.print("*Pin value change detected*\r\n");
            display_pin_values();
            oldPinValues = pinValues;
        }

        delay(POLL_DELAY_MSEC);
    }
    Наверное, придется кнопки вешать, через 595-е, изменяя код. :/ Если такой же проблемы не будет- пока не пробовал ))))
     

    Вложения:

  3. Black_Parrot

    Black_Parrot Нуб

    Может проблема именно в Decoupling ?? Как я понял, это довольно тонкая штука - может я не так поставил конденсаторы?