Go Down

Topic: shiftOut() and 7 segment display problem (decimal to hex) (Read 2083 times) previous topic - next topic

mattybigback

Hi there

I'm trying to use shiftOut() to control two 7 segment LED displays. I'm shifting out to a 4051 that is connected to two 4511 BCD to 7 segment decoders. The electronics are working fine, but the displays aren't counting up as they should. What I think is happening is that the value I'm sending out is being represented as a single byte rather than two nibbles, but I'm unsure how to fix it. I thought about somehow converting the number I'm shifting out into hexadecimal so that instead of getting to 9 and trying to represent 10(d) as A(h) it would skip straight up to 16(d), or 10(h).

Here's my code.

//Pin connected to Strobe (pin 1) of 4094
int strobePin = A3;
//Pin connected to Data (pin 2) of 4094
int dataPin = A4;
//Pin connected to Clock (pin 3) of 4094
int clockPin = A5;
//Pin connected to OE (pin 15) of 4094
int oePin = A2;


void setup() {
 //set pins to output because they are addressed in the main loop
 pinMode(strobePin, OUTPUT);
 pinMode(clockPin, OUTPUT);
 pinMode(dataPin, OUTPUT);
 pinMode(oePin, OUTPUT);
 digitalWrite(oePin, HIGH);
}

void loop() {
 //count up routine
 for (int i = 0; i < 100; i++) {
   //set strobe pin low to begin storing bits
   digitalWrite(strobePin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, i);  
   //set strobe pin high to stop storing bits
   digitalWrite(strobePin, HIGH);
   delay(500);
 }
}


Any help would be greatly appreciated =]

mattybigback

I solved it.

The way I did it was to split the tens and units into separate variables, bitshift the tens 4 places left and then add the units using a bitwise OR.

//Pin connected to Strobe (pin 1) of 4094
int strobePin = A3;
//Pin connected to Data (pin 2) of 4094
int dataPin = A4;
//Pin connected to Clock (pin 3) of 4094
int clockPin = A5;
//Pin connected to OE (pin 15) of 4094
int oePin = A2;


void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(strobePin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(oePin, OUTPUT);
  digitalWrite(oePin, HIGH);
}

void loop() {
  //count up routine
  for (int count = 0; count < 100; count++) {
   
    //get the value for the tens display
    byte tens = count / 10;
    //get the value for the units display
    byte units = count % 10;
    //shift the tens value 4 bits to the left and attach the units value using a bitwise OR
    byte out = (tens << 4|units);   
   
    //set strobe pin low to begin storing bits
    digitalWrite(strobePin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, out); 
    //set strobe pin high to stop storing bits
    digitalWrite(strobePin, HIGH);
    delay(200);
  }
}

Go Up