久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

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

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

--用3個單片機引腳讀取10個開關狀態

作者注:此方法并非擴展輸入腳最佳方法,如果需要大量擴展輸入腳的話(幾十甚至上百路),可以使用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狀態。但是Arduino引腳不多,傳統接法開關多了要占用很多引腳。減少引腳的方法有很多,可以選矩陣方式,編碼器方式,還有本文要介紹的分時復用開關法等。

特點:十個開關占用三個數據引腳,之后每增加十個開關就增加一個引腳。
4017是一塊十進制計數器,每輸入一個CLK脈沖,Q0~Q9輪流產生高電平。每時刻有且只有一個引腳高電平。
二極管防止多個開關閉合時,有的輸出端輸出高電平,有的輸出低電平,互相接上的話,會低電平引腳會干擾高電平腳的工作。
開關用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}; //按照總開關數定義。可能要改為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); //讀取第二個板子的狀態,地址放在i+10
digitalWrite(clk, HIGH);
delayMicroseconds(10);
digitalWrite(clk, LOW);
delayMicroseconds(10);
}

for(int i = 0; i < 10; i++) //循環打印KeyStatus數組,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://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 日韩一区二区三区四区五区 | 久久精品国产久精国产 | 狠狠av | 在线综合视频 | 亚洲一区在线日韩在线深爱 | 精品欧美二区 | 欧美成人a∨高清免费观看 色999日韩 | av网站在线播放 | 欧美亚洲国产一区二区三区 | www一级片 | 国产精品视频一区二区三区四区国 | 国产精品二区三区 | 国产精品中文字幕在线观看 | 特黄毛片 | 男女一区二区三区 | 国产精品一二三区在线观看 | 黄色大全免费看 | 天天看天天干 | 成人在线一区二区 | 亚洲欧美日韩电影 | 亚洲精品福利在线 | 亚洲国产一区二区在线 | 国产精品一区二区不卡 | 国产农村妇女精品一区 | 国产高清精品一区二区三区 | 久久精品网 | 特级黄色毛片 | 欧美5区| 91久久精品一区二区二区 | 久久精品高清视频 | 国产在线一区二区 | 一级片在线视频 | 免费的一级视频 | 久久久91精品国产一区二区三区 | 久久精品视频在线观看 | 亚洲精品国产一区 | 国产激情99| 中文字字幕在线中文乱码范文 | av片免费 | 国产亚洲精品久久情网 | 亚洲毛片一区二区 |