How to work with BCD(binary-coded decimal) switch?: Unterschied zwischen den Versionen

Aus hyperdramatik
Zur Navigation springen Zur Suche springen
 
Zeile 103: Zeile 103:


== How to read the BCD switch with an Arduino and a shift register? ==
== How to read the BCD switch with an Arduino and a shift register? ==
One BCD Switch contains fourfour switches, so one shift register could match two switches. Luckily the BCD Switch transforms the numbers, as described earlier, in four binary bits. Therefore we could read our incoming byte with the help of a bit mask, which allows us to take the first four bits to get our first value. Then shift the byte four places to the right and take these to determine the second value.
The good thing about the shift registers is that you could chain them up, because the bit values, bit by bit, are going down the lane of the serial signal. In order to get two shift registers, we have to read out the data twice after latching the registers. This allows the concept of using just three pins on the Arduino to control many input pins. Like a button matrix
If you need a lot of output pins like an LED Cube or projects alike, you could use a shift-out register. It transforms the serial data into parallel data and switches the output pins according to the bit array it gets.


[[Datei:BCD_One_Switch.png|200px|thumb|right|one row of switches resambles one BCD Switch]]
[[Datei:BCD_One_Switch.png|200px|thumb|right|one row of switches resambles one BCD Switch]]

Aktuelle Version vom 10. März 2021, 16:11 Uhr

Foto by Laura Alapfy

How does it work?

Shift-in registers are a common piece of electronics. They combine transistors so you could survey eight input pins in that way that one shift register sends a so-called bit array of eight bits called a byte.

If the Arduino receives a bit array looking like this [0,0,1,0,1,0,0,0] it means you have an input voltage on pin four and pin six. So switch four and six are on and all others are off. Due to its mode of operation, it transforms the parallel incoming signals into a serial signal. The clue here is to use time as a reference to transform the eight parallel signals of the independent “ones” and “zeros” into a sequence of “ones” and “zeros”. This is a very basic principle of computing. But now you ask how the Arduino knows it gets eight bits (one byte) – and not just four or three. How does the Arduino know which bit is the first and the last one? The shift register has a so-called latch pin. If you activate the latch pin, the Arduino gives the signal to send data. So the shift register locks the state of the input pins and starts to send the bits. We are now sure to get the first bit by latching the register. To actually transmit the data, we need a clock pulse for each bit we want to get. Repeat this procedure eight times. Afterwards we repeat the whole cycle through the loop function in the Arduino sketch.

BCD Switch
Number in Front BitArray Switch 1 Switch 2 Switch 3 Switch 4
1 0001 off off off on
2 0010 off off on off
3 0011 off off on on
4 0100 off on off off
5 0101 off on off on
6 0110 off on on off
7 0111 off on on on
8 1000 on off off off
9 1001 on off off on
0 0000 off off off off

What will you need?

  • an Arduino (every typ will do)
  • a BCD-Switch
  • a shift register (CD4021)
  • jumperwires
  • a breadboard

How to connect a shift register with an Arduino?

Shift-In registers are a common piece of electronics. They combine transistors in that way that you could survey eight input pins in that way that one shift register sends a so-called bit array of eight bits called a byte.

If the arduino receives a bit array looking like this [0,0,1,0,1,0,0,0] it means you have an input voltage on pin 4 and pin 6. So switch 4 and 6 are on and all others are off. It could do it like this because it transforms the parallel incoming signals into a serial signal.

The clue here is to use time as a reference to transform the 8 parallel signals of the independent ones and zeros into a sequence of ones and zeros. This is a very basic principle of computing. But now you ask how the arduino knows it gets 8 bits(1 byte) and not just four or three. How does the arduino know which bit is the first and the last one. Therefore, the shift register has a so-called latch pin. If you activate the latch the arduino says: please send me data. So the shift register locks the state of the input pins and starts to send the bits. We are now sure we get the first bit by latching the register, now to actually transmit the data we need a clock pulse for each bit we want to get. So we do that eight times. And after that we repeat the cycle through the loop function in our arduino sketch.

How to read the BCD switch with an Arduino and a shift register?

The good thing about the shift registers is that you could chain them up, because the bit values, bit by bit, are going down the lane of the serial signal. In order to get two shift registers, we have to read out the data twice after latching the registers. This allows the concept of using just three pins on the Arduino to control many input pins. Like a button matrix If you need a lot of output pins like an LED Cube or projects alike, you could use a shift-out register. It transforms the serial data into parallel data and switches the output pins according to the bit array it gets.

one row of switches resambles one BCD Switch
 int latchPin = 5;
 int dataPin = 4;
 int clockPin = 6;
 
 byte switchVar1 = 72;  //01001000
 byte switchVar2 = 159; //10011111
 byte switchVar3 = 201; //10011111
 
 byte mask = 15; //00001111
 
 byte Digit1 = 0; //the five BCD-switches
 byte Digit2 = 0;
 byte Digit3 = 0;
 byte Digit4 = 0;
 byte Digit5 = 0;
 
 byte oldValue = 6;
 
 void setup() {
   Serial.begin(9600);
 
   pinMode(latchPin, OUTPUT);
   pinMode(clockPin, OUTPUT);
   pinMode(dataPin, INPUT);
 }
    
 void loop() {

   digitalWrite(latchPin,1); //locking the data
 
   delayMicroseconds(20);
 
   digitalWrite(latchPin,0);
 
   switchVar1 = shiftIn(dataPin, clockPin); //get the data
   switchVar2 = shiftIn(dataPin, clockPin);
   switchVar3 = shiftIn(dataPin, clockPin); 
 
   Digit1 = switchVar1 & mask; //use mask
   Digit2 = switchVar1>>4; //push four bits to the right
   Digit3 = switchVar2 & mask;
   Digit4 = switchVar2>>4;
   Digit5 = switchVar3 & mask;

   if(oldValue != Digit1 + Digit2 + Digit3 + Digit4 + Digit5){ //just print if the value changed
     Serial.print(Digit5, DEC);
     Serial.print(Digit4, DEC);
     Serial.print(Digit3, DEC);
     Serial.print(Digit2, DEC);
     Serial.println(Digit1, DEC);
     oldValue = Digit1 + Digit2 + Digit3 + Digit4 + Digit5;
   }
 
 delay(100);  
 
 } 
 
 byte shiftIn(int myDataPin, int myClockPin) {
 
   int i;
   int temp = 0;
   int pinState;
   byte myDataIn = 0;
 
   pinMode(myClockPin, OUTPUT);
   pinMode(myDataPin, INPUT);
 
   for (i = 7; i >= 0; i--)
 
     {
 
       digitalWrite(myClockPin, 0);
 
       delayMicroseconds(2);
 
       temp = digitalRead(myDataPin);
 
       if (temp) {
   
         pinState = 1;
   
         myDataIn = myDataIn | (1 << i); 
 
       }else {
   
         pinState = 0;
   
       }   
       digitalWrite(myClockPin, 1);   
     }
     return myDataIn;
   }

How to connect multiple BCD switches?

two switches
four switches with two registers

The good thing about the shift registers is that you could chain them up, because the bit values are going down the lane of the serial signal. In order to get two shift registers we have to read out the date twice after latching the registers. This is the concept of using just three pins on the arduino to control many input pins. If you have a lot of output pins like a LED cube or something like this you could use a shift-out register. It transforms the serial data into parallel data and switches the output pins according to the bit array it gets.