Sorry for my code is long and my English is not good.
I'm working with 8x8x8 LED cube project using Arduino nano. I tried to understand code, but I don't understand how cube[][]
data write on pins
.
I just want to know, how write the data from cube[8][8]
to hardware using SPI. code download using this link.
Thanks in advance.
My code is here :
#define AXIS_X 1
#define AXIS_Y 2
#define AXIS_Z 3
#define REFRESH_RATE 120
int CUBE_SIZE = 8;
int SPI_CS = 10;// This SPI Chip Select pin controls the MAX72xx
byte value[8];
volatile unsigned char cube[8][8];
volatile int current_layer = 0;
void setup()
{
Serial.begin (115200);
Serial.println("jolliFactory 8x8x8 jolliCube Demo example 1.0");
pinMode(SPI_CS, OUTPUT);
SPI.begin();
maxTransferAll(0x0F, 0x00); // 00 - Turn off Test mode
maxTransferAll(0x09, 0x00); // Register 09 - BCD Decoding // 0 = No decoding
maxTransferAll(0x0B, 0x07); // Register B - Scan limit 1-7 // 7 = All LEDS
maxTransferAll(0x0C, 0x01); // 01 = on 00 = Power saving mode or shutdown
maxTransferAll(0x0A, 0x0F); // Set Brightness Intensity
setUpInterrupts();
}
// Get the current status of a voxel
unsigned char getvoxel(int x, int y, int z)
{
if (inrange(x,y,z))
{
if (cube[z][y] & (1 << x))
{
return 0x01;
} else
{
return 0x00;
}
} else
{
return 0x00;
}
}
void maxTransferAll(uint8_t address, uint8_t value)
{
digitalWrite(SPI_CS, LOW);
for ( int c=1; c<= CUBE_SIZE;c++) {
SPI.transfer(address);
SPI.transfer(value);
}
digitalWrite(SPI_CS, HIGH);
}
void maxTransferOne(uint8_t whichMax, uint8_t address, uint8_t value)
{
byte noop_reg = 0x00; //max72xx No op register
byte noop_value = 0x00; //value
digitalWrite(SPI_CS, LOW);
for (int i=CUBE_SIZE; i>0; i--) // Loop through our number of Max72xx ICs
{
if (i==whichMax)
{
SPI.transfer(address);
SPI.transfer(value);
}
else
{
SPI.transfer(noop_reg);
SPI.transfer(noop_value);
}
}
digitalWrite(SPI_CS, HIGH);
}
void maxTransferLEDCube(uint8_t address)
{
digitalWrite(SPI_CS, LOW);
for (int i=0; i<CUBE_SIZE; i++) // Loop through our number of Max72xx ICs
{
SPI.transfer(address);
SPI.transfer(value[CUBE_SIZE-i-1]);
}
digitalWrite(SPI_CS, HIGH);
}
void setUpInterrupts()
{
cli();//stop interrupts while we set them up
//set up an interrupt with timer1
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = (16000000/REFRESH_RATE/1024/CUBE_SIZE -1);
TCCR1B |= (1 << WGM12);
// Set to CS10 and CS12 so we have the 1024
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A);
sei();//re-allow interrupts
}
ISR(TIMER1_COMPA_vect)
{
for(byte y = 0 ; y < CUBE_SIZE ; y++)
{
byte b = 0;
for(byte x = 0 ; x < CUBE_SIZE ; x++)
{
//form data byte
b = b << 1;
if (getvoxel(x,y,current_layer)==1)
{
b |= 1;
}
}
value[y] = b;
}
maxTransferLEDCube(CUBE_SIZE - current_layer);
current_layer++;
if (current_layer == 8)
current_layer = 0;
}