#include <SPI.h>
#define CS  7

// MAX7219 Control registers
#define DECODE_MODE   9 
#define INTENSITY     0x0A
#define SCAN_LIMIT    0x0B 
#define SHUTDOWN      0x0C
#define DISPLAY_TEST  0x0F

void SendData(uint8_t address, uint8_t value){
  digitalWrite(CS, LOW);  // Start transfer
  SPI.transfer(address);   // Send Address
  SPI.transfer(value);     // Send the value
  digitalWrite(CS, HIGH);  // Finish transfer
}

void setup() {
  pinMode(CS, OUTPUT);  
  SPI.setBitOrder(MSBFIRST);   // Most significant bit first 
  SPI.begin();                 // Start SPI
  SendData(DISPLAY_TEST, 0x01);       // Run test - All LED segments lit.
  delay(2000);
  SendData(DISPLAY_TEST, 0x00);           // Finish test mode.
  SendData(DECODE_MODE, 0x00);            // Disable BCD mode. 
  SendData(INTENSITY, 0x0e);              // Use lowest intensity. 
  SendData(SCAN_LIMIT, 0x0f);             // Scan all digits.
  SendData(SHUTDOWN, 0x01);               // Turn on chip.
}

void loop() {
  SendData(1, B00000000); // Row1, column 3 led on
  SendData(2, B00000010); // Row1, column 3 led on
  SendData(3, B00000010); // Row3, column 5 led on
  SendData(4, B00001110); // Row3, column 5 led on
  SendData(5, B00010010); // Row3, column 5 led on
  SendData(6, B00010010); // Row3, column 5 led on
  SendData(7, B00001110);
  delay(1000);
}