Skip to content

Commit 805ae87

Browse files
author
Martin Crossley
committed
add PIO seven segment display example
1 parent c89aa9b commit 805ae87

File tree

10 files changed

+300
-0
lines changed

10 files changed

+300
-0
lines changed

pio/seven_segment/7_segment.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include "pico/stdlib.h"
3+
#include "7_segment_lib.h"
4+
5+
6+
const PIO pio = pio0;
7+
const uint first_segment_pin = 8; // gpio 15-8 = segments E,D,B,G,A,C,F,dp
8+
const uint first_digit_pin = 16; // gpio 19-16 = common anodes 4,3,2,1
9+
10+
// EDBGACF. bit ordering depends on your display and wiring
11+
const uint32_t Pico = 0b10111010 << 24 | // 'P'
12+
0b10000000 << 16 | // 'i'
13+
0b11010000 << 8 | // 'c'
14+
0b11010100; // 'o'
15+
16+
int main() {
17+
uint sm;
18+
stdio_init_all();
19+
20+
if (seven_segment_init (pio, &sm, first_segment_pin, first_digit_pin)) {
21+
puts ("running");
22+
23+
// display scrolling 'Pico'
24+
for (int shift = 24; shift >= 0; shift -= 8) {
25+
pio_sm_put (pio, sm, Pico >> shift);
26+
sleep_ms (1000);
27+
}
28+
29+
// count to 9999
30+
for (int i = 0; i < 10000; i += 1) {
31+
sleep_ms (100);
32+
pio_sm_put (pio, sm, int_to_seven_segment (i));
33+
}
34+
}
35+
36+
puts ("done");
37+
while (true);
38+
}
60.8 KB
Binary file not shown.
362 KB
Loading
Lines changed: 76 additions & 0 deletions
Loading

pio/seven_segment/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
add_executable(pio_seven_segment)
2+
3+
target_sources(pio_seven_segment PRIVATE 7_segment.c)
4+
5+
add_subdirectory_exclude_platforms(pio_7_segment_library)
6+
7+
target_link_libraries(pio_seven_segment PRIVATE
8+
pico_stdlib
9+
hardware_pio
10+
pio_7_segment_library
11+
)
12+
13+
pico_add_extra_outputs(pio_seven_segment)
14+
15+
# add url via pico_set_program_url
16+
example_auto_set_url(pio_seven_segment)

pio/seven_segment/README.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
= Driving a 7 segment LED display
2+
This example demonstrates how a PIO state machine can be used to control a four digit multiplexed 7-segment display with the Raspberry Pi Pico (RP2040).
3+
4+
Multiplexed displays work by rapidly displaying each digit in turn and the PIO provides an excellent way to relieve the CPU of this time-consuming task.
5+
6+
The total current required is typically more than the GPIO pins can supply so you're likely to need an external drive circuit like the one below.
7+
8+
The PIO code uses four `side-set` pins to control the digit multiplex lines and displays the segment patterns received on the FIFO. It uses a non-blocking PULL to keep showing the same segments until the CPU sends new data.
9+
10+
The provided example uses spells out the word **Pico** and then counts from 0 to 9999.
11+
12+
== Wiring information
13+
Connect the display to your board using a circuit like the one below, making any changes for your display and transistors.
14+
15+
Connect the circuit to an external 5V supply or power it via USB (in which case reconnect the _+5V_ rail to _VBUS_ instead of _VSYS_).
16+
17+
TIP: this circuit is for a _common anode_ display that takes about 15mA per segment.
18+
19+
[[pio_7_segment_wiring-diagram]]
20+
[pdfwidth=75%]
21+
.Wiring diagram
22+
image::7_segment_wiring_diagram.png[]

0 commit comments

Comments
 (0)