|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <string.h> |
| 9 | +#include "pico/stdlib.h" |
| 10 | +#include "pico/binary_info.h" |
| 11 | +#include "hardware/spi.h" |
| 12 | + |
| 13 | +/* Example code to talk to a Max7219 driving an 32x8 pixel LED display via SPI |
| 14 | +
|
| 15 | + NOTE: The device is driven at 5v, but SPI communications are at 3v3 |
| 16 | +
|
| 17 | + Connections on Raspberry Pi Pico board and a generic Max7219 board, other |
| 18 | + boards may vary. |
| 19 | +
|
| 20 | + * GPIO 17 (pin 22) Chip select -> CS on Max7219 board |
| 21 | + * GPIO 18 (pin 24) SCK/spi0_sclk -> CLK on Max7219 board |
| 22 | + * GPIO 19 (pin 25) MOSI/spi0_tx -> DIN on Max7219 board |
| 23 | + * 5v (pin 40) -> VCC on Max7219 board |
| 24 | + * GND (pin 38) -> GND on Max7219 board |
| 25 | +
|
| 26 | + Note: SPI devices can have a number of different naming schemes for pins. See |
| 27 | + the Wikipedia page at https://en.wikipedia.org/wiki/Serial_Peripheral_Interface |
| 28 | + for variations. |
| 29 | +
|
| 30 | +*/ |
| 31 | + |
| 32 | +// This defines how many Max7219 modules we have cascaded together, in this case, we have 4 x 8x8 matrices giving a total of 32x8 |
| 33 | +#define NUM_MODULES 4 |
| 34 | + |
| 35 | +const uint8_t CMD_NOOP = 0; |
| 36 | +const uint8_t CMD_DIGIT0 = 1; // Goes up to 8, for each line |
| 37 | +const uint8_t CMD_DECODEMODE = 9; |
| 38 | +const uint8_t CMD_BRIGHTNESS = 10; |
| 39 | +const uint8_t CMD_SCANLIMIT = 11; |
| 40 | +const uint8_t CMD_SHUTDOWN = 12; |
| 41 | +const uint8_t CMD_DISPLAYTEST = 15; |
| 42 | + |
| 43 | +#ifdef PICO_DEFAULT_SPI_CSN_PIN |
| 44 | +static inline void cs_select() { |
| 45 | + asm volatile("nop \n nop \n nop"); |
| 46 | + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 0); // Active low |
| 47 | + asm volatile("nop \n nop \n nop"); |
| 48 | +} |
| 49 | + |
| 50 | +static inline void cs_deselect() { |
| 51 | + asm volatile("nop \n nop \n nop"); |
| 52 | + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1); |
| 53 | + asm volatile("nop \n nop \n nop"); |
| 54 | +} |
| 55 | +#endif |
| 56 | + |
| 57 | +#if defined(spi_default) && defined(PICO_DEFAULT_SPI_CSN_PIN) |
| 58 | +static void write_register(uint8_t reg, uint8_t data) { |
| 59 | + uint8_t buf[2]; |
| 60 | + buf[0] = reg; |
| 61 | + buf[1] = data; |
| 62 | + cs_select(); |
| 63 | + spi_write_blocking(spi_default, buf, 2); |
| 64 | + cs_deselect(); |
| 65 | + sleep_ms(1); |
| 66 | +} |
| 67 | + |
| 68 | +static void write_register_all(uint8_t reg, uint8_t data) { |
| 69 | + uint8_t buf[2]; |
| 70 | + buf[0] = reg; |
| 71 | + buf[1] = data; |
| 72 | + cs_select(); |
| 73 | + for (int i = 0; i< NUM_MODULES;i++) { |
| 74 | + spi_write_blocking(spi_default, buf, 2); |
| 75 | + } |
| 76 | + cs_deselect(); |
| 77 | +} |
| 78 | + |
| 79 | +#endif |
| 80 | + |
| 81 | +int main() { |
| 82 | + stdio_init_all(); |
| 83 | + |
| 84 | +#if !defined(spi_default) || !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN) |
| 85 | +#warning spi/max7219_32x8_spi example requires a board with SPI pins |
| 86 | + puts("Default SPI pins were not defined"); |
| 87 | +#else |
| 88 | + |
| 89 | + printf("Hello, max7219! Drawing things on a LED matrix since 2022...\n"); |
| 90 | + |
| 91 | + // This example will use SPI0 at 10MHz. |
| 92 | + spi_init(spi_default, 10 * 1000 * 1000); |
| 93 | + gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI); |
| 94 | + gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI); |
| 95 | + |
| 96 | + // Make the SPI pins available to picotool |
| 97 | + bi_decl(bi_2pins_with_func(PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI)); |
| 98 | + |
| 99 | + // Chip select is active-low, so we'll initialise it to a driven-high state |
| 100 | + gpio_init(PICO_DEFAULT_SPI_CSN_PIN); |
| 101 | + gpio_set_dir(PICO_DEFAULT_SPI_CSN_PIN, GPIO_OUT); |
| 102 | + gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1); |
| 103 | + |
| 104 | + // Make the CS pin available to picotool |
| 105 | + bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "SPI CS")); |
| 106 | + |
| 107 | + // Send init sequence to device |
| 108 | + |
| 109 | + write_register_all(CMD_SHUTDOWN, 0); |
| 110 | + write_register_all(CMD_DISPLAYTEST, 0); |
| 111 | + write_register_all(CMD_SCANLIMIT, 7); // Use all lines |
| 112 | + write_register_all(CMD_DECODEMODE, 0); // No BCD decode, just use bit pattern. |
| 113 | + write_register_all(CMD_SHUTDOWN, 1); |
| 114 | + write_register_all(CMD_BRIGHTNESS, 8); |
| 115 | + |
| 116 | + for (int i=0; i<256; i++) { |
| 117 | + for (int j=0; j<8; j++) { |
| 118 | + write_register_all(CMD_DIGIT0+j, i); |
| 119 | + } |
| 120 | + sleep_ms(20); |
| 121 | + } |
| 122 | + |
| 123 | + int bright = 1; |
| 124 | + while (true) { |
| 125 | + for (int i=0; i<8; i++) { |
| 126 | + if (bright & 1) { |
| 127 | + write_register_all(CMD_DIGIT0 + i, 170 >> (i%2)); |
| 128 | + } else { |
| 129 | + write_register_all(CMD_DIGIT0 + i, (170 >> 1) << (i%2)); |
| 130 | + } |
| 131 | + |
| 132 | + write_register_all(CMD_BRIGHTNESS, bright % 16); |
| 133 | + } |
| 134 | + sleep_ms(250); |
| 135 | + |
| 136 | + bright++; |
| 137 | + } |
| 138 | + |
| 139 | + return 0; |
| 140 | +#endif |
| 141 | +} |
0 commit comments