Skip to content

Instantly share code, notes, and snippets.

@0xjmux
Last active March 11, 2019 14:56
Show Gist options
  • Save 0xjmux/8a871dc59cd33792742d96322322c320 to your computer and use it in GitHub Desktop.
Save 0xjmux/8a871dc59cd33792742d96322322c320 to your computer and use it in GitHub Desktop.
Code for a 74HC595 Shift register running an SH5261AS 2 digit 7 segment display
/* Running on an arduino uno with a 74HC595 Shift register to control the display
*/
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
byte leds = 0;
/*
* shift Reg to 7seg pinout, in bit position in byte variable above
* A - 7
* B - 6
* C - 1
* D - 4
* E - 3
* F - 0
* G - 5
* DP - 2
*
*/
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
updateShiftRegister();
Serial.begin(9600);
}
void loop()
{
//this loop works fine
for(int i=0; i<10; i++) {
Serial.println(i);
displayDigit(i);
delay(1000);
}
}
//this is where the problems are. There's some kind of issue with the leds byte value
//something in the way the 595 is being addressing is causing the problem, this function is inputting the correct value but it isn't outputting the right result on the display
void displayDigit(int digit) {
leds = 0;
updateShiftRegister();
delay(10);
//A
if(digit!=1 || digit!=4) {
bitSet(leds, 7);
updateShiftRegister();
delay(10);
}
//B
if(digit!=5 || digit!= 6) {
bitSet(leds, 6);
updateShiftRegister();
delay(10);
}
//C
if(digit!=2) {
bitSet(leds, 1);
updateShiftRegister();
delay(10);
}
//D
if(digit!=1 || digit!= 4 || digit!=7 || digit!=9) {
bitSet(leds, 4);
updateShiftRegister();
delay(10);
}
//E
if(digit==2 || digit==6 || digit==8 || digit==0) {
bitSet(leds, 3);
updateShiftRegister();
delay(10);
}
//F
if(digit==4 || digit==5 || digit==6 || digit==8 ||digit==9 || digit==0) {
bitSet(leds, 0);
updateShiftRegister();
delay(10);
}
//G
if(digit!=1 || digit!= 7 || digit!=0) {
bitSet(leds, 5);
updateShiftRegister();
delay(10);
}
}
void empty() {
leds = 0;
bitSet(leds, 5);
updateShiftRegister();
}
void dp() {
bitSet(leds, 2);
updateShiftRegister();
}
void clearDisplay() {
leds = 0;
updateShiftRegister();
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment