From d14860aab547c68f3cfa6916476ac0f838983662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 1 Apr 2017 17:18:51 +0200 Subject: [PATCH] can now control daisy-chained MAX7219 displays --- lib/led_max7219.c | 57 ++++++++++++++++++++++++----------------------- lib/led_max7219.h | 19 +++++++++++----- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/lib/led_max7219.c b/lib/led_max7219.c index 88c7353..8623505 100644 --- a/lib/led_max7219.c +++ b/lib/led_max7219.c @@ -158,13 +158,16 @@ static const uint8_t ascii_7segments[] = { /** write data on SPI bus and handle load signal * @param[in] data bytes to write */ -static void led_max7219_write(uint16_t data) +static void led_max7219_write(uint16_t data, uint8_t display) { gpio_clear(GPIO(LED_MAX7219_LOAD_PORT), GPIO(LED_MAX7219_LOAD_PIN)); // ensure load pin is low (data is put in MAX7219 register on rising edge) - while (SPI_SR(SPI(LED_MAX7219_SPI))&SPI_SR_BSY); // wait until not busy - spi_send(SPI(LED_MAX7219_SPI), data); // send data - while (!(SPI_SR(SPI(LED_MAX7219_SPI))&SPI_SR_TXE)); // wait until Tx is empty (reference manual says BSY should also cover this, but it doesn't) - while (SPI_SR(SPI(LED_MAX7219_SPI))&SPI_SR_BSY); // wait until not busy (= transmission completed) + for (uint16_t i=0; i<=display; i++) { // go though all displays + while (SPI_SR(SPI(LED_MAX7219_SPI))&SPI_SR_BSY); // wait until not busy + spi_send(SPI(LED_MAX7219_SPI), data); // send data + while (!(SPI_SR(SPI(LED_MAX7219_SPI))&SPI_SR_TXE)); // wait until Tx is empty (reference manual says BSY should also cover this, but it doesn't) + while (SPI_SR(SPI(LED_MAX7219_SPI))&SPI_SR_BSY); // wait until not busy (= transmission completed) + data = 0x0000; // send no-op command to shift initial command to correct display + } gpio_set(GPIO(LED_MAX7219_LOAD_PORT), GPIO(LED_MAX7219_LOAD_PIN)); // create rising edge on load pin for data to be set in MAX7219 register } @@ -186,65 +189,63 @@ void led_max7219_setup(void) spi_init_master(SPI(LED_MAX7219_SPI), SPI_CR1_BAUDRATE_FPCLK_DIV_8, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_DFF_16BIT, SPI_CR1_MSBFIRST); // initialise SPI as master, divide clock by 8 since max MAX7219 clock is 10 MHz and max SPI PCLK clock is 72 Mhz, depending on which SPI is used, set clock polarity to idle low (as in the datasheet of the MAX7219, but not that important), set clock phase to go high when bit is set (depends on polarity) as data is stored on MAX7219 on rising edge), use 16 bits frames (as used by MAX7219), use MSB first spi_set_unidirectional_mode(SPI(LED_MAX7219_SPI)); // we only need to transmit data spi_enable(SPI(LED_MAX7219_SPI)); // enable SPI - - // default setting - led_max7219_off(); // switch display off - led_max7219_test(false); // disable test mode - led_max7219_intensity(15); // set maximum intensity - led_max7219_write(0x0B07); // set scan limit to display all 7 digits } -void led_max7219_on(void) +void led_max7219_on(uint8_t display) { - led_max7219_write(0x0C01); // set scan limit to display all 7 digits + led_max7219_write(0x0C01, display); // put in normal operation more (registers remain as set) } -void led_max7219_off(void) +void led_max7219_off(uint8_t display) { - led_max7219_write(0x0C00); // put in shutdown mode (registers remain as set) + led_max7219_write(0x0C00, display); // put in shutdown mode (registers remain as set) } -void led_max7219_test(bool test) +void led_max7219_test(bool test, uint8_t display) { if (test) { - led_max7219_write(0x0F01); // go into display test mode + led_max7219_write(0x0F01, display); // go into display test mode } else { - led_max7219_write(0x0F00); // go into normal operation mode + led_max7219_write(0x0F00, display); // go into normal operation mode } } -void led_max7219_intensity(uint8_t intensity) +void led_max7219_intensity(uint8_t intensity, uint8_t digits, uint8_t display) { if (intensity>15) { // intensity must be 0-15 (corresponds to (2*brightness+1)/32) return; } - led_max7219_write(0x0A00+intensity); // set brightness + if (digits<1 || digits>8) { // scan limit must bit 0-7 + return; + } + led_max7219_write(0x0A00+intensity, display); // set brightness + led_max7219_write(0x0B00+digits-1, display); // set scan limit to display digits } -bool led_max7219_text(char* text) +bool led_max7219_text(char* text, uint8_t display) { for (uint8_t i=0; i<8; i++) { // input text should only contain printable character (8th bit is used for dots) if ((text[i]&0x7f)<' ' || (text[i]&0x7f)>=' '+LENGTH(ascii_7segments)) { return false; } } - led_max7219_write(0x0900); // disable BCD decoding on all 7 digits + led_max7219_write(0x0900, display); // disable BCD decoding on all 7 digits for (uint8_t i=0; i<8; i++) { // display text - led_max7219_write(((i+1)<<8)+(ascii_7segments[(text[7-i]&0x7f)-' '])+(text[7-i]&0x80)); // send digit (in reverse order) + led_max7219_write(((i+1)<<8)+(ascii_7segments[(text[7-i]&0x7f)-' '])+(text[7-i]&0x80), display); // send digit (in reverse order) } return true; } -void led_max7219_number(uint32_t number, uint8_t dots) +void led_max7219_number(uint32_t number, uint8_t dots, uint8_t display) { - led_max7219_write(0x09FF); // enable BCD decoding on all 7 digits + led_max7219_write(0x09FF, display); // enable BCD decoding on all 7 digits for (uint8_t digit=0; digit<8; digit++) { // go through digits if (0==digit) { // display 0 on 0 only to first digit - led_max7219_write(((digit+1)<<8) + (number%10) + (((dots>>digit)&0x01)<<7)); // display digit + led_max7219_write(((digit+1)<<8) + (number%10) + (((dots>>digit)&0x01)<<7), display); // display digit } else if (0==number) { // display blank on other digits - led_max7219_write(((digit+1)<<8) + 0x0F + (((dots>>digit)&0x01)<<7)); // display blank + led_max7219_write(((digit+1)<<8) + 0x0F + (((dots>>digit)&0x01)<<7), display); // display blank } else { - led_max7219_write(((digit+1)<<8) + (number%10) + (((dots>>digit)&0x01)<<7)); // display digit + led_max7219_write(((digit+1)<<8) + (number%10) + (((dots>>digit)&0x01)<<7), display); // display digit } number /= 10; // get next digit } diff --git a/lib/led_max7219.h b/lib/led_max7219.h index 813d37d..129e61d 100644 --- a/lib/led_max7219.h +++ b/lib/led_max7219.h @@ -25,27 +25,34 @@ */ void led_max7219_setup(void); /** switch display on + * @param[in] display display number in chain */ -void led_max7219_on(void); +void led_max7219_on(uint8_t display); /** switch display off + * @param[in] display display number in chain */ -void led_max7219_off(void); +void led_max7219_off(uint8_t display); /** switch display in test or normal operation mode * @param[in] test switch in test mode (else normal operation) + * @param[in] display display number in chain */ -void led_max7219_test(bool test); +void led_max7219_test(bool test, uint8_t display); /** set display intensity * @param[in] intensity level to set (0-15) + * @param[in] digits number of digits to display (1-8) + * @param[in] display display number in chain */ -void led_max7219_intensity(uint8_t intensity); +void led_max7219_intensity(uint8_t intensity, uint8_t digits, uint8_t display); /** display text * @param[in] text text to display (8 characters) + * @param[in] display display number in chain * @note use first bit of each character to enable dot * @return false if string has unsupported characters */ -bool led_max7219_text(char* text); +bool led_max7219_text(char* text, uint8_t display); /** display number * @param[in] number number to display (8 digits max) * @param[in] dots set bit if dot on corresponding digit should be displayed + * @param[in] display display number in chain */ -void led_max7219_number(uint32_t number, uint8_t dots); +void led_max7219_number(uint32_t number, uint8_t dots, uint8_t display);