Add PIO 7-segment display example#714
Conversation
|
@mjcross I've not tested this, as I don't have a bagful of transistors handy, but does this work? // Simple example of how to convert an integer between -999 and 9999
// into a 32-bit word representing up to four 7-segment digits.
//
uint32_t int_to_seven_segment (int num) {
uint32_t word = 0;
if (num < -999 || num > 9999) {
// number out of range, display 'E' symbol
// EDBGACF.
word = 0b11011010
} else {
if (num == 0) {
word = segments[0];
} else {
bool negative = num < 0;
if (negative) {
num *= -1;
}
int bitshift;
for (bitshift = 0; bitshift < 32 && num > 0; bitshift += 8) {
word |= segments[num % 10] << bitshift;
num /= 10;
}
if (negative) {
bitshift += 8;
// display '-' symbol
// EDBGACF.
word |= 0b00010000 << bitshift;
}
}
}
return word;
} |
|
@lurch yes: tested a couple of years ago when I wrote it, with tiny 7-segment displays that didn't need driver transistors ;-) The project I wrote it for needed up using an OLED display instead so it never got used. |
|
TBH if there's any bit of the project likely to cause issues it's probably the CMakeLists.txt files: I see at some point several of the example builds were adjusted to use |
Ahhh, sorry for the miscommunication - I wasn't asking "Does your function work?", I was asking "Does my suggested change to your function, to add support for displaying negative numbers, work?" 😂
It's so that if an example uses a hardware-feature that a particular board / chip / platform doesn't support, we don't try building that example for that chip. See e.g. https://github.com/raspberrypi/pico-examples/blob/master/rtc/CMakeLists.txt where we prevent the RTC examples from building for the RP2350 (as it doesn't have an RTC) or https://github.com/raspberrypi/pico-examples/blob/master/sha/CMakeLists.txt where we prevent the SHA examples from building for the RP2040 (as it doesn't have SHA256 hardware). P.S. I've just realised that you also need to edit the top-level |
|
Ahh - sorry! Didn't read carefully enough. That code looks nice - I'll check it this afternoon: tks! |
|
I like it! Turns out you don't need the |
|
You can simulate it with this slightly hacky code... |
|
The output is actually rather cute |
|
Hope the complex-looking circuit doesn't put people off: it's a really good use-case for the PIO. |
|
Maybe add a dimmable version by adding something like after |
|
That’s neat :-) |
|
I think you need to add more detail about the display you have written this for. A part number perhaps? A quick search mostly finds common cathode displays |
I agree - the parts I used were recovered from a (very) old calculator and as you say, common cathode displays are much more normal these days. I think I'll kill two birds with one stone and re-work this with a darlington driver chip (as previously suggested by @lurch IIRC) at the same time. |
|
Just in case it's useful: https://raspi.tv/2015/how-to-drive-a-7-segment-display-directly-on-raspberry-pi-in-python |
Very helpful, thanks 🙂 ... what that guy may not have quite worked out mind you, is that with 100 Ohm resistors he's probably driving 15mA per segment, so if he illuminates all 8 he's going to clobber the GPIO he's using for 'common' with 120mA #facepalm |
|
Waiting for some components to arrive for testing |
Add an example showing how to use the PIO to control a multiplexed 7-segment display.
@lurch this re-creates #375 as discussed, due to loss of the original repo