Thursday, September 26, 2013

Shift Registers

Good success 

...last night I wired my board with 8 LEDs, and a Shift register IC chip.
Wrote some code, learned about BIT maths to program the chip - and amazingly it all worked!. 




Before this configuration, to control 8 outputs from my Ardunio micro controller - I would need to use 8 plugs (or wires from the controller). Now I use only 3 wires (above you can see the Green, Blue and yellow wires on the right side of the Ardunio Uno board). And by using binary that I send to the Darlington Array IC chip - it will control the outputs for me. 
I've used a ULN2003A Array by the way. They are only a few dollars from Jaycar.

This expands one of the biggest restrictions I could see significantly. And further to this I apparently can daisy-chain more then one shift register together. That will be another project shortly.

In have read of a number of shield boards that can be added (one is the centipede shield), which does this work for you - but what the hell, I'm learning more this way.




The expansion looks like this...

Number of             
Shift Reg (8 bit)     Wires used                            Output 
IC Chips                 on Ardunio                            Devices
----------------------+------------------------------------+---------------------------------------
None                       8                                          8              <-- restrictive
1                             3                                          8
2                             3                                          16
3                                                                      24           <-- awesome

Here is the code I used.


  
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

 void setup() {
 
  Serial.begin(9600);
 
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  // count from 0 to 255 and display the number
  // on the LEDs
  //for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
    // take the latchPin low so
    // the LEDs don't change while you're sending in bits:
   
    int numberToDisplay = 255  ; //85   170   255;//255 is all lights on
    digitalWrite(latchPin, LOW);  
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay); 
    digitalWrite(latchPin, HIGH);
    // pause before next value:
    Serial.println(numberToDisplay);
    delay(2000);
   
//The following is simple BIT math examples

    // 0 & 0 == 0
    // 0 & 1 == 0
    // 1 & 0 == 0
    // 1 & 1 == 1
    int c = numberToDisplay & B00111100;
   
    // 0 | 0 == 0
    // 0 | 1 == 1
    // 1 | 0 == 1
    // 1 | 1 == 1
    // int c = numberToDisplay | B01111100;
   
    // 0 ^ 0 == 0
    // 0 ^ 1 == 1
    // 1 ^ 0 == 1
    // 1 ^ 1 == 0
    // int c = numberToDisplay ^ B10000010;
   
    // int c = ~numberToDisplay; // the ~ (NOT) will switch all bits to opposite state
    // int c = numberToDisplay << 3; // move all bits 3 places to the left
    // int c = numberToDisplay >> 3; // move all bits 3 places to the left
   
   
     numberToDisplay = c;
    digitalWrite(latchPin, LOW);
    //// shift out the bits:
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay); 

    ////take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH);
   
    //// pause before next value:
    Serial.println(numberToDisplay);
    delay(2000);
   
  //}

}

No comments:

Post a Comment