標題: Arduino學習1-使用4017數(shù)字集成塊擴展Arduino開關輸入端 [打印本頁]

作者: 51黑學者    時間: 2016-4-14 02:42
標題: Arduino學習1-使用4017數(shù)字集成塊擴展Arduino開關輸入端

--用3個單片機引腳讀取10個開關狀態(tài)

作者注:此方法并非擴展輸入腳最佳方法,如果需要大量擴展輸入腳的話(幾十甚至上百路),可以使用74HC165或者CD4021做shiftin。而不是本文的4017。
http://playground.arduino.cc/Code/ShiftRegSN74HC165N
http://www.arduino.cc/en/Tutorial/ShiftIn


The SN74HC165N is an 8-bit parallel-load or serial-in shift registers with complementary serial outputs available from the last stage. When the parallel load (PL) input is LOW, parallel data from the D0 to D7 inputs are loaded into the register asynchronously.  When PL is HIGH, data enters the register serially at the Ds input and shifts one place to the right (Q0 → Q1 → Q2, etc.) with each positive-going clock transition. This feature allows parallel-to-serial converter expansion by tying the Q7 output to the DS input of the succeeding stage.
Breadboard SchematicThe following image shows 10 pushbuttons wired to two SN74HC165N input shift registers. Note that the 6 unused input pins are grounded.

CodeThe following code demonstrates reading in 16 digital states from a pair of daisy-chained SN74HC165N shift registers while using only 4 digital pins on the Arduino.
  1. /*
  2. * SN74HC165N_shift_reg
  3. *
  4. * Program to shift in the bit values from a SN74HC165N 8-bit
  5. * parallel-in/serial-out shift register.
  6. *
  7. * This sketch demonstrates reading in 16 digital states from a
  8. * pair of daisy-chained SN74HC165N shift registers while using
  9. * only 4 digital pins on the Arduino.
  10. *
  11. * You can daisy-chain these chips by connecting the serial-out
  12. * (Q7 pin) on one shift register to the serial-in (Ds pin) of
  13. * the other.
  14. *
  15. * Of course you can daisy chain as many as you like while still
  16. * using only 4 Arduino pins (though you would have to process
  17. * them 4 at a time into separate unsigned long variables).
  18. *
  19. */

  20. /* How many shift register chips are daisy-chained.
  21. */
  22. #define NUMBER_OF_SHIFT_CHIPS   2

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

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

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

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

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

  40. BYTES_VAL_T pinValues;
  41. BYTES_VAL_T oldPinValues;

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

  50.     /* Trigger a parallel Load to latch the state of the data lines,
  51.     */
  52.     digitalWrite(clockEnablePin, HIGH);
  53.     digitalWrite(ploadPin, LOW);
  54.     delayMicroseconds(PULSE_WIDTH_USEC);
  55.     digitalWrite(ploadPin, HIGH);
  56.     digitalWrite(clockEnablePin, LOW);

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

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

  66.         /* Pulse the Clock (rising edge shifts the next bit).
  67.         */
  68.         digitalWrite(clockPin, HIGH);
  69.         delayMicroseconds(PULSE_WIDTH_USEC);
  70.         digitalWrite(clockPin, LOW);
  71.     }

  72.     return(bytesVal);
  73. }

  74. /* Dump the list of zones along with their current status.
  75. */
  76. void display_pin_values()
  77. {
  78.     Serial.print("Pin States:");

  79.     for(int i = 0; i < DATA_WIDTH; i++)
  80.     {
  81.         Serial.print("  Pin-");
  82.         Serial.print(i);
  83.         Serial.print(": ");

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

  88.         Serial.print("");
  89.     }

  90.     Serial.print("");
  91. }

  92. void setup()
  93. {
  94.     Serial.begin(9600);

  95.     /* Initialize our digital pins...
  96.     */
  97.     pinMode(ploadPin, OUTPUT);
  98.     pinMode(clockEnablePin, OUTPUT);
  99.     pinMode(clockPin, OUTPUT);
  100.     pinMode(dataPin, INPUT);

  101.     digitalWrite(clockPin, LOW);
  102.     digitalWrite(ploadPin, HIGH);

  103.     /* Read in and display the pin states at startup.
  104.     */
  105.     pinValues = read_shift_regs();
  106.     display_pin_values();
  107.     oldPinValues = pinValues;
  108. }

  109. void loop()
  110. {
  111.     /* Read the state of all zones.
  112.     */
  113.     pinValues = read_shift_regs();

  114.     /* If there was a chage in state, display which ones changed.
  115.     */
  116.     if(pinValues != oldPinValues)
  117.     {
  118.         Serial.print("*Pin value change detected*");
  119.         display_pin_values();
  120.         oldPinValues = pinValues;
  121.     }

  122.     delay(POLL_DELAY_MSEC);
  123. }
