

// Define Soft-SPI connections for LED display
#define CS_Pin       GP0_bit  //max 12pin  Load(CS)
#define MOSI_Pin     GP1_bit  //max  1pin  Din
#define CLK_Pin      GP5_bit  //max 13pin  CLK



void SPI_Write_Byte(unsigned short num){
 unsigned short t, Mask, Flag;
 CLK_Pin = 0;
 Mask = 128;
 for (t=0; t<8; t++){
  Flag = num & Mask;
  if(Flag == 0) MOSI_Pin = 0;
  else MOSI_Pin = 1;
  CLK_Pin = 1;
  CLK_Pin = 0;
  Mask = Mask >> 1;
 }
}

void MAX7219_INIT() {
  // Disable Shutdown mode
  CS_Pin = 0;              // CS pin is pulled LOW
  SPI_Write_Byte(0x0C);    // Select Shutdown register
  SPI_Write_Byte(0x01);    // Set D0 bit to return to normal operation
  CS_Pin = 1;              // CS pin is pulled HIGH

  // Set BCD decode mode for digits DIG1-DIG3, and DIG5-DIG7
  CS_Pin = 0;              // CS pin is pulled LOW
  SPI_Write_Byte(0x09);    // Select Decode Mode register
  SPI_Write_Byte(0b11101110);    // Disable BCD mode for digits DIG0-DIG4
  CS_Pin = 1;              // CS pin is pulled HIGH

  // Set display brighness
  CS_Pin = 0;              // CS pin is pulled LOW
  SPI_Write_Byte(0x0A);    // Select Intensity register
  SPI_Write_Byte(brightness);    // Set brightness
  CS_Pin = 1;              // CS pin is pulled HIGH

   // Set display refresh
  CS_Pin = 0;              // CS pin is pulled LOW
  SPI_Write_Byte(0x0B);    // Select Scan-Limit register
  SPI_Write_Byte(0x07);    // Select digits DIG0-DIG3
  CS_Pin = 1;              // CS pin is pulled HIGH

 // Enable Display-Test
  CS_Pin = 0;              // CS pin is pulled LOW
  SPI_Write_Byte(0x0F);    // Select Display-Test register
  SPI_Write_Byte(0x01);    // Enable Display-Test
  CS_Pin = 1;              // CS pin is pulled HIGH

  Delay_ms(1000);
 // Disable Display-Test
  CS_Pin = 0;              // CS pin is pulled LOW
  SPI_Write_Byte(0x0F);    // Select Display-Test register
  SPI_Write_Byte(0x00);    // Disable Display-Test
  CS_Pin = 1;              // CS pin is pulled HIGH

}

 void Display_Value(unsigned int j, unsigned short k){
  CS_Pin = 0;                    // CS pin is pulled LOW
  SPI_Write_Byte(k);             // Send thousands digit
  SPI_Write_Byte((j/100)%10);
  CS_Pin = 1;                    // CS pin is pulled HIGH

  CS_Pin = 0;                    // CS pin is pulled LOW
  SPI_Write_Byte(k-1);             // Send hundreds digit
  SPI_Write_Byte(((j/10)%10) | 0x80);
  CS_Pin = 1;                    // CS pin is pulled HIGH

  CS_Pin = 0;                    // CS pin is pulled LOW
  SPI_Write_Byte(k-2);             // Send tens digit
  SPI_Write_Byte(j%10);
  CS_Pin = 1;                    // CS pin is pulled HIGH

  CS_Pin = 0;                     // CS pin is pulled LOW
  SPI_Write_Byte(k-3);            // Send ones digit
  if(k == 4) SPI_Write_Byte(0x47);  // Display 'F'
  if(k == 8) SPI_Write_Byte(0x67);  // Display P
  CS_Pin = 1;                    // CS pin is pulled HIGH
 }




void main() {
  