/**
* Pi Pico PIO driving a 4-digit seven segment display example.
*
* Copyright (C) 2021, Uri Shaked
*/
//#define TIMER_INTERRUPT_DEBUG 1
//#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "MBED_RPi_Pico_TimerInterrupt.h"
#include "segment.pio.h"
#include "SN595.pio.h"
const uint8_t first_segment_pin = 2;
const uint8_t first_digit_pin = 6;
const uint8_t shift_data_pin = 18;
const uint8_t shift_clock_pin = 19;
// Init RPI_PICO_Timer
MBED_RPI_PICO_Timer ITimer1(1);
#define TIMER1_INTERVAL_MS 100
volatile uint milliscounter = 0;
void TimerHandler1(uint alarm_num)
{
///////////////////////////////////////////////////////////
// Always call this for MBED RP2040 before processing ISR
TIMER_ISR_START(alarm_num);
///////////////////////////////////////////////////////////
displayNumber(milliscounter);
milliscounter=milliscounter+1;
////////////////////////////////////////////////////////////
// Always call this for MBED RP2040 after processing ISR
TIMER_ISR_END(alarm_num);
////////////////////////////////////////////////////////////
}
void setup() {
Serial1.begin(115200);
Serial1.println("Raspberry Pi Pico PIO 7-Segment Example");
//pinMode(p10, OUTPUT);
//digitalWrite(p10, HIGH);
// Load the PIO program and initialize the machine
auto offset = pio_add_program(pio0, &bcd_output_program);
segment_program_init(pio0, 0, offset, first_segment_pin, first_digit_pin);
// Load the PIO program and initialize the machine
auto offset2 = pio_add_program(pio1, &SN595_program);
SN595_program_init(pio1, 0, offset2, shift_data_pin, shift_clock_pin);
//init the timer
ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerHandler1);
}
void displayNumber(uint value) {
pio_sm_put(pio0, 0,
(value / 1000 % 10)<<12 |
(value / 100 % 10)<<8 |
(value / 10 % 10)<<4 |
(value % 10)
);
pio_sm_put(pio1, 0, (1)<<value);
}
void loop() {
}