#include <segment.pio.h>
uint8_t digits[] = {
0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11111000,
0b10000000,
0b10011000,
};
const uint8_t first_segment_pin = 2;
const uint8_t first_digit_pin = 10;
void setup() {
Serial1.begin(115200);
Serial1.println("Raspberry Pi Pico PIO 7-Segment Example");
auto offset = pio_add_program(pio0, &segment_program);
segment_program_init(pio0, 0, offset, first_segment_pin, first_digit_pin);
}
void displayNumber(uint value) {
pio_sm_put(pio0, 0,
digits[value / 1000 % 10] << 24 |
digits[value / 100 % 10] << 16 |
digits[value / 10 % 10] << 8 |
digits[value % 10]
);
}
int i = 0;
void loop() {
displayNumber(i++);
delay(200);
}
#pragma once
#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif
#define segment_wrap_target 0
#define segment_wrap 5
static const uint16_t segment_program_instruction[] = {
0x8080,
0xa027,
0x6208,
0x6408,
0x6808,
0x7008,
};
#if !PICO_NO_HARDWARE
static const struct pio_program segment_program = {
.instruction = segment_program_instruction,
.length = 6,
.origin = -1,
};
static inline pio_sm_config segment_program_get_default_config(unit offset) {
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + segment_wrap_target, offset + segment_wrap);
sm_config_set_sideset(&c, 4, false, false);
return c;
}
#include "hardware/clocks.h"
static inline void segment_program_init(PIO pio, uint sm, uint offset, uint first_segment_pin,
uint first_digit_pin) {
const int segment_pins = 8;
const int digit_pins = 4;
pio_sm_config c = segment_program_get_default_config(offset);
sm_config_set_out_pins(&c, first_segment_pin, segment_pins);
sm_config_set_sideset_pins(&c, first_digit_pin);
sm_config_set_out_shift(&c, false, false, 32);
for (int pin = first_segment_pin; pin < first_segment_pin + segment_pins; pin++) {
pio_gpio_init(pio, pin);
}
for (int pin = first_digit_pin; pin < first_digit_pin + digit_pins; pin++) {
pio_gpio_init(pio, pin);
}
pio_sm_set_consecutive_pindirs(pio, sm, first_segment_pin, segment_pins, true);
pio_sm_set_consecutive_pindirs(pio, sm, first_digit_pin, digit_pins, true);
float div = (float)clock_get_hz(clk_sys) / 2000;
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
#endif