Pi PICO 4-segment display counter




The main CODE is here: 
/**
 * Pi Pico PIO driving a 4-digit seven segment display example.
 *
 * Copyright (C) 2021, Uri Shaked
 */

#include "segment.pio.h"

uint8_t digits[] = {
  0b11000000, // 0
  0b11111001, // 1
  0b10100100, // 2
  0b10110000, // 3
  0b10011001, // 4
  0b10010010, // 5
  0b10000010, // 6
  0b11111000, // 7
  0b10000000, // 8
  0b10011000, // 9
};

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");

  // Load the PIO program and initialize the machine
  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);
}
The segment CODE is HERE:
// -------------------------------------------------- //
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //

#pragma once

#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif

// ------- //
// segment //
// ------- //

#define segment_wrap_target 0
#define segment_wrap 5

static const uint16_t segment_program_instructions[] = {
            //     .wrap_target
    0x8080, //  0: pull   noblock         side 0    
    0xa027, //  1: mov    x, osr          side 0    
    0x6208, //  2: out    pins, 8         side 1    
    0x6408, //  3: out    pins, 8         side 2    
    0x6808, //  4: out    pins, 8         side 4    
    0x7008, //  5: out    pins, 8         side 8    
            //     .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program segment_program = {
    .instructions = segment_program_instructions,
    .length = 6,
    .origin = -1,
};

static inline pio_sm_config segment_program_get_default_config(uint 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;

    // Machine configuration: out pins, sideset pins, and shift direction.
    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); // Shift to the left

    // Attach all pins to the PIO peripheral
    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);
    }

    // Configure all pins as outputs
    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);

    // Run at 2000Hz:
    float div = (float)clock_get_hz(clk_sys) / 2000;
    sm_config_set_clkdiv(&c, div);

    // Start the machine
    pio_sm_init(pio, sm, offset, &c);
    pio_sm_set_enabled(pio, sm, true);
}

#endif

Comments

Popular posts from this blog

Digital clock " DS1307 RTC" with PIC16F877A microcontroller and I2C

Smart Elevator simulation with arduino uno and Proteus

Temperature sensor with PIC16F877a using Analog to Digital convertion.