Κώδικας:
//-------------------------------------------------------------------------
// MAX7219 LED DIGIT output pin definitions
//-------------------------------------------------------------------------
#define MAX2719_DIN 11
#define MAX2719_CS 10
#define MAX2719_CLK 13
#define ledpin 12
//-------------------------------------------------------------------------
// Key Matrix set up
//-------------------------------------------------------------------------
#include "Keypad.h"
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte msgBuffer[4] = {0x0F, 0x0F, 0x0F, 0x0F}; // keypress message buffer, set to spaces
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//-------------------------------------------------------------------------
// SET UP
//-------------------------------------------------------------------------
void setup()
{
initialise();
// For test mode (all digits on) set to 0x01. Normally we want this off (0x00)
output(0x0f, 0x0);
// Set all digits off initially
output(0x0c, 0x0);
// Set brightness for the digits to high(er) level than default minimum (Intensity Register Format)
output(0x0a, 0x02);
// Set decode mode for ALL digits to output actual ASCII chars rather than just
// individual segments of a digit
output(0x09, 0xFF);
// Blank all the digits with a 'space' character
for (int cnt = 1; cnt < 9; cnt++) {
output(cnt, 0x0F);
}
// Ensure ALL digits are displayed (Scan Limit Register)
output(0x0b, 0x07);
// Turn display ON (boot up = shutdown display)
output(0x0c, 0x01);
}
void loop()
{
char key = keypad.getKey();
if (key) // Check for a valid key.
{
switch (key)
{
case '*':
digitalWrite(ledpin, LOW);
output(0x08, 0x0C);
break;
case '#':
digitalWrite(ledpin, HIGH);
output(0x08, 0x0D);
break;
default:
Serial.println(key);
output(0x08, 0x0F);
// Shift message buffer chars to left (scroll left)
for (int cnt = 0; cnt < sizeof(msgBuffer) - 1; cnt++) {
msgBuffer[cnt] = msgBuffer[cnt + 1];
}
msgBuffer[sizeof(msgBuffer) - 1] = key;
}
// Output all characters in double for loop (one increasing, other decreasing)
for (int cnt = 1, bcnt = 3; cnt < 5; cnt++, bcnt--) {
output(cnt, msgBuffer[bcnt]);
}
}
}
void initialise()
{
// MAX7219
digitalWrite(MAX2719_CS, HIGH);
pinMode(MAX2719_DIN, OUTPUT);
pinMode(MAX2719_CS, OUTPUT);
pinMode(MAX2719_CLK, OUTPUT);
// For KEYPAD matrix
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, LOW);
Serial.begin(9600);
}
void output(byte address, byte data)
{
digitalWrite(MAX2719_CS, LOW);
// Send out two bytes (16 bit)
// parameters: shiftOut(dataPin, clockPin, bitOrder, value)
shiftOut(MAX2719_DIN, MAX2719_CLK, MSBFIRST, address);
shiftOut(MAX2719_DIN, MAX2719_CLK, MSBFIRST, data);
digitalWrite(MAX2719_CS, HIGH);