Skip to content
Browse files

7-Segment Arduino Library using the 74HC595 Shift Register

1 parent a8de63c commit 06445e8f3112bb128bf6f2f590195d7d01906a87 @MightyQ MightyQ committed
View
190 Pineapple/Pineapple.cpp
@@ -0,0 +1,190 @@
+#include "Arduino.h"
+#include "Pineapple.h"
+
+uint8_t Pineapple::registerPins(int SER_Pin, int RCLK_Pin, int SRCLK_Pin, int Number_of_Registers) {
+ _SER_Pin = SER_Pin;
+ _RCLK_Pin = RCLK_Pin;
+ _SRCLK_Pin = SRCLK_Pin;
+
+ _Number_of_Registers = Number_of_Registers;
+
+ pinMode(_SER_Pin, OUTPUT);
+ pinMode(_RCLK_Pin, OUTPUT);
+ pinMode(_SRCLK_Pin, OUTPUT);
+
+ clear(); //reset all register pins
+ update();
+}
+
+void Pineapple::update(){
+ //Set and display registers
+ //Only call AFTER all values are set how you would like (slow otherwise)
+
+ digitalWrite(_RCLK_Pin, LOW);
+
+ //iterate through the registers
+ for(int i = _Number_of_Registers - 1; i >= 0; i--){
+
+ //iterate through the bits in each registers
+ for(int j = 8 - 1; j >= 0; j--){
+
+ digitalWrite(_SRCLK_Pin, LOW);
+
+ int val = _shiftRegisters[i] & (1 << j);
+
+ digitalWrite(_SER_Pin, val);
+ digitalWrite(_SRCLK_Pin, HIGH);
+
+ }
+
+ }
+
+ digitalWrite(_RCLK_Pin, HIGH);
+}
+
+void Pineapple::setPin(int index, boolean val){
+ int byteIndex = index/8;
+ int bitIndex = index % 8;
+
+ byte current = _shiftRegisters[byteIndex];
+
+ current &= ~(1 << bitIndex); //clear the bit
+ current |= val << bitIndex; //set the bit
+
+ _shiftRegisters[byteIndex] = current; //set the value
+ update();
+}
+
+void Pineapple::setAll(boolean val){
+ //set all register pins to LOW
+ for(int i = _Number_of_Registers * 8 - 1; i >= 0; i--){
+ setPin(i, val);
+ }
+}
+
+void Pineapple::clear(){
+ //set all register pins to LOW
+ for(int i = _Number_of_Registers * 8 - 1; i >= 0; i--){
+ setPin(i, _common);
+ }
+}
+
+uint8_t Pineapple::segmentPins(int a, int b, int c, int d, int e, int f, int g, int dp, boolean common) {
+
+ _a=a;
+ _b=b;
+ _c=c;
+ _d=d;
+ _e=e;
+ _f=f;
+ _g=g;
+ _dp=dp;
+ _common=common;
+
+ segmentpins[0] = _dp;
+ segmentpins[1] = _g;
+ segmentpins[2] = _f;
+ segmentpins[3] = _e;
+ segmentpins[4] = _d;
+ segmentpins[5] = _c;
+ segmentpins[6] = _b;
+ segmentpins[7] = _a;
+
+ for(int i=0; i < 8; i++) {
+
+ pinMode(segmentpins[i], OUTPUT);
+ }
+
+
+}
+
+void Pineapple::flicker() {
+
+ write(0);
+ delay(50);
+ write(4);
+ delay(50);
+ write(9);
+ delay(50);
+ write(1);
+ delay(50);
+ write(6);
+ delay(50);
+ write(9);
+ delay(50);
+ write(3);
+ delay(50);
+ write(4);
+ delay(100);
+ write(7);
+ delay(100);
+ write(9);
+ delay(100);
+ write(3);
+ delay(100);
+ write(2);
+ delay(100);
+ write(5);
+ delay(100);
+ write(6);
+ delay(100);
+ write(3);
+ delay(200);
+ write(5);
+ delay(300);
+}
+
+void Pineapple::write(int number) {
+ boolean isBitSet;
+ // G, F, E, D, C, B, A, DP
+ numeral[0] = B11111100; // 0
+ numeral[1] = B01100000; // 1
+ numeral[2] = B11011010; // 2
+ numeral[3] = B11110010; // 3
+ numeral[4] = B01100110; // 4
+ numeral[5] = B10110110; // 5
+ numeral[6] = B10111110; // 6
+ numeral[7] = B11100000; // 7
+ numeral[8] = B11111110; // 8
+ numeral[9] = B11100110; // 9
+
+ if (_common == HIGH) {
+
+ for(int segment=1; segment < 8; segment++) {
+ if(number < 0 || number > 9) {
+ isBitSet = 0;
+ }else{
+ isBitSet = bitRead(numeral[number], segment);
+ }
+ isBitSet = ! isBitSet;
+ setPin(segmentpins[segment], isBitSet);
+ update();
+ }
+ }else{
+ for(int segment=1; segment < 8; segment++) {
+ if(number < 0 || number > 9) {
+ isBitSet = 0;
+ }else{
+ isBitSet = bitRead(numeral[number], segment);
+ }
+ setPin(segmentpins[segment], isBitSet);
+ update();
+ }
+ }
+ if (_number == '.' && _common == HIGH) {
+ setPin(_dp, LOW);
+ update();
+ }
+ if (_number == '.' && _common == LOW) {
+ setPin(_dp, HIGH);
+ update();
+ }
+ if (_common == HIGH) {
+ setPin(_dp, HIGH);
+ update();
+ }
+ if (_common == LOW) {
+ setPin(_dp, LOW);
+ update();
+ }
+}
View
28 Pineapple/Pineapple.h
@@ -0,0 +1,28 @@
+#ifndef Pineapple_h
+#define Pineapple_h
+
+#include "Arduino.h"
+
+class Pineapple
+{
+public:
+ uint8_t registerPins(int SER_Pin, int RCLK_Pin, int SRCLK_Pin, int Number_of_Registers);
+ uint8_t segmentPins(int a, int b, int c, int d, int e, int f, int g, int dp, boolean common);
+ void write(int number);
+ void clear();
+ void setAll(boolean val);
+ void setPin(int index, boolean val);
+ void update();
+ void flicker();
+
+private:
+ int _a, _b, _c, _d, _e, _f, _g, _dp, _number;
+ int _SER_Pin, _RCLK_Pin, _SRCLK_Pin, _Number_of_Registers;
+ byte numeral[10];
+ int segmentpins[8];
+ boolean _common;
+ byte _shiftRegisters[25];
+ boolean _val;
+};
+
+#endif
View
28 Pineapple/README.txt
@@ -0,0 +1,28 @@
+This library was made by Qtechknow (Quin). This drives a 7 segment LED with a 74HC595 shift register. Credits go to Qtechknow (Quin), Adam Meyer of Bildr, and Arduino Forum member lloyddean. If you have any problems, contact quin@qtechknow.com. Here are the main functions of the library:
+
+Pineapple pineapple
+ Initiates the library.
+
+pineapple.registerPins(int serialPin, int registerClockPin, int serialClockPin, int numberofRegisters)
+ Defines the pins of the shift register pins.
+
+pineapple.update()
+ Updates the display. You will not need to use this for it will be done automatically in the library.
+
+pineapple.setPin(int index, boolean value)
+ This will set an individual pin high or low, not a number on the display.
+
+pineapple.setAll(boolean value)
+ Sets all of the pins high or low. Watch out on the common anode or the common cathode, because that might be how you're messing up.
+
+pineapple.clear()
+ Clears the display.
+
+pineapple.segmentPins(int a, int b, int c, int d, int e, int f, int g, int dp, boolean common)
+ Declares the segment pins and the common anode or the common cathode. Set the boolean common HIGH for a common anode display and LOW for a common cathode display. For the segment pin numbers, set to 0 - 7 for the first shift register, 8-15 for the next, and so on.
+
+pineapple.flicker()
+ Flickers the display. I used this function in the example dice sketch to flicker before displaying the random number.
+
+pineapple.write(int number)
+ Displays int number. Can be used in for loops, so no more repetition.
View
31 Pineapple/examples/PineappleDemo/PineappleDemo.ino
@@ -0,0 +1,31 @@
+/* Pineapple Demo
+
+Demonstrates the use of the Pineapple library with a 74HC595 shift register and
+a 7-Segment display. Credits for the library go to Quin, or Qtechknow, Adam
+Meyer of Bildr, and Arduino forum member lloyddean.
+
+created on 18 Apr 2012
+made by Qtechknow (Quin)
+*/
+
+#include "Pineapple.h"
+
+int serial = 4; // shifte register, serial pin
+int registerClock = 3; // shift register, register clock
+int serialClock = 2; // shift register, clock
+
+int numberofRegisters = 1; // how many registers?
+
+Pineapple pineapple; // initialize the library
+
+void setup() {
+ pineapple.segmentPins(0, 1, 2, 3, 4, 5, 6, 7, HIGH); // set high for common anode, and low for common cathode
+ pineapple.registerPins(serial, registerClock, serialClock, numberofRegisters);
+}
+
+void loop() {
+ for(int i=0; i<10; i++) {
+ pineapple.write(i);
+ delay(500);
+ }
+}
View
45 Pineapple/examples/PineappleDice/PineappleDice.ino
@@ -0,0 +1,45 @@
+/* Dice Sketch
+
+Uses the Soda library to drive a 7-segment display. When you press the button
+on D5, the 7-segment display flickers and then displays a random number.
+Credits go to Qtechknow (Quin), Adam Meyer of Bildr, and Arduino Forum member
+lloyddean.
+
+created 07 May 2012
+made by Qtechknow (Quin)
+*/
+
+#include "Pineapple.h"
+
+Pineapple pineapple; // initialize the library
+
+int buttonVal; // variable to store our button state
+
+int randomNumber; // random number variable
+int serial = 4; // serial pin
+int registerClock = 3; // register clock pin
+int serialClock = 2; // serial clock pin
+
+int numberofRegisters = 1; // how many registers are on the chain?
+
+void setup() {
+ // Segments A, B, C, D, E, F, G, DP pin numbers
+ pineapple.segmentPins(0, 1, 2, 3, 4, 5, 6, 7, HIGH); // set high for common anode, and
+ pineapple.registerPins(serial, registerClock, serialClock, numberofRegisters); // low for common cathode display
+
+ randomSeed(analogRead(1)); // float the random value
+}
+
+void loop() {
+ buttonVal=analogRead(0); // assign the integer
+ pineapple.clear(); // clear the display before writing
+
+ if(buttonVal >= HIGH) { // if the button is pressed,
+
+ pineapple.flicker(); // flicker the display
+
+ randomNumber = random(1, 7); // assign a random number inbetween 1 and 6
+ pineapple.write(randomNumber); // write the number
+ delay(100);
+ }
+}
View
83 Pineapple/examples/PineappleSerial/PineappleSerial.ino
@@ -0,0 +1,83 @@
+/* PineappleSerial
+
+When you type in a number between 0 and 9 on the serial monitor, the 7-segment
+display, driven by the Pineapple library, will light up correspondingly.
+Credits go to Qtechknow (Quin), Adam Meyer of Bildr, and Arduino Forum member
+lloyddean.
+
+created 08 May 2012
+made by Qtechknow (Quin)
+*/
+
+#include "Pineapple.h"
+
+Pineapple pineapple; // initialize the library
+
+int serial = 4; // serial pin
+int registerClock = 3; // register clock pin
+int serialClock = 2; // serial clock pin
+
+int numberofRegisters = 1; // just one register
+
+void setup() {
+// Segments A, B, C, D, E, F, G, DP pin numbers on the shift register
+ pineapple.segmentPins(0, 1, 2, 3, 4, 5, 6, 7, HIGH); // set high for common anode, and
+ pineapple.registerPins(serial, registerClock, serialClock, numberofRegisters); // low for common cathode display
+ Serial.begin(9600); // begin serial communication
+}
+
+void loop() {
+
+ pineapple.clear();
+
+ if(Serial.available() > 0) { // is there anything on the serial monitor?
+ char ch = Serial.read();
+
+ switch (ch) { // start the switch case statement
+
+ case '0': // if we receive a 0, print a 0
+ pineapple.write(0);
+ break;
+
+ case '1':
+ pineapple.write(1); // if we receive a 1, print a 1
+ break;
+
+ case '2': // if we receive a 2, print a 2 and so on.
+ pineapple.write(2);
+ break;
+
+ case '3':
+ pineapple.write(3);
+ break;
+
+ case '4':
+ pineapple.write(4);
+ break;
+
+ case '5':
+ pineapple.write(5);
+ break;
+
+ case '6':
+ pineapple.write(6);
+ break;
+
+ case '7':
+ pineapple.write(7);
+ break;
+
+ case '8':
+ pineapple.write(8);
+ break;
+
+ case '9':
+ pineapple.write(9);
+ break;
+
+ default: // default is to clear the display
+ pineapple.clear();
+ }
+ }
+ delay(1000); // don't go too fast!
+}
View
9 Pineapple/keywords.txt
@@ -0,0 +1,9 @@
+Pineapple KEYWORD1
+registerPins KEYWORD2
+segmentPins KEYWORD2
+write KEYWORD2
+clear KEYWORD2
+setAll KEYWORD2
+setPin KEYWORD2
+update KEYWORD2
+flicker KEYWORD2

0 comments on commit 06445e8

Please sign in to comment.
Something went wrong with that request. Please try again.