2018年3月1日 星期四

NOMCU 24bit I/O Expander 使用3個74HC165 與3個 74HC595

NOMCU 24bit I/O Expander 
使用3個74HC165 與3個 74HC595

//NodeMCU
//static const uint8_t D0   = 16;
//static const uint8_t D1   = 5;
//static const uint8_t D2   = 4;
//static const uint8_t D3   = 0;
//static const uint8_t D4   = 2;
//static const uint8_t D5   = 14;
//static const uint8_t D6   = 12;
//static const uint8_t D7   = 13;
//static const uint8_t D8   = 15;
//static const uint8_t D9   = 3;
//static const uint8_t D10  = 1;

#define SO  12      // SO-Serial output (Pin 9) of 74165 is connected to Nodemcu D6 = 12.
#define SH_LD  15   // SH/LD (Pin 1) of 74165 is connected to Nodemcu D8 = 15.
#define CLK  14      // CLK (Pin 2) of 74165 is connected to Nodemcu D5 = 14.

#include <ShiftRegister74HC595.h>
// create shift register object (number of shift registers, data pin, clock pin, latch pin)
ShiftRegister74HC595 sr (3, 16, 4, 5);

// variables will change:
int i = 0;         // variable for reading 8-bit data
long int PinState = 0;   //read the state of the SO pin
int Parallel_data = 0;//To store parallel data
int Parallel_data1 = 0;//To store parallel data


int read_shift_data()
{
  Parallel_data = 0;
  digitalWrite(CLK, LOW);
  /*Parallel data that will be entered through D0 - D7 Pins of 74165  **/
  digitalWrite(SH_LD, LOW);
  delay(5);
  /*******************  INHIBIT SECTION START HERE  *****************/
  digitalWrite(SH_LD, HIGH);
  /*******************  INHIBIT SECTION ENDS HERE ******************/
 
  /************** SERIAL SHIFT SECTION START HERE **********/
  //Read 8-bit data from 74HC165
  // Serial.print("Parallel Data1:");
   for(i=0;i<=23;i++)
    {
        PinState = digitalRead(SO);// read the state of the SO:
        digitalWrite(CLK, LOW);
        delay(1);
        digitalWrite(CLK, HIGH);
        delay(1);
        //Serial.print(PinState);
        Parallel_data = (Parallel_data<<1)|PinState; //Store the value in Parallel_data
    }
 return  Parallel_data;

}


void setup()
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
  // initialize the SH_LD & CLK pin as an output:
  pinMode(SH_LD, OUTPUT);
  pinMode(CLK, OUTPUT); 
  // initialize the SO-Serial output pin as an input:
  pinMode(SO, INPUT);
 
}

void loop()
{
   Parallel_data1=read_shift_data();
   String thisString1 = String(Parallel_data1, HEX);
   Serial.print("--switchBank1:");
   Serial.println (thisString1);

  // set all pins at once

  byte d1= (MSBFIRST, (Parallel_data1 / 65536));
  byte d2= (MSBFIRST, (Parallel_data1 / 256));
  byte d3= (MSBFIRST, (Parallel_data1 ));
  String thisString4 = String(d1, HEX);
  String thisString5 = String(d2, HEX);
  String thisString6 = String(d3, HEX);
  Serial.print("--switchBank1:");
  Serial.print (thisString4);
  Serial.print("---");
  Serial.print (thisString5);
  Serial.print("---");
  Serial.print (thisString6);
  Serial.println ();

  uint8_t pinValues[] = { d1, d2 , d3 };
  sr.setAll(pinValues);
 
  Serial.println();
  delay(10000);

  }

 
}



沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...