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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 10707|回復: 2
收起左側

Arduino學習1-使用4017數字集成塊擴展Arduino開關輸入端

[復制鏈接]
ID:114115 發表于 2016-4-14 02:42 | 顯示全部樓層 |閱讀模式

--用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.
5iErfC9.png
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路撥動式小型開關,或者自己選擇其他開關形式。

電路工作原理:
  • 先在RST(4017的復位腳MR)發出一個脈沖,使4017復位。
  • 此時有且只有Q0輸出高電平(Q0對應開關S1,Q9對應開關S10),讀取一次輸出信號DATA。如果第一個開關S1閉合了,應該DATA得到高電平;如果S1斷開了,就DATA得到低電平。此時記DATA結果對應第一個開關S1的狀態。
  • 給CLK輸出一個脈沖,讓4017移位,有且只有Q1輸出高電平(Q0,Q2~Q9均為低電平)。讀取DATA。得到S2狀態。
  • 不斷給CLK脈沖。總共給10次脈沖,讓4017由Q0移動到Q9,完成一次開關遍歷,每次移動獲取一次DATA狀態。存為S1~S10狀態。

電路原理圖如圖:
00300057evq6q1y11llve1.gif

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

關于在一塊Arduino上使用多塊模塊:每增加一塊模塊,可以增加十路開關(當然你也可以使用兩塊4017做成行列矩陣控制100個開關。不過那個就屬于另外話題了)。增加的方式是將兩塊模塊的RST,CLK,VCC,GND接在一起,接到單片機的相應引腳,然后兩個模塊的DATA腳分別接單片機兩個IO口。
003001z4jccaasf8bs8p3u.gif
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);
}
洞洞板實物圖:

1024425oh7mddd5epahpa5.jpg
1024418fyffco8yz33zf7c.jpg
102439yj3artmurfhm2jmt.jpg
回復

使用道具 舉報

ID:200272 發表于 2017-5-13 13:48 | 顯示全部樓層
你好,請問關于那個rst 和clk 的設置的問題,一開始給了一個高電平后為什么還要再給一個低電平?
回復

使用道具 舉報

ID:200272 發表于 2017-5-13 13:46 | 顯示全部樓層
請問digitalWrite(rst, HIGH); 和digitalWrite(rst, LOW); 這兩句是什么意思前一個是給一個高電平,4017復位那么后面那個給一個低電平又是什么情況?還有后面設計的CLK時的那兩句,主要是不懂為什么還要再給一個低電平
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 中文字幕成人av | 欧美日韩在线免费观看 | 日韩成人影院 | 久久精品二区 | 狠狠色综合久久婷婷 | 正在播放国产精品 | 一区二区av | 天天色天天 | 天天爱天天操 | 久久精品国产一区二区电影 | 亚洲情综合五月天 | 精品免费国产一区二区三区四区 | 中国一级毛片免费 | 一区二区三区在线播放 | 999久久精品| 日韩在线免费 | 国产福利精品一区 | 亚洲第一福利视频 | 不卡av电影在线播放 | 久久久精品一区 | 久草在线| 国产一区不卡 | 欧美成人h版在线观看 | 国产h在线| 国产人免费人成免费视频 | 久草在线在线精品观看 | 亚洲精品一区二三区不卡 | 一区二区三区在线 | 欧 | 欧美中文字幕一区二区三区亚洲 | 五月婷婷丁香 | 久久伊人免费视频 | 亚洲视频一区在线观看 | 午夜视频网站 | 请别相信他免费喜剧电影在线观看 | 午夜精品一区二区三区在线视频 | 超碰最新在线 | 成人黄色网址大全 | 91观看 | 91在线视频观看免费 | 久久久久亚洲视频 | 交专区videossex农村 |