A: Let us carry out the following tutorial to operate the setup of Fig-1 using the SPI.h Library first before we switch over to LedControl.h Library in Section-B.
Figure-1:
1. Connect the display unit as per Fig-1.
2. Upload the following sketch on UNO and check that 1 2 3 have appeared on DP0 DP1 DP2 positions of the display unit of Fig-1.
#include<SPI.h>
#define sdReg 0x0C //Shutdown Register = 01; Normal Mode
#define dmReg 0x09 //Decode Mode Register=00;cc-code on no-decode format
#define inReg 0x0A //Intensity Register = 01; nominal intensity
#define slReg 0x0B //Scan Limit Register = 07; eight digit to scan
#define LOAD 10
byte registerAddress[] = {sdReg, dmReg, inReg, slReg};//0x0C, 0x09, 0x0A, 0x0B};
byte registerData[] = {0x01, 0x00, 0x01, 0x07}; //data for above registers
byte dataArray[50]; //to hold cc-codes for the digits to be displayed (no-decode format)
byte digitAddress[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};//DP0-DP7
byte lupTable[] = {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70,
0x7F, 0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47
}; //0, 1, ...., E, F (no-decode format: p, a, b, c, d, e, f, g
void setup()
{
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV128); //TX rate = 16MHz/128 = 125 kbit
//------------------------------------------------
digitalWrite(LOAD, LOW); //Low at LOAD pin
//----intialize MAX7219---------------------------------------
for (int i = 0; i < 4; i++)
{
SPI.transfer(registerAddress[i]);
SPI.transfer(registerData[i]);
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH); //assert LH/LL on LOAD p
digitalWrite(LOAD, LOW);
}
//---- example computation----------------------------------
byte z = 0x10 + 0x02; //z = 12
dataArray[0] = lupTable[z >> 4]; // 1 appears on DP0 position
dataArray[1] = lupTable[z & 0x0F]; // 2 appears on DP1 position
dataArray[2] = 0x79; //to show 3 on DP position
//-- show result of above computaion on DP0, DP1 and DP2 of display uniy-------
for (int i = 0; i < 8; i++)
{
SPI.transfer(digitAddress[i]); //DPX position
SPI.transfer(dataArray[i]); //shows 2 on DP0-position
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH); //assert LH/LL on LOAD pin
digitalWrite(LOAD, LOW);
}
}
void loop()
{
}
3. Connect 4x4 Keypad with UNO as per Fig-1.
4. Upload the following sketch.
#include<SPI.h>
#include <Keypad.h>
#define sdReg 0x0C //Shutdown Register = 01; Normal Mode
#define dmReg 0x09 //Decode Mode Register=00;cc-code on no-decode format
#define inReg 0x0A //Intensity Register = 01; nominal intensity
#define slReg 0x0B //Scan Limit Register = 07; eight digit to scan
#define LOAD 10
byte registerAddress[] = {sdReg, dmReg, inReg, slReg};//0x0C, 0x09, 0x0A, 0x0B};
byte registerData[] = {0x01, 0x00, 0x01, 0x07}; //data for above registers
byte dataArray[8]; //to hold cc-codes for the digits to be displayed (no-decode format)
byte digitAddress[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};//DP0-DP7
byte lupTable[] = {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70,
0x7F, 0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47
}; //0, 1, ...., E, F (no-decode format: p, a, b, c, d, e, f, g
//------------------------------------------------------------------------
char keyAscii[8];// = ""; //holds ASCII codes of a pressed down key
//byte ccCode[8]; //holds cc = {0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E}; //
int i = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad; R1R2R3R4
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//-------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV128); //TX rate = 16MHz/128 = 125 kbit
//------------------------------------------------
digitalWrite(LOAD, LOW); //Low at LOAD pin
//----intialize MAX7219---------------------------------------
for (int i = 0; i < 4; i++)
{
SPI.transfer(registerAddress[i]);
SPI.transfer(registerData[i]);
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH); //assert LH/LL on LOAD p
digitalWrite(LOAD, LOW);
}
//---- blank DP0 DP1 DP2 of display unit ----------------------------------
dataArray[0] = 0x00;//// blank appears on DP0 position
dataArray[1] = 0x00;//blankappears on DP1 position
dataArray[2] = 0x00; //blank DP2 position
updateDisplay();
}
void loop()
{
byte myKey = customKeypad.getKey();
if (myKey != 0x00)
{
Serial.println(myKey, HEX);
//keyAscii[i] = customKey;
dataArray[i] = lupTable[(myKey & 0x0F)]; //cc-code for the pressed digit of Keypad is saved
Serial.println(dataArray[i], HEX);
updateDisplay();
i++;
//-------------------------------
}
}
void updateDisplay()
{
for (int i = 0; i < 8; i++)
{
SPI.transfer(digitAddress[i]); //DPX position
SPI.transfer(dataArray[i]); //
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH); //assert LH/LL on LOAD pin
digitalWrite(LOAD, LOW);
}
}
(1) Check that the display is blank (at least first 3-digit).
(2) Press 1 on Keypad and check that 1 has appeared on DP0 position of display.
(3) Press 4 on Keypad and check that 4 has appeared on DP1 position of display unit.
(4) Press 7 on Keypad and check that 7 has appeared on DP2 position of display unit.
B: Let us carry out the following tutorial to operate the setup of Fig-1 using the LedControl.h Library.
1. Upload the following sketch.
#include <Keypad.h>
#include "LedControl.h"
LedControl lc = LedControl(11,13,10,1); //DIN, SCK, LOAD,1xMAX7219
byte posiTion = 0; //starts from DP0 position
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};//D7, D6, D5, D4}; //connect from D0 to D7 (skip gnd and 3v) with D0 the far rigth connector on the keypad and D7 the far left.
byte colPins[COLS] = {6, 7, 8, 9};//D3, D2, D1, D0};
Keypad mykeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
lc.shutdown(0, false); // Enable display
lc.setIntensity(0, 10); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register
}
void loop()
{
char key = mykeypad.getKey();
if (key != NO_KEY)
{
Serial.println(key);
lc.setChar(0, posiTion, key, false); //displayBlank = 1, digitPosition, digit to show, no-decimal point
posiTion++; //next digit will be shown at DP1 and so on
}
}
2. Check that the display is blank.
3. Press 1 on Keypad and check that 1 has appeared on DP0 position of display.
4. Press 4 on Keypad and check that 4 has appeared on DP1 position of display unit.
5. Press 7 on Keypad and check that 7 has appeared on DP2 position of display unit.