#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/spi.h"
/*
Our 7 Segment display has pins as follows:
--A--
F B
--G--
E C
--D--
*/
// using default SPI0 on pins GP16 (SPI RX/MISO), GP18 (SCK) and GP19 (SPI TX/MOSI)
const unsigned int mosi = 19;
const unsigned int sck = 18;
const unsigned int cs = 7;
spi_inst_t *spi = spi0; // make sure spi is spi0
// This array converts a number 0-9 to a bit pattern to send to the GPIO's
int bits[10] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
0x07, // 7
0x7f, // 8
0x67 // 9
};
void main() {
spi_init(spi, 500*1000);
gpio_set_function(cs, GPIO_FUNC_SIO);
gpio_set_dir(cs, GPIO_OUT);
// Make sure the CS is low at start
gpio_put(cs, 0);
gpio_set_function(sck, GPIO_FUNC_SPI);
gpio_set_function(mosi, GPIO_FUNC_SPI);
sleep_ms(1000);
uint8_t val = 0;
while(1) {
if (val == 9) val = 0;
else val++;
// Get mask - common anode so need to put 0 out to turn LED segment on.
unsigned char mask = ~bits[val];
char str[25];
// Set all our GPIO's in one go!
spi_write_blocking(spi, &mask, 1);
// pulse the CS to put data in shift register out
gpio_put(cs, 1);
sleep_ms(50);
gpio_put(cs, 0);
sleep_ms(950);
}
}