找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 9442|回复: 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 单片机教程网

快速回复 返回顶部 返回列表