#include <SPI.h>
#define GSM_1CS 7 //10-???
#define MAX7219_1CS 8
#define MAX7219_2CS 9
/*
#define OP_NOOP 0x00
#define OP_DIGIT0 0x01
#define OP_DIGIT1 0x02
#define OP_DIGIT2 0x03
#define OP_DIGIT3 0x04
#define OP_DIGIT4 0x05
#define OP_DIGIT5 0x06
#define OP_DIGIT6 0x07
#define OP_DIGIT7 0x08
*/
#define OP_DECODEMODE 0x09
#define OP_INTENSITY 0x0a
#define OP_SCANLIMIT 0x0b
#define OP_SHUTDOWN 0x0c
#define OP_DISPLAYTEST 0x0f
/*
void shift(byte send_to_address, byte send_this_data)
{
digitalWrite(MAX7219_Chip_Select, LOW);
shiftOut(MAX7219_Data_IN, MAX7219_Clock, MSBFIRST, send_to_address);
shiftOut(MAX7219_Data_IN, MAX7219_Clock, MSBFIRST, send_this_data);
digitalWrite(MAX7219_Chip_Select, HIGH);
}
*/
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT); //CS for SPI, Is Need???
digitalWrite(10, HIGH);
pinMode(GSM_1CS, OUTPUT);
digitalWrite(GSM_1CS, HIGH);
pinMode(MAX7219_1CS, OUTPUT);
digitalWrite(MAX7219_1CS, HIGH);
pinMode(MAX7219_2CS, OUTPUT);
digitalWrite(MAX7219_1CS, HIGH);
// start the SPI library:
//Set SPI Speed 125kHz!!!
SPI.setClockDivider(SPI_CLOCK_DIV128);
SPI.begin();
delay(200);
//Setup of MAX7219 chip, Display 1
writeRegister(MAX7219_1CS, OP_DISPLAYTEST, 0x00); //display test register - test mode off
writeRegister(MAX7219_1CS, OP_SHUTDOWN , 0x01); //shutdown register - normal operation
writeRegister(MAX7219_1CS, OP_SCANLIMIT , 0x07); //scan limit register - display digits 0 thru 7
writeRegister(MAX7219_1CS, OP_INTENSITY , 0x0f); //intensity register - max brightness
writeRegister(MAX7219_1CS, OP_DECODEMODE , 0xff); //decode mode register - CodeB decode all digits
//Setup of MAX7219 chip, Display 2
writeRegister(MAX7219_2CS, OP_DISPLAYTEST, 0x00); //display test register - test mode off
writeRegister(MAX7219_2CS, OP_SHUTDOWN , 0x01); //shutdown register - normal operation
writeRegister(MAX7219_2CS, OP_SCANLIMIT , 0x07); //scan limit register - display digits 0 thru 7
writeRegister(MAX7219_2CS, OP_INTENSITY , 0x0f); //intensity register - max brightness
writeRegister(MAX7219_2CS, OP_DECODEMODE , 0xff); //decode mode register - CodeB decode all digits
}
void loop() {
// Display Test on
Serial.println("Display Test");
writeRegister(MAX7219_1CS, OP_INTENSITY, 0);
writeRegister(MAX7219_2CS, OP_INTENSITY, 0);
writeRegister(MAX7219_1CS, OP_DISPLAYTEST , 0x01); // Display test on
writeRegister(MAX7219_2CS, OP_DISPLAYTEST , 0x01); // Display test on
//delay(2000);
for (int i = 0; i < 16; i++) {
writeRegister(MAX7219_1CS, OP_INTENSITY, i);
writeRegister(MAX7219_2CS, OP_INTENSITY, i);
delay(100);
}
for (int i = 0; i < 16; i++) {
writeRegister(MAX7219_1CS, OP_INTENSITY, 15-i);
writeRegister(MAX7219_2CS, OP_INTENSITY, 15-i);
delay(100);
}
writeRegister(MAX7219_1CS, OP_DISPLAYTEST , 0x00); // Display test off
writeRegister(MAX7219_2CS, OP_DISPLAYTEST , 0x00); // Display test off
delay(1000);
writeRegister(MAX7219_1CS, OP_INTENSITY, 10);
writeRegister(MAX7219_2CS, OP_INTENSITY, 10);
writeRegister(MAX7219_1CS, OP_DECODEMODE , 0xff); //decode mode register - CodeB decode all digits
writeRegister(MAX7219_2CS, OP_DECODEMODE , 0xff); //decode mode register - CodeB decode all digits
byte OutB1[4] = {0x00, 0x00, 0x00, 0x80};
writeDispFlow(MAX7219_1CS, OutB1);
/*
writeRegister(MAX7219_1CS, OP_DIGIT0, 0x00);
writeRegister(MAX7219_1CS, OP_DIGIT1, 0x00);
writeRegister(MAX7219_1CS, OP_DIGIT2, 0x00);
writeRegister(MAX7219_1CS, OP_DIGIT3, 0x80);
*/
byte OutB2[4] = {0x0f, 0x00, 0x80, 0x00};
writeDispO2(MAX7219_1CS, OutB2);
/*
writeRegister(MAX7219_1CS, OP_DIGIT4, 0x0f);
writeRegister(MAX7219_1CS, OP_DIGIT5, 0x00);
writeRegister(MAX7219_1CS, OP_DIGIT6, 0x80);
writeRegister(MAX7219_1CS, OP_DIGIT7, 0x00);
*/
byte OutB3[4] = {0x00, 0x00, 0x80, 0x00};
writeDispFlow(MAX7219_2CS, OutB3);
/*
writeRegister(MAX7219_2CS, OP_DIGIT0, 0x00);
writeRegister(MAX7219_2CS, OP_DIGIT1, 0x00);
writeRegister(MAX7219_2CS, OP_DIGIT2, 0x80);
writeRegister(MAX7219_2CS, OP_DIGIT3, 0x00);
*/
byte OutB4[4] = {0x0f, 0x00, 0x80, 0x00};
writeDispO2(MAX7219_2CS, OutB4);
/*
writeRegister(MAX7219_2CS, OP_DIGIT4, 0x0f);
writeRegister(MAX7219_2CS, OP_DIGIT5, 0x00);
writeRegister(MAX7219_2CS, OP_DIGIT6, 0x80);
writeRegister(MAX7219_2CS, OP_DIGIT7, 0x00);
*/
delay(10000);
/*
// Display Test on
Serial.println("Shutdown");
shift(OP_SHUTDOWN , 0x00); // Display test on
delay(2000);
shift(OP_SHUTDOWN , 0x01); // Display test off
delay(1000);
Serial.println("Scan Limit test (3)");
shift(OP_DECODEMODE , 0xff); //decode mode register - CodeB decode all digits
shift(OP_DIGIT7, 0x08); //digit 7 (leftmost digit) data
shift(OP_DIGIT6, 0x08);
shift(OP_DIGIT5, 0x08);
shift(OP_DIGIT4, 0x08);
shift(OP_DIGIT3, 0x08);
shift(OP_DIGIT2, 0x08);
shift(OP_DIGIT1, 0x08);
shift(OP_DIGIT0, 0x08); //digit 0 (rightmost digit) data
shift(OP_SCANLIMIT, 0x03);
delay(1000);
Serial.println("Scan Limit test (7)");
shift(OP_SCANLIMIT, 0x07);
delay(1000);
Serial.println("Intensity test");
for (int i = 0; i < 16; i++) {
shift(OP_INTENSITY, 15-i);
delay(100);
}
for (int i = 0; i < 16; i++) {
shift(OP_INTENSITY, i);
delay(100);
}
for (int i = 0; i < 16; i++) {
shift(OP_INTENSITY, 15-i);
delay(100);
}
for (int i = 0; i < 16; i++) {
shift(OP_INTENSITY, i);
delay(100);
}
for (int i = 0; i < 16; i++) {
shift(OP_INTENSITY, 15-i);
delay(100);
}
for (int i = 0; i < 16; i++) {
shift(OP_INTENSITY, i);
delay(100);
}
shift(OP_INTENSITY, 0xf);
//Data transfer
Serial.println("BCD with decimal point");
shift(OP_DECODEMODE , 0xff); //decode mode register - CodeB decode all digits
shift(OP_DIGIT7, 0x80); //digit 7 (leftmost digit) data
shift(OP_DIGIT6, 0x81);
shift(OP_DIGIT5, 0x82);
shift(OP_DIGIT4, 0x83);
shift(OP_DIGIT3, 0x84);
shift(OP_DIGIT2, 0x85);
shift(OP_DIGIT1, 0x86);
shift(OP_DIGIT0, 0x87); //digit 0 (rightmost digit) data
delay(1000);
Serial.println("BCD without decimal point");
shift(OP_DIGIT7, 0x08); //digit 7 (leftmost digit) data
shift(OP_DIGIT6, 0x09);
shift(OP_DIGIT5, 0x0a); //= '-'
shift(OP_DIGIT4, 0x0b); //= 'E'
shift(OP_DIGIT3, 0x0c); //= 'H'
shift(OP_DIGIT2, 0x0d); //= 'L'
shift(OP_DIGIT1, 0x0e); //= 'P'
shift(OP_DIGIT0, 0x0f); //digit 0 (rightmost digit) data //= ' '
delay(1000);
shift(OP_DECODEMODE , 0x00); //decode mode register - CodeB decode all digits
Serial.println("Cleared segments");
shift(OP_DIGIT7, 0x00); //digit 7 (leftmost digit) data
shift(OP_DIGIT6, 0x00);
shift(OP_DIGIT5, 0x00);
shift(OP_DIGIT4, 0x00);
shift(OP_DIGIT3, 0x00);
shift(OP_DIGIT2, 0x00);
shift(OP_DIGIT1, 0x00);
shift(OP_DIGIT0, 0x00); //digit 0 (rightmost digit) data
delay(1000);
Serial.println("Segment test");
uint8_t x = 0x80;
for (int i = 0; i < 8; i++) {
shift(OP_DIGIT7, x); //digit 7 (leftmost digit) data
shift(OP_DIGIT6, x);
shift(OP_DIGIT5, x);
shift(OP_DIGIT4, x);
shift(OP_DIGIT3, x);
shift(OP_DIGIT2, x);
shift(OP_DIGIT1, x);
shift(OP_DIGIT0, x); //digit 0 (rightmost digit) data
x = x >> 1;
delay(500);
}
*/
}
//Sends a write command to SPI
void writeRegister(byte thisDisplay, byte thisRegister, byte thisValue) {
// take the chip select low to select the device:
digitalWrite(thisDisplay, LOW);
//SPI.transfer(dataToSend); //Send register location
SPI.transfer(thisRegister); //Send register location
SPI.transfer(thisValue); //Send value to record into register
// take the chip select high to de-select:
digitalWrite(thisDisplay, HIGH);
}
//Write to Display 1/1 - 'Xn' or 'O2'
void writeDispO2(byte thisDisplay, byte Data[4]) {
for (int i = 5; i < 9; i++) {
writeRegister(thisDisplay, i, Data[i-5]);
}
}
//Write to Display 1/2 - 'Flow' or 'Volume'
void writeDispFlow(byte thisDisplay, byte Data[4]) {
for (int i = 1; i < 5; i++) {
writeRegister(thisDisplay, i, Data[i-1]);
}
}