add MAX7219 7-segments library

This commit is contained in:
King Kévin 2017-03-07 16:39:16 +01:00
parent 805050e169
commit e3bd04fce6
2 changed files with 280 additions and 0 deletions

235
lib/led_max7219.c Normal file
View File

@ -0,0 +1,235 @@
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** library to communicate with a Titan Micro MAX7219 IC attached to a 4-digit 7-segment (code)
* @file led_max7219.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2017
* @note peripherals used: GPIO @ref led_max7219_gpio, timer @ref led_tm1637_timer
* @note the protocol is very similar to I2C but incompatible for the following reasons: the capacitance is too large for open-drain type output with weak pull-up resistors (push-pull needs to be used, preventing to get ACKs since no indication of the ACK timing is provided); the devices doesn't use addresses; the STM32 I2C will switch to receiver mode when the first sent byte (the I2C address) has last bit set to 1 (such as for address commands with B7=1 where B7 is transmitted last), preventing to send further bytes (the data byte after the address)
* @warning all calls are blocking
*
* bit vs segment: 0bpgfedcba
* +a+
* f b p
* +g+
* e c p
* +b+
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // general utilities
#include <string.h> // string utilities
/* STM32 (including CM3) libraries */
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/spi.h> // SPI library
#include <libopencm3/stm32/timer.h> // timer library
#include "global.h" // global utilities
#include "led_max7219.h" // MAX7219 header and definitions
/** @defgroup led_max7219_gpio GPIO used to control MAX7219 IC load line
* @{
*/
#define LED_MAX7219_LOAD_PORT B /**< port for load line */
#define LED_MAX7219_LOAD_PIN 12 /**< pin for load line */
/** @} */
/** @defgroup led_max7219_spi SPI used to communication with MAX7219 IC
* @{
*/
#define LED_MAX7219_SPI 2 /**< SPI to send data */
/** @} */
/** ASCII characters encoded for the 7 segments digit block
* @note starts with space
*/
static const uint8_t ascii_7segments[] = {
0x00, // 0b00000000 space
0x30, // 0b00110000 ! (I)
0x22, // 0b00100010 "
0x5c, // 0b01011100 # (o)
0x6d, // 0b01101101 $ (s)
0x52, // 0b01010010 % (/)
0x7d, // 0b01111101 & (6)
0x20, // 0b00100000 '
0x39, // 0b00111001 ( ([)
0x0f, // 0b00001111 )
0x70, // 0b01110000 *
0x46, // 0b01000110 +
0x10, // 0b00010000 ,
0x40, // 0b01000000 -
0x10, // 0b00010000 . (,)
0x52, // 0b01010010 /
0x3f, // 0b00111111 0
0x06, // 0b00000110 1
0x5b, // 0b01011011 2
0x4f, // 0b01001111 3
0x66, // 0b01100110 4
0x6d, // 0b01101101 5
0x7d, // 0b01111101 6
0x07, // 0b00000111 7
0x7f, // 0b01111111 8
0x6f, // 0b01101111 9
0x48, // 0b01001000 : (=)
0x48, // 0b01001000 ; (=)
0x58, // 0b01011000 <
0x48, // 0b01001000 =
0x4c, // 0b01001100 >
0x53, // 0b01010011 ?
0x7b, // 0b01111011 @
0x77, // 0b01110111 A
0x7f, // 0b01111111 B
0x39, // 0b00111001 C
0x5e, // 0b01011110 D
0x79, // 0b01111001 E
0x71, // 0b01110001 F
0x3d, // 0b00111101 G
0x76, // 0b01110110 H
0x30, // 0b00110000 I
0x1e, // 0b00011110 J
0x76, // 0b01110110 K
0x38, // 0b00111000 L
0x37, // 0b00110111 M
0x37, // 0b00110111 N
0x3f, // 0b00111111 O
0x73, // 0b01110011 P
0x6b, // 0b01101011 Q
0x33, // 0b00110011 R
0x6d, // 0b01101101 S
0x78, // 0b01111000 T
0x3e, // 0b00111110 U
0x3e, // 0b00111110 V (U)
0x3e, // 0b00111110 W (U)
0x76, // 0b01110110 X (H)
0x6e, // 0b01101110 Y
0x5b, // 0b01011011 Z
0x39, // 0b00111001 [
0x64, // 0b01100100 '\'
0x0f, // 0b00001111 /
0x23, // 0b00100011 ^
0x08, // 0b00001000 _
0x02, // 0b00000010 `
0x5f, // 0b01011111 a
0x7c, // 0b01111100 b
0x58, // 0b01011000 c
0x5e, // 0b01011110 d
0x7b, // 0b01111011 e
0x71, // 0b01110001 f
0x6f, // 0b01101111 g
0x74, // 0b01110100 h
0x10, // 0b00010000 i
0x0c, // 0b00001100 j
0x76, // 0b01110110 k
0x30, // 0b00110000 l
0x54, // 0b01010100 m
0x54, // 0b01010100 n
0x5c, // 0b01011100 o
0x73, // 0b01110011 p
0x67, // 0b01100111 q
0x50, // 0b01010000 r
0x6d, // 0b01101101 s
0x78, // 0b01111000 t
0x1c, // 0b00011100 u
0x1c, // 0b00011100 v (u)
0x1c, // 0b00011100 w (u)
0x76, // 0b01110110 x
0x6e, // 0b01101110 y
0x5b, // 0b01011011 z
0x39, // 0b00111001 { ([)
0x30, // 0b00110000 |
0x0f, // 0b00001111 } ([)
0x40, // 0b01000000 ~
};
void led_max7219_setup(void)
{
// configure GPIO for load line
rcc_periph_clock_enable(RCC_GPIO(LED_MAX7219_LOAD_PORT)); // enable clock for GPIO peripheral
gpio_clear(GPIO(LED_MAX7219_LOAD_PORT), GPIO(LED_MAX7219_LOAD_PIN)); // idle low (load on rising edge)
gpio_set_mode(GPIO(LED_MAX7219_LOAD_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_MAX7219_LOAD_PIN)); // set as output
// configure SPI peripheral
rcc_periph_clock_enable(RCC_SPI_SCK_PORT(LED_MAX7219_SPI)); // enable clock for GPIO peripheral for clock signal
gpio_set_mode(SPI_SCK_PORT(LED_MAX7219_SPI), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_SCK_PIN(LED_MAX7219_SPI)); // set as output (max clock speed for MAX7219 is 10 MHz)
rcc_periph_clock_enable(RCC_SPI_MOSI_PORT(LED_MAX7219_SPI)); // enable clock for GPIO peripheral for MOSI signal
gpio_set_mode(SPI_MOSI_PORT(LED_MAX7219_SPI), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_MOSI_PIN(LED_MAX7219_SPI)); // set as output
rcc_periph_clock_enable(RCC_AFIO); // enable clock for SPI alternate function
rcc_periph_clock_enable(RCC_SPI(LED_MAX7219_SPI)); // enable clock for SPI peripheral
spi_reset(SPI(LED_MAX7219_SPI)); // clear SPI values to default
spi_init_master(SPI(LED_MAX7219_SPI), SPI_CR1_BAUDRATE_FPCLK_DIV_16, 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 high (polarity is not really important for the MAX7219), 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_ss_output(SPI(LED_MAX7219_SPI)); // use NSS for LOAD line
//spi_enable_software_slave_management(SPI(LED_MAX7219_SPI)); // control the slave select in software (since there is chip select)
//spi_set_nss_low(SPI(LED_MAX7219_SPI)); // set NSS low so we can output
spi_enable(SPI(LED_MAX7219_SPI)); // enable SPI
}
/** write data on bus
* @param[in] data bytes to write
*/
static void led_max7219_write(uint16_t data)
{
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)
spi_set_nss_low(SPI(LED_MAX7219_SPI));
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)
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
spi_set_nss_high(SPI(LED_MAX7219_SPI));
}
void led_max7219_on(void)
{
led_max7219_write(0x0B70); // set scan limit to display all 7 digits
}
void led_max7219_off(void)
{
led_max7219_write(0x0C00); // put in shutdown mode (registers remain as set)
}
void led_max7219_test(bool test)
{
if (test) {
led_max7219_write(0x0F01); // go into display test mode
} else {
led_max7219_write(0x0F00); // go into normal operation mode
}
}
void led_max7219_intensity(uint8_t intensity)
{
if (intensity>15) { // intensity must be 0-15 (corresponds to (2*brightness+1)/32)
return;
}
led_max7219_write(0x0A00+intensity); // set brightness
}
void led_max7219_text(char* text)
{
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;
}
}
for (uint8_t i=0; i<8; i++) { // display text
led_max7219_write(((i+1)<<8)+(ascii_7segments[text[i]&0x7f])+(text[i]&0x80)); // send digit
}
}

45
lib/led_max7219.h Normal file
View File

@ -0,0 +1,45 @@
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** library to communicate with a Maxim MAX7219 IC attached to a 8-digit 7-segment (API)
* @file led_tmax7219.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2017
* @note peripherals used: GPIO @ref led_max7219_gpio, SPI @ref led_max7219_spi
*/
#pragma once
/** setup communication with MAX7219 IC
*/
void led_max7219_setup(void);
/** switch display on
*/
void led_max7219_on(void);
/** switch display off
*/
void led_max7219_off(void);
/** switch display in test or normal operation mode
* @param[in] test switch in test mode (else normal operation)
*/
void led_max7219_test(bool test);
/** set display intensity
* @param[in] intensity level to set (0-15)
*/
void led_max7219_intensity(uint8_t intensity);
/** display text
* @param[in] text text to display (8 characters)
* @note use first bit of each character to enable dot
*/
void led_max7219_text(char* text);