#include "MAX7219.h"
#define MAX7219_SPI SPI1 //interface definition
#define MAX7219_MaxCountValue 12 //maximum vaule of chips in serial to scan
/* You can manualy assign the value of chips in serialy connected here*/
unsigned char ChipsInSerial=0; //otherwise search procedure will be needed and loop must be closed
unsigned char DigitsPerChip=8;
unsigned char MAX7219_WriteReg (unsigned char Reg, unsigned char Data);
unsigned char MAX7219_WriteReg (unsigned char Reg, unsigned char Data)
{
unsigned char cnt=0;
unsigned char tmp=0;
if (ChipsInSerial==0)
{
PIN_OFF(CS);
for (tmp=0;tmp!=MAX7219_MaxCountValue;tmp++) SPI_SendByte(MAX7219_SPI, MAX7219_NoOperation);
PIN_ON(CS);
PIN_OFF(CS);
tmp=0;
while ((tmp!=Reg)&(cnt<MAX7219_MaxCountValue))
{
tmp=SPI_ReadByte(MAX7219_SPI, Reg);
SPI_SendByte(MAX7219_SPI, Data);
cnt++;
}
PIN_ON(CS);
ChipsInSerial = cnt-1;
}
else
{
PIN_OFF(CS);
for (tmp=0;tmp!=ChipsInSerial;tmp++)
{
SPI_SendByte(MAX7219_SPI, Reg);
SPI_SendByte(MAX7219_SPI, Data);
}
PIN_ON(CS);
}
return ChipsInSerial;
}
unsigned char MAX7219_Init (MAX7219Init_TypeDef * MAX7219_InitStruct)
{
MAX7219_DisplayTestVoid(0);
DigitsPerChip = MAX7219_InitStruct->ScanLimit+1;
MAX7219_WriteReg(MAX7219_DecodeMode, MAX7219_InitStruct->DecodeMode);
MAX7219_WriteReg(MAX7219_Intensity, MAX7219_InitStruct->Intensity);
MAX7219_WriteReg(MAX7219_ScanLimit, MAX7219_InitStruct->ScanLimit);
return MAX7219_WriteReg(MAX7219_ShutDown, MAX7219_InitStruct->ShutDown);
}
void MAX7219_StructInit (MAX7219Init_TypeDef * MAX7219_InitStruct, unsigned char DigitsPerChip)
{
MAX7219_InitStruct->DecodeMode=0xFF; //code B 0..7
MAX7219_InitStruct->Intensity=8; //0...16
MAX7219_InitStruct->ScanLimit=(DigitsPerChip-1);
MAX7219_InitStruct->ShutDown=1;
}
void MAX7219_DrawDigitSerial (unsigned char * Digit)
{
unsigned char ChipCNT;
unsigned char DigitCNT = DigitsPerChip;
for (DigitCNT=0; DigitCNT!=DigitsPerChip; DigitCNT++)
{
PIN_OFF(CS);
for (ChipCNT=0; ChipCNT!=ChipsInSerial; ChipCNT++)
{
SPI_SendByte(MAX7219_SPI, (DigitCNT+1));
SPI_SendByte(MAX7219_SPI, Digit[(ChipsInSerial-ChipCNT-1)*DigitsPerChip+DigitCNT]);
}
PIN_ON(CS);
}
}
void MAX7219_DisplayTestVoid (unsigned char Enable)
{
MAX7219_WriteReg(MAX7219_DisplayTest, Enable);
}
void MAX7219_SetIntensity (unsigned char Intensity)
{
MAX7219_WriteReg(MAX7219_Intensity, Intensity&0x0F);
}
void MAX7219_Power (unsigned char PowerState)
{
MAX7219_WriteReg(MAX7219_ShutDown, PowerState);
}
void MAX7219_DrawCharSerial (unsigned char CHAR, const unsigned long (*FONT)[])
{
unsigned char DigitArray[8];
CHAR=CHAR<<1;
DigitArray[0]=((*FONT)[CHAR]&0xFF000000)>>24;
DigitArray[1]=((*FONT)[CHAR]&0x00FF0000)>>16;
DigitArray[2]=((*FONT)[CHAR]&0x0000FF00)>>8;
DigitArray[3]=((*FONT)[CHAR]&0x000000FF);
DigitArray[4]=((*FONT+1)[CHAR]&0xFF000000)>>24;
DigitArray[5]=((*FONT+1)[CHAR]&0x00FF0000)>>16;
DigitArray[6]=((*FONT+1)[CHAR]&0x0000FF00)>>8;
DigitArray[7]=((*FONT+1)[CHAR]&0x000000FF);
MAX7219_DrawDigitSerial(DigitArray);
}