forked for MAX6951. The 3V-variant of MAX7219
Revision 5:e1415fc2841c, committed 2021-07-26
- Comitter:
- davidanasco
- Date:
- Mon Jul 26 09:48:25 2021 +0000
- Parent:
- 4:b5e4379a3d90
- Commit message:
- Initial commit for 7SEG demo
Changed in this revision
max7219.cpp | Show annotated file Show diff for this revision Revisions of this file |
max7219.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r b5e4379a3d90 -r e1415fc2841c max7219.cpp --- a/max7219.cpp Thu May 12 20:06:28 2016 +0000 +++ b/max7219.cpp Mon Jul 26 09:48:25 2021 +0000 @@ -39,7 +39,7 @@ Max7219::Max7219(SPI *spi_bus, PinName cs): _p_spi(spi_bus) { _num_devices = 1; - + _p_cs = new DigitalOut(cs, 1); _spi_owner = false; } @@ -49,10 +49,10 @@ Max7219::Max7219(PinName mosi, PinName miso, PinName sclk, PinName cs) { _num_devices = 1; - + _p_spi = new SPI(mosi, miso, sclk); _p_cs = new DigitalOut(cs, 1); - + _spi_owner = true; } @@ -61,8 +61,8 @@ Max7219::~Max7219() { delete _p_cs; - - if(_spi_owner) + + if(_spi_owner) { delete _p_spi; } @@ -73,13 +73,13 @@ int32_t Max7219::set_num_devices(uint8_t num_devices) { int32_t rtn_val = -1; - + if(num_devices > 0) { _num_devices = num_devices; rtn_val = _num_devices; } - + return(rtn_val); } @@ -88,14 +88,14 @@ void Max7219::set_display_test(void) { uint8_t idx = 0; - - _p_cs->write(0); + + _p_cs->write(0); for(idx = 0; idx < _num_devices; idx++) { _p_spi->write(MAX7219_DISPLAY_TEST); _p_spi->write(1); } - _p_cs->write(1); + _p_cs->write(1); } @@ -103,14 +103,14 @@ void Max7219::clear_display_test(void) { uint8_t idx = 0; - - _p_cs->write(0); + + _p_cs->write(0); for(idx = 0; idx < _num_devices; idx++) { _p_spi->write(MAX7219_DISPLAY_TEST); _p_spi->write(0); } - _p_cs->write(1); + _p_cs->write(1); } @@ -119,7 +119,7 @@ { int32_t rtn_val = -1; uint8_t idx = 0; - + if(config.device_number > _num_devices) { rtn_val = -1; @@ -132,7 +132,7 @@ else { //write DECODE_MODE register of device - _p_cs->write(0); + _p_cs->write(0); for(idx = _num_devices; idx > 0; idx--) { if(config.device_number == idx) @@ -146,12 +146,12 @@ _p_spi->write(0); } } - _p_cs->write(1); - + _p_cs->write(1); + wait_us(1); - + //write INTENSITY register of device - _p_cs->write(0); + _p_cs->write(0); for(idx = _num_devices; idx > 0; idx--) { if(config.device_number == idx) @@ -165,12 +165,12 @@ _p_spi->write(0); } } - _p_cs->write(1); - + _p_cs->write(1); + wait_us(1); - + //write SCAN_LIMT register of device - _p_cs->write(0); + _p_cs->write(0); for(idx = _num_devices; idx > 0; idx--) { if(config.device_number == idx) @@ -184,13 +184,13 @@ _p_spi->write(0); } } - _p_cs->write(1); - + _p_cs->write(1); + wait_us(1); - + rtn_val = 0; } - + return(rtn_val); } @@ -199,38 +199,38 @@ void Max7219::init_display(max7219_configuration_t config) { uint8_t idx = 0; - + //write DECODE_MODE register of all devices - _p_cs->write(0); + _p_cs->write(0); for(idx = 0; idx < _num_devices; idx++) { _p_spi->write(MAX7219_DECODE_MODE); _p_spi->write(config.decode_mode); } - _p_cs->write(1); - + _p_cs->write(1); + wait_us(1); - + //write INTENSITY register of all devices - _p_cs->write(0); + _p_cs->write(0); for(idx = 0; idx < _num_devices; idx++) { _p_spi->write(MAX7219_INTENSITY); _p_spi->write(config.intensity); } - _p_cs->write(1); - + _p_cs->write(1); + wait_us(1); - + //write SCAN_LIMT register of all devices - _p_cs->write(0); + _p_cs->write(0); for(idx = 0; idx < _num_devices; idx++) { _p_spi->write(MAX7219_SCAN_LIMIT); _p_spi->write(config.scan_limit); } - _p_cs->write(1); - + _p_cs->write(1); + wait_us(1); } @@ -240,7 +240,7 @@ { int32_t rtn_val = -1; uint8_t idx = 0; - + if(device_number > _num_devices) { rtn_val = -1; @@ -252,12 +252,12 @@ } else { - _p_cs->write(0); + _p_cs->write(0); for(idx = _num_devices; idx > 0; idx--) { if(device_number == idx) { - _p_spi->write(MAX7219_SHUTDOWN); + _p_spi->write(MAX7219_CONFIGURATION); _p_spi->write(1); } else @@ -266,11 +266,11 @@ _p_spi->write(0); } } - _p_cs->write(1); - + _p_cs->write(1); + rtn_val = 0; } - + return(rtn_val); } @@ -279,23 +279,23 @@ void Max7219::enable_display(void) { uint8_t idx = 0; - - _p_cs->write(0); + + _p_cs->write(0); for(idx = 0; idx < _num_devices; idx++) { - _p_spi->write(MAX7219_SHUTDOWN); + _p_spi->write(MAX7219_CONFIGURATION); _p_spi->write(1); } - _p_cs->write(1); + _p_cs->write(1); } - + -//********************************************************************* +//********************************************************************* int32_t Max7219::disable_device(uint8_t device_number) { int32_t rtn_val = -1; uint8_t idx = 0; - + if(device_number > _num_devices) { rtn_val = -1; @@ -307,12 +307,12 @@ } else { - _p_cs->write(0); + _p_cs->write(0); for(idx = _num_devices; idx > 0; idx--) { if(device_number == idx) { - _p_spi->write(MAX7219_SHUTDOWN); + _p_spi->write(MAX7219_CONFIGURATION); _p_spi->write(0); } else @@ -321,42 +321,42 @@ _p_spi->write(0); } } - _p_cs->write(1); - + _p_cs->write(1); + rtn_val = 0; } - + return(rtn_val); } -//********************************************************************* +//********************************************************************* void Max7219::disable_display(void) { uint8_t idx = 0; - - _p_cs->write(0); + + _p_cs->write(0); for(idx = 0; idx < _num_devices; idx++) { - _p_spi->write(MAX7219_SHUTDOWN); + _p_spi->write(MAX7219_CONFIGURATION); _p_spi->write(0); } - _p_cs->write(1); + _p_cs->write(1); } -//********************************************************************* +//********************************************************************* int32_t Max7219::write_digit(uint8_t device_number, uint8_t digit, uint8_t data) { int32_t rtn_val = -1; uint8_t idx = 0; - - if(digit > MAX7219_DIGIT_7) + + if(digit > MAX7219_D7_P0) { rtn_val = -3; } - else if(digit < MAX7219_DIGIT_0) + else if(digit < MAX7219_D0_P0) { rtn_val = -4; } @@ -372,7 +372,7 @@ } else { - _p_cs->write(0); + _p_cs->write(0); for(idx = _num_devices; idx > 0; idx--) { if(idx == device_number) @@ -386,27 +386,27 @@ _p_spi->write(0); } } - _p_cs->write(1); - + _p_cs->write(1); + rtn_val = 0; } } - + return(rtn_val); } - + -//********************************************************************* +//********************************************************************* int32_t Max7219::clear_digit(uint8_t device_number, uint8_t digit) { int32_t rtn_val = -1; uint8_t idx = 0; - - if(digit > MAX7219_DIGIT_7) + + if(digit > MAX7219_D7_P0) { rtn_val = -3; } - else if(digit < MAX7219_DIGIT_0) + else if(digit < MAX7219_D0_P0) { rtn_val = -4; } @@ -422,7 +422,7 @@ } else { - _p_cs->write(0); + _p_cs->write(0); for(idx = _num_devices; idx > 0; idx--) { if(idx == device_number) @@ -436,22 +436,22 @@ _p_spi->write(0); } } - _p_cs->write(1); - + _p_cs->write(1); + rtn_val = 0; } } - + return(rtn_val); } -//********************************************************************* +//********************************************************************* int32_t Max7219::device_all_on(uint8_t device_number) { int32_t rtn_val = -1; uint8_t idx = 0; - + if(device_number > _num_devices) { rtn_val = -1; @@ -463,7 +463,7 @@ else { rtn_val = 0; - + //writes every digit of given device to 0xFF for(idx = 0; idx < 8; idx++) { @@ -473,17 +473,17 @@ } } } - + return(rtn_val); } -//********************************************************************* +//********************************************************************* int32_t Max7219::device_all_off(uint8_t device_number) { int32_t rtn_val = -1; uint8_t idx = 0; - + if(device_number > _num_devices) { rtn_val = -1; @@ -495,7 +495,7 @@ else { rtn_val = 0; - + //writes every digit of given device to 0 for(idx = 0; idx < 8; idx++) { @@ -505,7 +505,7 @@ } } } - + return(rtn_val); } @@ -513,29 +513,28 @@ void Max7219::display_all_on(void) { uint8_t idx, idy; - + //writes every digit of every device to 0xFF for(idx = 0; idx < _num_devices; idx++) { - for(idy = 0; idy < MAX7219_DIGIT_7; idy++) + for(idy = 0; idy < MAX7219_D7_P0; idy++) { write_digit((idx + 1), (idy + 1), 0xFF); } } } - - + + void Max7219::display_all_off(void) { uint8_t idx, idy; - + //writes every digit of every device to 0 for(idx = 0; idx < _num_devices; idx++) { - for(idy = 0; idy < MAX7219_DIGIT_7; idy++) + for(idy = 0; idy < MAX7219_D7_P0; idy++) { write_digit((idx + 1), (idy + 1), 0); } } } -
diff -r b5e4379a3d90 -r e1415fc2841c max7219.h --- a/max7219.h Thu May 12 20:06:28 2016 +0000 +++ b/max7219.h Mon Jul 26 09:48:25 2021 +0000 @@ -102,7 +102,7 @@ class Max7219 { public: - + /** * @brief Digit and Control Registers * @details The 14 addressable digit and control registers. @@ -110,22 +110,38 @@ typedef enum { MAX7219_NO_OP = 0, - MAX7219_DIGIT_0, - MAX7219_DIGIT_1, - MAX7219_DIGIT_2, - MAX7219_DIGIT_3, - MAX7219_DIGIT_4, - MAX7219_DIGIT_5, - MAX7219_DIGIT_6, - MAX7219_DIGIT_7, MAX7219_DECODE_MODE, MAX7219_INTENSITY, MAX7219_SCAN_LIMIT, - MAX7219_SHUTDOWN, - MAX7219_DISPLAY_TEST = 15 + MAX7219_CONFIGURATION, + MAX7219_DISPLAY_TEST = 7, + MAX7219_D0_P0 = 0x20, + MAX7219_D1_P0 = 0x21, + MAX7219_D2_P0 = 0x22, + MAX7219_D3_P0 = 0x23, + MAX7219_D4_P0 = 0x24, + MAX7219_D5_P0 = 0x25, + MAX7219_D6_P0 = 0x26, + MAX7219_D7_P0 = 0x27, + MAX7219_D0_P1 = 0x40, + MAX7219_D1_P1 = 0x41, + MAX7219_D2_P1 = 0x42, + MAX7219_D3_P1 = 0x43, + MAX7219_D4_P1 = 0x44, + MAX7219_D5_P1 = 0x45, + MAX7219_D6_P1 = 0x46, + MAX7219_D7_P1 = 0x47, + MAX7219_D0_P01 = 0x60, + MAX7219_D1_P01 = 0x61, + MAX7219_D2_P01 = 0x62, + MAX7219_D3_P01 = 0x63, + MAX7219_D4_P01 = 0x64, + MAX7219_D5_P01 = 0x65, + MAX7219_D6_P01 = 0x66, + MAX7219_D7_P01 = 0x67, }max7219_register_e; - - + + /** * @brief Intensity values * @details Digital control of display brightness is provided by an @@ -151,11 +167,11 @@ MAX7219_INTENSITY_E, MAX7219_INTENSITY_F }max7219_intensity_e; - - + + /** * @brief Scan limit for mutiplexing digits - * @details The scan-limit register sets how many digits are + * @details The scan-limit register sets how many digits are * displayed, from 1 to 8. They are displayed in a multiplexed * manner with a typical display scan rate of 800Hz with 8 * digits displayed. @@ -171,11 +187,11 @@ MAX7219_SCAN_7, MAX7219_SCAN_8 }max7219_scan_limit_e; - - + + /**********************************************************//** - * @brief Constructor for Max7219 Class. - * + * @brief Constructor for Max7219 Class. + * * @details Allows user to pass pointer to existing SPI bus * * On Entry: @@ -187,11 +203,11 @@ * @return None **************************************************************/ Max7219(SPI *spi_bus, PinName cs); - - + + /**********************************************************//** - * @brief Constructor for Max7219 Class. - * + * @brief Constructor for Max7219 Class. + * * @details Allows user to specify SPI peripheral to use * * On Entry: @@ -205,12 +221,12 @@ * @return None **************************************************************/ Max7219(PinName mosi, PinName miso, PinName sclk, PinName cs); - - + + /**********************************************************//** - * @brief Default destructor for Max7219 Class. + * @brief Default destructor for Max7219 Class. * - * @details Destroys SPI object if owner + * @details Destroys SPI object if owner * * On Entry: * @@ -219,27 +235,27 @@ * @return None **************************************************************/ ~Max7219(); - - + + /**********************************************************//** * @brief Sets the number of MAX7219 devices being used. * Defaults to one * - * @details + * @details * * On Entry: * @param[in] num_devices - number of MAX7219 devices being - * used, max of 255 + * used, max of 255 * * On Exit: * * @return Returns number of devices **************************************************************/ int32_t set_num_devices(uint8_t num_devices); - - + + /**********************************************************//** - * @brief Tests all devices being used + * @brief Tests all devices being used * * @details Sets bit0 of DISPLAY_TEST regiser in all devices * @@ -250,8 +266,8 @@ * @return None **************************************************************/ void set_display_test(void); - - + + /**********************************************************//** * @brief Stops test * @@ -264,63 +280,63 @@ * @return None **************************************************************/ void clear_display_test(void); - - + + /**********************************************************//** - * @brief Initializes specific device in display with given + * @brief Initializes specific device in display with given * config data * - * @details + * @details * * On Entry: - * @param[in] config - Structure containing configuration + * @param[in] config - Structure containing configuration * data of device * * On Exit: * - * @return Returns 0 on success\n + * @return Returns 0 on success\n * Returns -1 if device number is > _num_devices\n * Returns -2 if device number is 0\n **************************************************************/ int32_t init_device(max7219_configuration_t config); - - + + /**********************************************************//** * @brief Initializes all devices with given config data * * @details All devices are configured with given data * * On Entry: - * @param[in] config - Structure containing configuration + * @param[in] config - Structure containing configuration * data * On Exit: * * @return None **************************************************************/ void init_display(max7219_configuration_t config); - - + + /**********************************************************//** * @brief Enables specific device in display * - * @details + * @details * * On Entry: * @param[in] device_number - device to enable * * On Exit: * - * @return Returns 0 on success\n + * @return Returns 0 on success\n * Returns -1 if device number is > _num_devices\n * Returns -2 if device number is 0\n **************************************************************/ int32_t enable_device(uint8_t device_number); - - + + /**********************************************************//** - * @brief Enables all device in display + * @brief Enables all device in display * - * @details + * @details * * On Entry: * @@ -329,28 +345,28 @@ * @return None **************************************************************/ void enable_display(void); - - + + /**********************************************************//** * @brief Disables specific device in display * - * @details + * @details * * On Entry: * @param[in] device_number - device to disable * * On Exit: - * @return Returns 0 on success\n + * @return Returns 0 on success\n * Returns -1 if device number is > _num_devices\n * Returns -2 if device number is 0\n **************************************************************/ int32_t disable_device(uint8_t device_number); - - + + /**********************************************************//** * @brief Disables all devices in display * - * @details + * @details * * On Entry: * @@ -359,13 +375,13 @@ * @return None **************************************************************/ void disable_display(void); - - + + /**********************************************************//** * @brief Writes digit of given device with given data, user * must enter correct data for decode_mode chosen * - * @details + * @details * * On Entry: * @param[in] device_number - device to write too @@ -374,19 +390,19 @@ * * On Exit: * - * @return Returns 0 on success\n + * @return Returns 0 on success\n * Returns -1 if device number is > _num_devices\n * Returns -2 if device number is 0\n * Returns -3 if digit > 8\n * Returns -4 if digit < 1\n **************************************************************/ int32_t write_digit(uint8_t device_number, uint8_t digit, uint8_t data); - - + + /**********************************************************//** * @brief Clears digit of given device * - * @details + * @details * * On Entry: * @param[in] device_number - device to write too @@ -394,53 +410,53 @@ * * On Exit: * - * @return Returns 0 on success\n + * @return Returns 0 on success\n * Returns -1 if device number is > _num_devices\n * Returns -2 if device number is 0\n * Returns -3 if digit > 8\n * Returns -4 if digit < 1\n **************************************************************/ int32_t clear_digit(uint8_t device_number, uint8_t digit); - - + + /**********************************************************//** * @brief Turns on all segments/digits of given device * - * @details + * @details * * On Entry: * @param[in] device_number - device to write too * * On Exit: * - * @return Returns 0 on success\n + * @return Returns 0 on success\n * Returns -1 if device number is > _num_devices\n * Returns -2 if device number is 0\n **************************************************************/ int32_t device_all_on(uint8_t device_number); - - + + /**********************************************************//** * @brief Turns off all segments/digits of given device * - * @details + * @details * * On Entry: * @param[in] device_number - device to write too * * On Exit: * - * @return Returns 0 on success\n + * @return Returns 0 on success\n * Returns -1 if device number is > _num_devices\n * Returns -2 if device number is 0\n **************************************************************/ int32_t device_all_off(uint8_t device_number); - - + + /**********************************************************//** * @brief Turns on all segments/digits of display * - * @details + * @details * * On Entry: * @@ -449,12 +465,12 @@ * @return None **************************************************************/ void display_all_on(void); - - + + /**********************************************************//** * @brief Turns off all segments/digits of display * - * @details + * @details * * On Entry: * @@ -463,15 +479,15 @@ * @return None **************************************************************/ void display_all_off(void); - - + + private: - + SPI *_p_spi; DigitalOut *_p_cs; bool _spi_owner; - + uint8_t _num_devices; - + }; -#endif /* MAX7219_H*/ \ No newline at end of file +#endif /* MAX7219_H*/