2013-11-06

Cascading MAX7219 Matrix LEDs funtion tested OK






























After some hard thought, I concluded that one possible cause of me always getting one digit displayed is that I made a careless mistake of always sending 16 bits even for 2 digits.  The reason is that serial output only begins after the 16 bit.  In other words, to entertain 2 digits, I need to send 32 bits in one go.

So I modified the function to handle digit at any position.  Then I made one more careless mistake of sending the least significant digit last.  Actually it should be send first.

Finaly I have all the bugs removed, and get 2 digits displayed.

And I call it a day.




// ***********************************************************************
// Program  - Fong EKG v5.0
// Function - Olimex EKG Board Evaluation
// Author   - TL Fong
// Build    - 2013.11.06.02
// Date     - 2013nov06hkt2152
// Hardware - Olimex/CooCox/MagicBlue/WHUT/Somy LPC1114/C14/301/FN28
//            Olimex EKG board (SHIELD-EKG/EMG Rev B, Bulgaria 2011)
// Software - GCC ARM 4.7, CoIDE 1.7.5,  CoLinkEx 1.1, Flash Magic v7.66
// ***********************************************************************

#include "test050.h"

...
testMax7219SegmentLed01(SPI_CHANNEL_0);

// testMax7219MatrixLed01(SPI_CHANNEL_1);

testMax7219MatrixLed02(SPI_CHANNEL_1);

// ***********************************************************************
// End
// ***********************************************************************



// ***********************************************************************
// test050.h 2013oct24hkt1022
// ***********************************************************************

#include "led050.h"
#include "key050.h"
#include "spi050.h"
#include "adc050.h"
#include "sled050.h"
#include "eeprom050.h"
#include "mled050.h"

// ***************************************************************************
// Function - Test MAX7219 8x8 matrix LED
// Date     - 2013nov06
// ***************************************************************************
void testMax7219MatrixLed02(uint8_t spiChannelNumber)
{
setupSpi051(spiChannelNumber);
max7219MatrixLed02(spiChannelNumber);
}

void testMax7219MatrixLed01(uint8_t spiChannelNumber)
{
setupSpi051(spiChannelNumber);
max7219MatrixLed01(spiChannelNumber);
}

...

// ***********************************************************************
// End
// ***********************************************************************



// ***********************************************************************
// mled050.h 2013nov06hkt1128
// 4-digit 7-segment and 8x8 matrix LED controlled by MAX7219
// ***********************************************************************

#include "spi050.h"

// *** MAX7219 Register Address Map ***
#define NO_OP_ADDR 0x00
#define DIGIT_ADDR_0 0x01
#define DIGIT_ADDR_1 0x02
#define DIGIT_ADDR_2 0x03
#define DIGIT_ADDR_3 0x04
#define DIGIT_ADDR_4 0x05
#define DIGIT_ADDR_5 0x06
#define DIGIT_ADDR_6 0x07
#define DIGIT_ADDR_7 0x08
#define DECODE_MODE_ADDR 0x09
#define INTENSITY_ADDR     0x0a
#define SCAN_LIMIT_ADDR 0x0b
#define OPERATION_ADDR   0x0c // Shutdown address actually
#define DISPLAY_TEST_ADDR   0x0f

// *** MAX7219 Command Format ***
#define SHUT_DOWN            0x00
#define NORMAL               0x01
#define CODE_B_ALL_DIGITS    0xff
#define NO_DECODE_ALL_DIGITS 0x00
#define DISPLAY_8_DIGITS     0x07

// *** Data Constants ***
#define DIGIT_0 0
#define DIGIT_1 1
#define DIGIT_2 2
#define DIGIT_3 3
#define DIGIT_4 4
#define DIGIT_5 5
#define DIGIT_6 6
#define DIGIT_7 7
#define DIGIT_8 8
#define DIGIT_9 9
#define CHAR_HYPHEN 10
#define CHAR_E 11
#define CHAR_H 12
#define CHAR_L 13
#define CHAR_P 14
#define CHAR_BLANK 15

#define PATTERN_ALL_ON 0xff
#define PATTERN_ALL_OFF 0x00

#define MAX_BUFFER_SIZE 16

uint8_t char_A[5] = {0x7E, 0x11, 0x11, 0x11, 0x7E};
uint8_t char_K[5] = {0x41, 0x22, 0x14, 0x08, 0x7f};
uint8_t char_O[5] = {0x3E, 0x41, 0x41, 0x41, 0x3E};

// ***************************************************************************
// Function - Display 8x8 matrix LED
// Date     - 2013nov06
// ***************************************************************************

void max7219MatrixLed02(uint8_t spiChannelNumber)
{
    // *** Print project title ***********************************************
printf("\n\n*** Matrix LED 02 - 2013nov06hkt2100 ***\n\n");

// *** Setup MAX7219 digit control register buffer ***********************
    #define MAX_DEVICE_NUMBER 16 // maximum 16 MAX7219 devices
#define ADDRESS_INDEX 0
#define DATA_INDEX 1

    uint8_t deviceNumber;

    deviceNumber = 1;

    // *** Setup MAX7219 for digit decode and scan limit ***
    setUpMax7219NoDecode02(deviceNumber, spiChannelNumber);

    // *** Display 8 digits ***

    writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);

    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_7, char_O[0]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_6, char_O[1]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_5, char_O[2]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_4, char_O[3]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_3, char_O[4]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_2, PATTERN_ALL_OFF);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_1, PATTERN_ALL_OFF);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_0, PATTERN_ALL_OFF);

    writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, NORMAL);

    deviceNumber = 2;

    // *** Setup MAX7219 for digit decode and scan limit ***
    setUpMax7219NoDecode02(deviceNumber, spiChannelNumber);

    // *** Display 8 digits ***

    writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);

    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_7, char_K[0]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_6, char_K[1]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_5, char_K[2]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_4, char_K[3]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_3, char_K[4]);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_2, PATTERN_ALL_OFF);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_1, PATTERN_ALL_OFF);
    displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_0, PATTERN_ALL_OFF);

    writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, NORMAL);
}

void max7219MatrixLed01(uint8_t spiChannelNumber)
{
    // *** Print project title ***********************************************
printf("\n\n*** Matrix LED 01 - 2013nov06hkt1154 ***\n\n");

// *** Setup MAX7219 digit control register buffer ***********************
#define MAX_BUFFER_SIZE 16
#define ADDRESS_INDEX 0
#define DATA_INDEX 1

    #define BUFFER_SIZE 2

uint8_t txBuffer[BUFFER_SIZE];
uint8_t rxBuffer[BUFFER_SIZE];

    // *** Setup SPI xferConfig ***
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = txBuffer;
    xferConfig.rx_data = rxBuffer;
    xferConfig.length = BUFFER_SIZE;

    // *** Setup MAX7219 for digit decode and scan limit ***
    setUpMax7219NoDecode(txBuffer, xferConfig, spiChannelNumber);


    // *** Display 8 digits ***

writeLedCommand(txBuffer, xferConfig, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);

displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_7, char_A[0]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_6, char_A[1]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_5, char_A[2]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_4, char_A[3]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_3, char_A[4]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_2, PATTERN_ALL_OFF);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_1, PATTERN_ALL_OFF);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_0, PATTERN_ALL_OFF);

    /*
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_7, char_K[0]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_6, char_K[1]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_5, char_K[2]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_4, char_K[3]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_3, char_K[4]);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_2, PATTERN_ALL_OFF);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_1, PATTERN_ALL_OFF);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_0, PATTERN_ALL_OFF);
    */

    writeLedCommand(txBuffer, xferConfig, spiChannelNumber, OPERATION_ADDR, NORMAL);
}


// ***************************************************************************
// Function - Display 4 digits of 7-segment LED
// Date     - 2013nov06
// ***************************************************************************
void max7219SegmentLed01(uint8_t spiChannelNumber)
{
    // *** Print project title ***********************************************
printf("\n\n*** Segment LED 01 - 2013nov06hkt1128 ***\n\n");

// *** Setup MAX7219 digit control register buffer ***********************
#define BUFFER_SIZE 2
#define ADDRESS_INDEX 0
#define DATA_INDEX 1

uint8_t txBuffer[BUFFER_SIZE];
uint8_t rxBuffer[BUFFER_SIZE];

    // *** Setup SPI xferConfig ***
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = txBuffer;
    xferConfig.rx_data = rxBuffer;
    xferConfig.length = BUFFER_SIZE;

    // *** Setup MAX7219 for digit decode and scan limit ***
    setUpMax7219BcdCodeB(txBuffer, xferConfig, spiChannelNumber);

    // *** Display 8 digits ***
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_7, CHAR_H);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_6, CHAR_E);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_5, CHAR_L);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_4, CHAR_P);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_3, CHAR_BLANK);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_2, CHAR_BLANK);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_1, DIGIT_0);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_0, DIGIT_1);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, OPERATION_ADDR, NORMAL);
}

