|
--用3個(gè)單片機(jī)引腳讀取10個(gè)開關(guān)狀態(tài)
作者注:此方法并非擴(kuò)展輸入腳最佳方法,如果需要大量擴(kuò)展輸入腳的話(幾十甚至上百路),可以使用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.
- /*
- * SN74HC165N_shift_reg
- *
- * Program to shift in the bit values from a SN74HC165N 8-bit
- * parallel-in/serial-out shift register.
- *
- * This sketch demonstrates reading in 16 digital states from a
- * pair of daisy-chained SN74HC165N shift registers while using
- * only 4 digital pins on the Arduino.
- *
- * You can daisy-chain these chips by connecting the serial-out
- * (Q7 pin) on one shift register to the serial-in (Ds pin) of
- * the other.
- *
- * Of course you can daisy chain as many as you like while still
- * using only 4 Arduino pins (though you would have to process
- * them 4 at a time into separate unsigned long variables).
- *
- */
- /* How many shift register chips are daisy-chained.
- */
- #define NUMBER_OF_SHIFT_CHIPS 2
- /* Width of data (how many ext lines).
- */
- #define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
- /* Width of pulse to trigger the shift register to read and latch.
- */
- #define PULSE_WIDTH_USEC 5
- /* Optional delay between shift register reads.
- */
- #define POLL_DELAY_MSEC 1
- /* You will need to change the "int" to "long" If the
- * NUMBER_OF_SHIFT_CHIPS is higher than 2.
- */
- #define BYTES_VAL_T unsigned int
- int ploadPin = 8; // Connects to Parallel load pin the 165
- int clockEnablePin = 9; // Connects to Clock Enable pin the 165
- int dataPin = 11; // Connects to the Q7 pin the 165
- int clockPin = 12; // Connects to the Clock pin the 165
- BYTES_VAL_T pinValues;
- BYTES_VAL_T oldPinValues;
- /* This function is essentially a "shift-in" routine reading the
- * serial Data from the shift register chips and representing
- * the state of those pins in an unsigned integer (or long).
- */
- BYTES_VAL_T read_shift_regs()
- {
- long bitVal;
- BYTES_VAL_T bytesVal = 0;
- /* Trigger a parallel Load to latch the state of the data lines,
- */
- digitalWrite(clockEnablePin, HIGH);
- digitalWrite(ploadPin, LOW);
- delayMicroseconds(PULSE_WIDTH_USEC);
- digitalWrite(ploadPin, HIGH);
- digitalWrite(clockEnablePin, LOW);
- /* Loop to read each bit value from the serial out line
- * of the SN74HC165N.
- */
- for(int i = 0; i < DATA_WIDTH; i++)
- {
- bitVal = digitalRead(dataPin);
- /* Set the corresponding bit in bytesVal.
- */
- bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));
- /* Pulse the Clock (rising edge shifts the next bit).
- */
- digitalWrite(clockPin, HIGH);
- delayMicroseconds(PULSE_WIDTH_USEC);
- digitalWrite(clockPin, LOW);
- }
- return(bytesVal);
- }
- /* Dump the list of zones along with their current status.
- */
- void display_pin_values()
- {
- Serial.print("Pin States:");
- for(int i = 0; i < DATA_WIDTH; i++)
- {
- Serial.print(" Pin-");
- Serial.print(i);
- Serial.print(": ");
- if((pinValues >> i) & 1)
- Serial.print("HIGH");
- else
- Serial.print("LOW");
- Serial.print("");
- }
- Serial.print("");
- }
- void setup()
- {
- Serial.begin(9600);
- /* Initialize our digital pins...
- */
- pinMode(ploadPin, OUTPUT);
- pinMode(clockEnablePin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, INPUT);
- digitalWrite(clockPin, LOW);
- digitalWrite(ploadPin, HIGH);
- /* Read in and display the pin states at startup.
- */
- pinValues = read_shift_regs();
- display_pin_values();
- oldPinValues = pinValues;
- }
- void loop()
- {
- /* Read the state of all zones.
- */
- pinValues = read_shift_regs();
- /* If there was a chage in state, display which ones changed.
- */
- if(pinValues != oldPinValues)
- {
- Serial.print("*Pin value change detected*");
- display_pin_values();
- oldPinValues = pinValues;
- }
- delay(POLL_DELAY_MSEC);
- }
復(fù)制代碼
使用Arduino做制作,需要有一組開關(guān)控制Arduino狀態(tài)。但是Arduino引腳不多,傳統(tǒng)接法開關(guān)多了要占用很多引腳。減少引腳的方法有很多,可以選矩陣方式,編碼器方式,還有本文要介紹的分時(shí)復(fù)用開關(guān)法等。
特點(diǎn):十個(gè)開關(guān)占用三個(gè)數(shù)據(jù)引腳,之后每增加十個(gè)開關(guān)就增加一個(gè)引腳。
4017是一塊十進(jìn)制計(jì)數(shù)器,每輸入一個(gè)CLK脈沖,Q0~Q9輪流產(chǎn)生高電平。每時(shí)刻有且只有一個(gè)引腳高電平。
二極管防止多個(gè)開關(guān)閉合時(shí),有的輸出端輸出高電平,有的輸出低電平,互相接上的話,會(huì)低電平引腳會(huì)干擾高電平腳的工作。
開關(guān)用10路撥動(dòng)式小型開關(guān),或者自己選擇其他開關(guān)形式。
電路工作原理:
- 先在RST(4017的復(fù)位腳MR)發(fā)出一個(gè)脈沖,使4017復(fù)位。
- 此時(shí)有且只有Q0輸出高電平(Q0對應(yīng)開關(guān)S1,Q9對應(yīng)開關(guān)S10),讀取一次輸出信號DATA。如果第一個(gè)開關(guān)S1閉合了,應(yīng)該DATA得到高電平;如果S1斷開了,就DATA得到低電平。此時(shí)記DATA結(jié)果對應(yīng)第一個(gè)開關(guān)S1的狀態(tài)。
- 給CLK輸出一個(gè)脈沖,讓4017移位,有且只有Q1輸出高電平(Q0,Q2~Q9均為低電平)。讀取DATA。得到S2狀態(tài)。
- 不斷給CLK脈沖。總共給10次脈沖,讓4017由Q0移動(dòng)到Q9,完成一次開關(guān)遍歷,每次移動(dòng)獲取一次DATA狀態(tài)。存為S1~S10狀態(tài)。
電路原理圖如圖:
洞洞板圖(由于引腳多,不建議面包板制作。)
關(guān)于在一塊Arduino上使用多塊模塊:每增加一塊模塊,可以增加十路開關(guān)(當(dāng)然你也可以使用兩塊4017做成行列矩陣控制100個(gè)開關(guān)。不過那個(gè)就屬于另外話題了)。增加的方式是將兩塊模塊的RST,CLK,VCC,GND接在一起,接到單片機(jī)的相應(yīng)引腳,然后兩個(gè)模塊的DATA腳分別接單片機(jī)兩個(gè)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}; //按照總開關(guān)數(shù)定義。可能要改為20,30等
digitalWrite(rst, HIGH);
delayMicroseconds(10); //所有delayMicroseconds(10);均是給4017一個(gè)反應(yīng)時(shí)間。
digitalWrite(rst, LOW);
delayMicroseconds(10);
for(int i = 0; i < 10; i++)
{
KeyStatus = digitalRead(data1);
//KeyStatus[i+10] = digitalRead(data2); //讀取第二個(gè)板子的狀態(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);
}
洞洞板實(shí)物圖:
|
|