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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 10480|回復: 2
打印 上一主題 下一主題
收起左側

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

電路工作原理:
  • 先在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狀態。

電路原理圖如圖:


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


關于在一塊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);
}
洞洞板實物圖:




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

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

使用道具 舉報

板凳
ID:200272 發表于 2017-5-13 13:48 | 只看該作者
你好,請問關于那個rst 和clk 的設置的問題,一開始給了一個高電平后為什么還要再給一個低電平?
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日韩精品| 日韩欧美国产不卡 | 中文字幕一区在线观看视频 | 伊人久久在线 | 黄色毛片大全 | 日韩成人在线播放 | 在线观看国产www | 九七午夜剧场福利写真 | 在线观看久草 | 亚洲97| 成人在线免费观看 | 成人免费视频观看视频 | 香蕉婷婷 | av在线天堂网 | 四虎网站在线观看 | 亚洲在线| 国产成年人小视频 | 美女在线国产 | 成人av免费播放 | 久久久九九 | 亚洲国产精品一区二区三区 | 国产91在线视频 | 欧美成人精品欧美一级 | 日韩一区二区三区在线观看 | 成人免费视频在线观看 | 日韩亚洲一区二区 | 日日干天天操 | 国产精品欧美一区二区三区 | 国产精品久久久久久久久久久久 | 日韩在线视频观看 | 国产高清精品网站 | 91亚洲国产| 国产综合久久久久久鬼色 | 欧美精品一区三区 | 999www视频免费观看 | www.天天操 | 国产99视频精品免费播放照片 | 国产精品爱久久久久久久 | 亚洲精品在线视频 | 成人免费视频 | 国产伦精品一区二区三区在线 |