2013-11-08
MAX7219 functions refactoring notes
Now I have written more higher level functions. The main test program becomes very neat and tidy.
setupLedDriver04(spiChannelNumber); // drivers 1, 2 for matrix, 3, 4 for 7-segment
displayMatrix(spiChannelNumber, LED_DRIVER_1, CharBigO);
displayMatrix(spiChannelNumber, LED_DRIVER_2, CharBigK);
display8digits04(spiChannelNumber, LED_DRIVER_3, 1, 2, 3, 4, 5, 6, 7, 8);
display8digits04(spiChannelNumber, LED_DRIVER_4, 8, 7, 6, 5, 4, 3, 2, 1);
.END
// ***********************************************************************
// mled050.h 2013nov08hkt1111
// MAX7219 4-digit 7-segment and 8x8 matrix LED
// ***********************************************************************
#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 DECODE_ALL_8_DIGITS 0xff
#define NO_DECODE_ALL_8_DIGITS 0x00
#define NO_DECODE_ALL_8_ROWS 0x00
#define DISPLAY_8_DIGITS 0x07
#define SCAN_ALL_8_DIGITS 0x07
#define SCAN_ALL_8_ROWS 0x07
// *** Data Constants ***
#define DEVICE_1 1
#define DEVICE_2 2
#define DEVICE_3 3
#define DEVICE_4 4
#define LED_DRIVER_1 1
#define LED_DRIVER_2 2
#define LED_DRIVER_3 3
#define LED_DRIVER_4 4
#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 ADDRESS_INDEX 0
#define DATA_INDEX 1
#define MAX_DEVICE_NUMBER 16
#define MAX_BUFFER_SIZE 32
#define MAX_MATRIX_NUMBER 4
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};
uint8_t CharBigO[8] = {0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00, 0x00, 0x00};
uint8_t CharBigK[8] = {0x41, 0x22, 0x14, 0x08, 0x7F, 0x00, 0x00, 0x00};
uint8_t TriGram0[8] = {0x54, 0x54, 0x54, 0x00, 0x54, 0x54, 0x54, 0x00};
// ***************************************************************************
// Function - Display MAX7219 4-digit 7-segment LEDs and 8x8 matrix LEDs
// Date - 2013nov07
// ***************************************************************************
void max7219MatrixLed05(uint8_t spiChannelNumber)
{
printf("\n\n*** Matrix LED 05 - 2013nov08hkt1052 ***\n\n");
setupLedDriver04(spiChannelNumber); // drivers 1, 2 for matrix, 3, 4 for 7-segment
displayMatrix(spiChannelNumber, LED_DRIVER_1, CharBigO);
displayMatrix(spiChannelNumber, LED_DRIVER_2, CharBigK);
display8digits04(spiChannelNumber, LED_DRIVER_3, 1, 2, 3, 4, 5, 6, 7, 8);
display8digits04(spiChannelNumber, LED_DRIVER_4, 8, 7, 6, 5, 4, 3, 2, 1);
}
void max7219MatrixLed04(uint8_t spiChannelNumber)
{
// *** Print project title ***********************************************
printf("\n\n*** Matrix LED 04 - 2013nov08hkt1041 ***\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
setupLedDriver04(spiChannelNumber); // 2 matrixes, 2 7-segments
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_7, char_K[0]);
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_6, char_K[1]);
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_5, char_K[2]);
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_4, char_K[3]);
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_3, char_K[4]);
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_2, PATTERN_ALL_OFF);
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_1, PATTERN_ALL_OFF);
displayOneDigit02(DEVICE_1, spiChannelNumber, DIGIT_ADDR_0, PATTERN_ALL_OFF);
writeLedDriverCommand02(DEVICE_1, spiChannelNumber, OPERATION_ADDR, NORMAL);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_7, char_O[0]);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_6, char_O[1]);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_5, char_O[2]);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_4, char_O[3]);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_3, char_O[4]);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_2, PATTERN_ALL_OFF);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_1, PATTERN_ALL_OFF);
displayOneDigit02(DEVICE_2, spiChannelNumber, DIGIT_ADDR_0, PATTERN_ALL_OFF);
writeLedDriverCommand02(DEVICE_2, spiChannelNumber, OPERATION_ADDR, NORMAL);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_7, DIGIT_1);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_6, DIGIT_1);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_5, DIGIT_0);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_4, DIGIT_7);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_3, DIGIT_0);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_2, DIGIT_0);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_1, DIGIT_0);
displayOneDigit02(DEVICE_3, spiChannelNumber, DIGIT_ADDR_0, DIGIT_1);
writeLedDriverCommand02(DEVICE_3, spiChannelNumber, OPERATION_ADDR, NORMAL);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_7, DIGIT_2);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_6, DIGIT_2);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_5, DIGIT_3);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_4, DIGIT_4);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_3, DIGIT_5);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_2, DIGIT_6);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_1, DIGIT_7);
displayOneDigit02(DEVICE_4, spiChannelNumber, DIGIT_ADDR_0, DIGIT_8);
writeLedDriverCommand02(DEVICE_4, spiChannelNumber, OPERATION_ADDR, NORMAL);
}
void setupLedDriver04(spiChannelNumber)
{
uint8_t i;
for (i = 1; i < 3; i++)
{
writeLedCommand02(i, spiChannelNumber, DECODE_MODE_ADDR, NO_DECODE_ALL_8_ROWS);
writeLedCommand02(i, spiChannelNumber, SCAN_LIMIT_ADDR, SCAN_ALL_8_ROWS);
writeLedCommand02(i, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
}
for (i = 3; i < 5; i++)
{
writeLedCommand02(i, spiChannelNumber, DECODE_MODE_ADDR, DECODE_ALL_8_DIGITS);
writeLedCommand02(i, spiChannelNumber, SCAN_LIMIT_ADDR, SCAN_ALL_8_DIGITS);
writeLedCommand02(i, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
}
}
void setupDecodeMode04(uint8_t spiChannelNumber, uint8_t matrixNumber, uint8_t decodeMode)
{
writeLedCommand02(matrixNumber, spiChannelNumber, DECODE_MODE_ADDR, decodeMode);
}
void setupScanLimit04(uint8_t spiChannelNumber, uint8_t matrixNumber, uint8_t scanLimit)
{
writeLedCommand02(matrixNumber, spiChannelNumber, SCAN_LIMIT_ADDR, scanLimit);
}
void shutDownLedDriver(uint8_t spiChannelNumber, uint8_t driverNumber)
{
writeLedDriverCommand02(driverNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
}
void startUpLedDriver(uint8_t spiChannelNumber, uint8_t driverNumber)
{
writeLedDriverCommand02(driverNumber, spiChannelNumber, OPERATION_ADDR, NORMAL);
}
void displayOneDigit04(uint8_t spiChannelNumber, uint8_t ledDriverNumber, \
uint8_t digitAddress, uint8_t digitData)
{
writeLedCommand02(ledDriverNumber, spiChannelNumber, digitAddress, digitData);
}
void writeLedDriverCommand02(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;
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);
}
void displayMatrix(uint8_t spiChannelNumber, uint8_t ledDriverNumber, uint8_t matrixPattern[8])
{
shutDownLedDriver(spiChannelNumber, ledDriverNumber);
uint8_t columnNumber;
uint8_t digitAddress;
for (columnNumber = 0; columnNumber < 8; columnNumber++)
{
digitAddress = columnNumber + 1;
displayOneDigit04(spiChannelNumber, ledDriverNumber, digitAddress, matrixPattern[7 - columnNumber]);
}
startUpLedDriver(spiChannelNumber, ledDriverNumber);
}
void display8digits04(uint8_t spiChannelNumber, uint8_t ledDriverNumber, \
uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4, \
uint8_t digit5, uint8_t digit6, uint8_t digit7, uint8_t digit8)
{
shutDownLedDriver(spiChannelNumber, ledDriverNumber);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_7, digit1);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_6, digit2);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_5, digit3);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_4, digit4);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_3, digit5);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_2, digit6);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_1, digit7);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_0, digit8);
startUpLedDriver(spiChannelNumber, ledDriverNumber);
}
// ***********************************************************************
// End
// ***********************************************************************
// *** Old functions - 2013nov08 ***
void max7219MatrixLed03(uint8_t spiChannelNumber)
{
// *** Print project title ***********************************************
printf("\n\n*** Matrix LED 03 - 2013nov07hkt1203 ***\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 no 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);
deviceNumber = 3;
// *** Setup MAX7219 for digit decode and scan limit ***
setUpMax7219BcdCodeB03(deviceNumber, spiChannelNumber);
// *** Display 8 digits ***
writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
// *** Display 8 digits ***
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_7, DIGIT_2);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_6, DIGIT_0);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_5, DIGIT_1);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_4, DIGIT_3);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_3, DIGIT_1);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_2, DIGIT_1);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_1, DIGIT_0);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_0, DIGIT_7);
writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, NORMAL);
deviceNumber = 4;
// *** Setup MAX7219 for digit decode and scan limit ***
setUpMax7219BcdCodeB03(deviceNumber, spiChannelNumber);
// *** Display 8 digits ***
writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
// *** Display 8 digits ***
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_7, DIGIT_1);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_6, DIGIT_2);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_5, DIGIT_3);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_4, DIGIT_4);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_3, DIGIT_5);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_2, DIGIT_6);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_1, DIGIT_7);
displayOneDigit02(deviceNumber, spiChannelNumber, DIGIT_ADDR_0, DIGIT_8);
writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, NORMAL);
}
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;
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);
writeLedCommand(txBuffer, xferConfig, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
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);
}
void setUpMax7219BcdCodeB03(uint8_t deviceNumber, uint8_t spiChannelNumber)
{
writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
delayTime(ONE_FIFTH_SECOND);
writeLedCommand02(deviceNumber, spiChannelNumber, \
DECODE_MODE_ADDR, CODE_B_ALL_DIGITS);
writeLedCommand02(deviceNumber, 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, OPERATION_ADDR, SHUT_DOWN);
//writeLedCommand02(deviceNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
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
// ***********************************************************************
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment