2013-10-27

Max7912 4-digit 7-segment LED function refactoring notes

// ***********************************************************************
// sled050.h 2013oct27hk1425
// ***********************************************************************

#include "spi050.h"

void max7219v03(uint8_t spiChannelNumber) // 2013oct23hkt1315
{
    // *** Print project title ***********************************************
printf("*** MAX7219 Test - 2013oct27hk1357 ***\n\n");

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

uint8_t digitControlRegisterBuffer[BUFFER_SIZE];
uint8_t dummyReceiveBuffer[BUFFER_SIZE];

setupSpi051(spiChannelNumber);

    // Setup xferConfig
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = (void*)digitControlRegisterBuffer;
    xferConfig.rx_data = (void*)dummyReceiveBuffer;
    xferConfig.length = BUFFER_SIZE;

// *** Setup Max7219 digit and control ***********************************
    // Register addresses
#define NO_OP_ADDR 0x00
#define DIGIT_0_ADDR 0x01
    #define DIGIT_1_ADDR 0x02
#define DIGIT_2_ADDR 0x03
#define DIGIT_3_ADDR 0x04
#define DIGIT_4_ADDR 0x05
#define DIGIT_5_ADDR 0x06
#define DIGIT_6_ADDR 0x07
#define DIGIT_7_ADDR 0x08
#define DECODE_MODE_ADDR 0x09
#define INTENSITY       0x0a
#define SCAN_LIMIT_ADDR 0x0b
#define SHUTDOWN_ADDR   0x0c
#define DISPLAY_TEST     0x0f

// Control byte
#define SHUTDOWN_MODE          0x00
#define NORMAL_OPERATION_MODE  0x01
    #define CODE_B_DECODE_ALL_BITS 0xff
    #define DISPLAY_8_DIGITS       0x07

#define DIGIT_0_DATA 0
#define DIGIT_1_DATA 1
    #define DIGIT_2_DATA 2
    #define DIGIT_3_DATA 3
    #define DIGIT_4_DATA 4
#define DIGIT_5_DATA 5
#define DIGIT_6_DATA 6
#define DIGIT_7_DATA 7
#define DIGIT_8_DATA 8
#define DIGIT_9_DATA 9

    while (1)
        {
    writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR, SHUTDOWN_MODE);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        delayTime(ONE_SECOND);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DECODE_MODE_ADDR, \
                                       CODE_B_DECODE_ALL_BITS);
        SpiWriteRead050(xferConfig, spiChannelNumber);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SCAN_LIMIT_ADDR, DISPLAY_8_DIGITS);
        SpiWriteRead050(xferConfig, spiChannelNumber);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_0_ADDR, DIGIT_1_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_1_ADDR, DIGIT_1_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_2_ADDR, DIGIT_2_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_3_ADDR, DIGIT_3_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_4_ADDR, DIGIT_4_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_5_ADDR, DIGIT_5_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_6_ADDR, DIGIT_6_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_7_ADDR, DIGIT_8_DATA);
        SpiWriteRead050(xferConfig, spiChannelNumber);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR, \
                                       NORMAL_OPERATION_MODE);
        SpiWriteRead050(xferConfig, spiChannelNumber);
        delayTime(ONE_SECOND);
        }
}

void writeSegmentLedCommandToBuffer(uint8_t digitControlRegisterBuffer[BUFFER_SIZE], \
                           uint8_t commandRegisterAddress, uint8_t command)
{
digitControlRegisterBuffer[ADDRESS_INDEX] = commandRegisterAddress;
digitControlRegisterBuffer[DATA_INDEX] = command;
}

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



// ***********************************************************************
// spi050.h 2013oct26hk1611
// ***********************************************************************

#include "gpio050.h"
#include "led050.h"
#include "lpc11xx_ssp.h"
#include "semihosting.h"
#include "stdio.h"
#include "config050.h"

#ifndef SPI_HEADER_SEEN
#define SPI_HEADER_SEEN

// *** SPI Functions ***

#define SPI_CHANNEL_0 0
#define SPI_CHANNEL_1 1

void setupSpi051(int spiChannelNumber)
{
if (spiChannelNumber == 0)
{
// Enable SSP0 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP0, ENABLE);

// Reset SSP0 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, DISABLE);
SYSCON_SetSPI0ClockDiv(10);

// Assign GPIO pins for SPI
SSP_SSP0PinsInit(SCK0_PIO0_6, DISABLE); // Select P06, disable

// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP0, &SSP_ConfigStruct);

// Enable SSP peripheral
SSP_Cmd(LPC_SSP0, ENABLE);

// Setup Ssel0
setupGpioPinOutputLow050(PortPinArraySsel0);
}
else // (if spiChannelNumber == 1)
{
// Enable SSP1 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP1, ENABLE);

// Reset SSP1 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, DISABLE);
SYSCON_SetSPI1ClockDiv(10);

// No need to assign GPIO pins for SPI1, just disable
SSP_SSP1PinsInit(DISABLE); // disable SSEL

// IOCON_SetPinFunc(IOCON_PIO2_2, PIO2_2_FUN_MISO1);
// IOCON_SetPinFunc(IOCON_PIO2_3, PIO2_3_FUN_PIO_MOSI1);
// IOCON_SetPinFunc(IOCON_PIO2_1, PIO2_1_FUN_SCK1);
// if(useSSEL == ENABLE) {
        //    IOCON_SetPinFunc(IOCON_PIO2_0, PIO2_0_FUN_SSEL1);

// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP1, &SSP_ConfigStruct);

// Enable SSP1 peripheral
SSP_Cmd(LPC_SSP1, ENABLE);

// Setup Ssel1
setupGpioPinOutputLow050(PortPinArraySsel1);
}
}

void setupXferConfig(SSP_DATA_SETUP_Type *xferConfigPointer, uint8_t digitControlRegisterBuffer[], \
            uint8_t dummyReceiveBuffer[], uint8_t bufferSize)
{
    xferConfigPointer->tx_data = digitControlRegisterBuffer;
    xferConfigPointer->rx_data = dummyReceiveBuffer;
    xferConfigPointer->length = bufferSize;
 }

void SpiWriteRead050(SSP_DATA_SETUP_Type xferConfig, int spiChannelNumber)
{
if (spiChannelNumber == 0)
{
setGpioDataPinLow01(PortPinArraySsel0);
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(PortPinArraySsel0);
}
else // (spiChannelNumber == 1)
{
setGpioDataPinLow01(PortPinArraySsel1);
SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(PortPinArraySsel1);
}
}

#endif /* SPI_HEADER_SEEN */

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




.END

No comments:

Post a Comment