復制代碼


使用Arduino做制作,需要有一組開關控制Arduino狀態(tài)。但是Arduino引腳不多,傳統(tǒng)接法開關多了要占用很多引腳。減少引腳的方法有很多,可以選矩陣方式,編碼器方式,還有本文要介紹的分時復用開關法等。

特點:十個開關占用三個數(shù)據(jù)引腳,之后每增加十個開關就增加一個引腳。
4017是一塊十進制計數(shù)器,每輸入一個CLK脈沖,Q0~Q9輪流產(chǎn)生高電平。每時刻有且只有一個引腳高電平。
二極管防止多個開關閉合時,有的輸出端輸出高電平,有的輸出低電平,互相接上的話,會低電平引腳會干擾高電平腳的工作。
開關用10路撥動式小型開關,或者自己選擇其他開關形式。

電路工作原理:

電路原理圖如圖:


洞洞板圖(由于引腳多,不建議面包板制作。)


關于在一塊Arduino上使用多塊模塊:每增加一塊模塊,可以增加十路開關(當然你也可以使用兩塊4017做成行列矩陣控制100個開關。不過那個就屬于另外話題了)。增加的方式是將兩塊模塊的RST,CLK,VCC,GND接在一起,接到單片機的相應引腳,然后兩個模塊的DATA腳分別接單片機兩個IO口。

Arduino程序例子:

const int rst = 2; //板子的RST腳接Arduino的D4口(自定義)
const int clk = 3; //板子的CLK腳接Arduino的D3口(自定義)
const int data1 = 4; //板子的DATA腳接Arduino的D2口(自定義)
//const int data2 = 5; //如果有第二塊板子的話,兩塊板子共用RST和CLK引腳。DATA接Arduino的D5口,第三塊板子可以類推接D6口(自定義)
void setup()
{
Serial.begin(9600);
pinMode(rst, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(data1, INPUT);
//pinMode(data2,INPUT); //如果有第二塊板子的話要定義IO
}
void loop()
{
int KeyStatus[10] = {0}; //按照總開關數(shù)定義?赡芤臑20,30等
digitalWrite(rst, HIGH);
delayMicroseconds(10); //所有delayMicroseconds(10);均是給4017一個反應時間。
digitalWrite(rst, LOW);
delayMicroseconds(10);
for(int i = 0; i < 10; i++)
{
KeyStatus = digitalRead(data1);
//KeyStatus[i+10] = digitalRead(data2); //讀取第二個板子的狀態(tài),地址放在i+10
digitalWrite(clk, HIGH);
delayMicroseconds(10);
digitalWrite(clk, LOW);
delayMicroseconds(10);
}

for(int i = 0; i < 10; i++) //循環(huán)打印KeyStatus數(shù)組,i<10可能要改為i<20,30等
{
Serial.print(KeyStatus);
Serial.print("        ");
}
Serial.println();
delay(100);
}
洞洞板實物圖:





作者: 小飛裕    時間: 2017-5-13 13:46
請問digitalWrite(rst, HIGH); 和digitalWrite(rst, LOW); 這兩句是什么意思前一個是給一個高電平,4017復位那么后面那個給一個低電平又是什么情況?還有后面設計的CLK時的那兩句,主要是不懂為什么還要再給一個低電平
作者: 小飛裕    時間: 2017-5-13 13:48
你好,請問關于那個rst 和clk 的設置的問題,一開始給了一個高電平后為什么還要再給一個低電平?




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1