This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

2 digit - 7 Segment Display Example

Is there an example for using a 10 pin 2 digit 7 segment display similar the picture below?

It is common anode and actually has 10 pins (pin 2 is the DP or decimal point). I have all of the LED pins (1-4 and 5-9) hooked up to their own GPIO and the common anodes are hooked up to pins 10 and 5 through a PNP transistor. Those 2 transistors are hooked up their own GPIO pins. I need an example to multiplex the 2 displays without adding any other chips.

I did this before on Arduino using an old library I found but not sure how to do this on nRF51822.

I am using a custom board with nrf51822 chip. SDK 12.

Any help would be appreciated!

2 digit 10 pin 7 segment display 7 segment display?

  • Have no idea if there's sample code for that, but it's very simple to write. You just want to round-robin between the two segments giving each a fraction of a second, then going back to the other one.

    Keep the current bitmap of on/off for each segment for each of the two (or more) segments, you can update that whenever you like. Then set up a repeating app timer, or any other repeating timer, which alternately puts one or other bitmap onto the cathode pins and sets/resets the correct one of the anode pins. Adjust the interval to be fast enough not to flicker, slow enough to give the LEDs time to light up.

    You change the value displayed by simply writing one or other of the bitmaps in memory, it will show on the next cycle for that digit, completely asynchronous and about 5 lines of code.

    I think you have your GPIO numbers wrong, you've used 5 twice, so you probably mean 1-4 and 6-9, and hopefully you don't have the cathodes connected directly to the GPIO pins because any LED display I've come across will need way more current than the nRF can sink.

  • Yes - you are correct, I typed the numbers wrong - should have been 1-4 and 6-9 as you said. 5 and 10 are common anodes.

    You have been a big help so far!

    Can you explain more (or example code) the repeating app timer (new to nRF). I set the individual led segments up as LEDs and the Comman Anodes as GPIOs. I am able to get "Ao" to show on the display with the following code for about 5 seconds with a for loop when a button is pressed but ends up just saying "o" at the end of the for loop:

                {
            LEDS_OFF(LEDS_MASK);
            LEDS_ON(SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G);
            nrf_gpio_pin_write(COMAN1,1);
            nrf_gpio_pin_write(COMAN2,0);
            nrf_delay_ms(5);
            LEDS_OFF(LEDS_MASK);
            LEDS_ON(SEG_C | SEG_D | SEG_E | SEG_G);
            nrf_gpio_pin_write(COMAN1,0);
            nrf_gpio_pin_write(COMAN2,1);
            nrf_delay_ms(5);
            }
    

    Obviously, that will not work in a complete program - mainly with the delay (I need to avoid using that as the program hangs while in delay) and the fact that I assume that I should not be running that in a for loop.

    Can you suggest a proper way to code this? I want the display to say one thing constantly until a button is pressed to change a setting and then the display will say something else. I assume this will be done in memory and asynchronously as you stated but I have no idea where to begin with nRF. Thanks in advance!

  • something like this typed in without checking

    uint32_t leds_bits[2];    // two uint32s with bits set for each segment including the anode bit
    uint8_t leds_index;
    
    void called_by_app_timer_regularly()
    {
        NRF_GPIO->OUTCLR = 0x000007fe;    // clear all the pins 1-10
        NRF_GPIO->OUTSET = leds_bits[ leds_index ]; // set all the pins to the required value
        leds_index = ( 1 - leds_index ); // do the other one the next time
    }
    

    leds_bits has bit 5 clear and bit 10 set for one entry and the other way around for the other, plus the bits for the segments you want lit up set. Update that at any point, the next cycle will refresh it. Set up a repeating app_timer (lots of examples for that) to call that function regularly.

  • This worked great! Thank you for helping out - it is appreciated!

Related