// ***************************************************************************
// Function - Write MAX7219 command
// Date     - 2013nov06
// ***************************************************************************
void writeLedCommand(uint8_t txBuffer[BUFFER_SIZE], SSP_DATA_SETUP_Type xferConfig, \
            uint8_t spiChannelNumber, \
            uint8_t commandRegisterAddress, uint8_t command)
{
txBuffer[ADDRESS_INDEX] = commandRegisterAddress;
txBuffer[DATA_INDEX] = command;
    SpiWriteRead050(xferConfig, spiChannelNumber);
}


void writeLedCommand02(uint8_t deviceNumber, uint8_t spiChannelNumber, \
            uint8_t commandRegisterAddress, uint8_t command)
{
uint8_t txBuffer[MAX_BUFFER_SIZE];
uint8_t rxBuffer[MAX_BUFFER_SIZE];

SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = txBuffer;
    xferConfig.rx_data = rxBuffer;
    xferConfig.length =  deviceNumber * 2;
/*
    int i;
    for (i = 0; i < (deviceNumber - 1); i++)
    {
        txBuffer[(i * 2) + ADDRESS_INDEX] = NO_OP_ADDR;
}

    txBuffer[((deviceNumber - 1) * 2) + ADDRESS_INDEX] = commandRegisterAddress;
txBuffer[((deviceNumber - 1) * 2) + DATA_INDEX] = command;
*/

    txBuffer[ADDRESS_INDEX] = commandRegisterAddress;
txBuffer[DATA_INDEX] = command;

    int i;
    for (i = 1; i < deviceNumber; i++)
    {
        txBuffer[(i * 2) + ADDRESS_INDEX] = NO_OP_ADDR;
}

    SpiWriteRead050(xferConfig, spiChannelNumber);
}

// ***************************************************************************
// Function - Display 1 digit
// Date     - 2013nov06
// ***************************************************************************
void displayOneDigit(uint8_t txBuffer[BUFFER_SIZE], SSP_DATA_SETUP_Type xferConfig, \
            uint8_t spiChannelNumber, \
            uint8_t digitAddress, uint8_t digitData)
{
writeLedCommand(txBuffer, xferConfig, spiChannelNumber, digitAddress, digitData);
}

void displayOneDigit02(uint8_t deviceNumber, \
            uint8_t spiChannelNumber, \
            uint8_t digitAddress, uint8_t digitData)
{
writeLedCommand02(deviceNumber, spiChannelNumber, digitAddress, digitData);
}


// ***************************************************************************
// Function - Set up MAX7219 for Code B of all 8 digits, scan 8 digits
// Date     - 2013nov06
// ***************************************************************************
void setUpMax7219BcdCodeB(uint8_t txBuffer[BUFFER_SIZE], SSP_DATA_SETUP_Type xferConfig, \
                  uint8_t spiChannelNumber)
{
writeLedCommand(txBuffer, xferConfig, spiChannelNumber, SHUTDOWN_ADDR, SHUTDOWN_MODE);
    delayTime(ONE_FIFTH_SECOND);
    writeLedCommand(txBuffer, xferConfig, spiChannelNumber, \
               DECODE_MODE_ADDR, CODE_B_ALL_DIGITS);
    writeLedCommand(txBuffer, xferConfig, spiChannelNumber, \
               SCAN_LIMIT_ADDR, DISPLAY_8_DIGITS);
}

// ***************************************************************************
// Function - Set up MAX7219 for no decode all 8 digits, scan 8 digits
// Date     - 2013nov06
// ***************************************************************************
void setUpMax7219NoDecode(uint8_t txBuffer[BUFFER_SIZE], SSP_DATA_SETUP_Type xferConfig, \
                  uint8_t spiChannelNumber)
{
writeLedCommand(txBuffer, xferConfig, spiChannelNumber, SHUTDOWN_ADDR, SHUTDOWN_MODE);
    delayTime(ONE_FIFTH_SECOND);
    writeLedCommand(txBuffer, xferConfig, spiChannelNumber, \
               DECODE_MODE_ADDR, NO_DECODE_ALL_DIGITS);
    writeLedCommand(txBuffer, xferConfig, spiChannelNumber, \
               SCAN_LIMIT_ADDR, DISPLAY_8_DIGITS);
}

void setUpMax7219NoDecode02(uint8_t deviceNumber, uint8_t spiChannelNumber)
{
writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
    delayTime(ONE_FIFTH_SECOND);
    writeLedCommand02(deviceNumber, spiChannelNumber, \
                 DECODE_MODE_ADDR, NO_DECODE_ALL_DIGITS);
    writeLedCommand02(deviceNumber, spiChannelNumber, \
                 SCAN_LIMIT_ADDR, DISPLAY_8_DIGITS);
}

// ***********************************************************************
// End
// ***********************************************************************



.END












No comments:

Post a Comment