Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ App|Description
[pio_pwm](pio/pwm) | Pulse width modulation on PIO. Use it to gradually fade the brightness of an LED.
[pio_quadrature_encoder](pio/quadrature_encoder) | A quadrature encoder using PIO to maintain counts independent of the CPU.
[pio_quadrature_encoder_substep](pio/quadrature_encoder_substep) | High resolution speed measurement using a standard quadrature encoder.
[pio_seven_segment](pio/seven_segment) | Drive a multiplexed four digit 7-segment LED display using the PIO.
[pio_spi_flash](pio/spi) | Use PIO to erase, program and read an external SPI flash chip.
[pio_spi_loopback](pio/spi) | Use PIO to run a loopback test with all four CPHA/CPOL combinations.
[pio_squarewave](pio/squarewave) | Drive a fast square wave onto a GPIO. This example accesses low-level PIO registers directly, instead of using the SDK functions.
Expand Down
1 change: 1 addition & 0 deletions pio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if (TARGET hardware_pio)
add_subdirectory_exclude_platforms(pwm)
add_subdirectory_exclude_platforms(quadrature_encoder)
add_subdirectory_exclude_platforms(quadrature_encoder_substep)
add_subdirectory_exclude_platforms(seven_segment)
add_subdirectory_exclude_platforms(spi)
add_subdirectory_exclude_platforms(squarewave)
add_subdirectory_exclude_platforms(st7789_lcd)
Expand Down
49 changes: 49 additions & 0 deletions pio/seven_segment/7_segment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "7_segment_lib.h"


const PIO pio = pio0;
const uint first_segment_pin = 8; // gpio 15-8 = segments E,D,B,G,A,C,F,dp
const uint first_digit_pin = 16; // gpio 19-16 = common anodes 4,3,2,1

// By convention the segments are labelled as follows:
//
// AAAA
// F B
// F B
// GGGG
// E C
// E C
// DDDD .

// You can define a custom bit pattern like this:
// the order here is EDBGACF. but should match the way you wire up the GPIOs
const uint32_t Pico = 0b10111010 << 24 | // 'P'
0b10000000 << 16 | // 'i'
0b11010000 << 8 | // 'c'
0b11010100; // 'o'

int main() {
uint sm;
stdio_init_all();

if (seven_segment_init (pio, &sm, first_segment_pin, first_digit_pin)) {
puts ("running");

// display scrolling 'Pico'
for (int shift = 24; shift >= 0; shift -= 8) {
pio_sm_put (pio, sm, Pico >> shift);
sleep_ms (1000);
}

// count to 9999
for (int i = 0; i < 10000; i += 1) {
sleep_ms (100);
pio_sm_put (pio, sm, int_to_seven_segment (i));
}
}

puts ("done");
while (true);
}
Binary file added pio/seven_segment/7_segment_wiring.fzz
Binary file not shown.
Binary file added pio/seven_segment/7_segment_wiring_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions pio/seven_segment/7seg 07.35.32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions pio/seven_segment/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
add_executable(pio_seven_segment)

target_sources(pio_seven_segment PRIVATE 7_segment.c)

add_subdirectory_exclude_platforms(pio_7_segment_library)

target_link_libraries(pio_seven_segment PRIVATE
pico_stdlib
hardware_pio
pio_7_segment_library
)

pico_add_extra_outputs(pio_seven_segment)

# add url via pico_set_program_url
example_auto_set_url(https://github.com/pio_seven_segment)
22 changes: 22 additions & 0 deletions pio/seven_segment/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
= Driving a 7 segment LED display
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).

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.

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.

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.

The provided example spells out the word **Pico** and then counts from 0 to 9999.

== Wiring information
Connect the display to your board using a circuit like the one below, making any changes for your display and transistors.

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_).

TIP: this circuit is for a _common anode_ display that takes about 15mA per segment.

[[pio_7_segment_wiring-diagram]]
[pdfwidth=75%]
.Wiring diagram
image::7_segment_wiring_diagram.png[]
Loading