hello,
I want to use the max 7219 LED driver to controll the brightness of 64 LEDs individually. The hardware part is already done and I used the code example I found in the sketchbook "max7219_v1" to work with. My idea is to declare an array of 64 bytes. each byte stands for the brightness of an LED. In the future I want to controll these values with Processing over the serial port. At the moment I have 8 possible values for the brightness (0..7). there is a function called "brightnessController" which turns an LED "on" or "off", depending on its value in the array "brightnessArray". The programm itself seems to work, but I have performance problems! The on/off frequency of each LED is to slow, although there is no delay function in my programm. Any ideas to solve this problem or has someone already done a programm, that controlls the brightness of 64 LEDs individually?
Cheers,
Andreas
here is the sketch:
////////////////////////
byte brightnessArray[64];
byte maxArray[8];
////////////////////////
// define wiring pins
byte pin_max7219_clock = 7;
byte pin_max7219_load = 2;
byte pin_max7219_dataIn = 4;
// specify wiring pin i/o directions
void setPinModes()
{
pinMode(pin_max7219_dataIn, OUTPUT);
pinMode(pin_max7219_clock, OUTPUT);
pinMode(pin_max7219_load, OUTPUT);
}
// define max7219 registers
byte max7219_reg_noop = 0x00;
byte max7219_reg_digit0 = 0x01;
byte max7219_reg_digit1 = 0x02;
byte max7219_reg_digit2 = 0x03;
byte max7219_reg_digit3 = 0x04;
byte max7219_reg_digit4 = 0x05;
byte max7219_reg_digit5 = 0x06;
byte max7219_reg_digit6 = 0x07;
byte max7219_reg_digit7 = 0x08;
byte max7219_reg_decodeMode = 0x09;
byte max7219_reg_intensity = 0x0a;
byte max7219_reg_scanLimit = 0x0b;
byte max7219_reg_shutdown = 0x0c;
byte max7219_reg_displayTest = 0x0f;
// function to control max7219 data line
void max7219_setData(boolean value)
{
digitalWrite(pin_max7219_dataIn, value);
}
// function to control max7219 clock line
void max7219_setClock(boolean value)
{
digitalWrite(pin_max7219_clock, value);
}
// function to control max7219 load line
void max7219_setLoad(boolean value)
{
digitalWrite(pin_max7219_load, value);
}
// function that puts a byte of data to the max7219
void max7219_putByte(byte data)
{
byte i = 8;
byte mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
max7219_setClock(LOW); // tick
if (data & mask){ // choose bit
max7219_setData(HIGH); // send 1
}
else{
max7219_setData(LOW); // send 0
}
max7219_setClock(HIGH); // tock
--i; // move to lesser bit
}
}
// function that puts a byte of data into a max7219 register
void max7219_put(byte reg, byte data)
{
max7219_setLoad(HIGH); // begin
max7219_putByte(reg); // specify register
max7219_putByte(data); // put data
max7219_setLoad(LOW); // latch in data
max7219_setLoad(HIGH); // end
}
// function that sets brightness of the max7219
void max7219_setIntensity(byte intensity)
{
// range: 0x00 to 0x0f
max7219_put(max7219_reg_intensity, intensity & 0x0f);
}
////////////////////////////////////////////
// function that sets the same value for all registers of the max7219
void max7219_all(byte value)
{
max7219_put(0x01, value);
max7219_put(0x02, value);
max7219_put(0x03, value);
max7219_put(0x04, value);
max7219_put(0x05, value);
max7219_put(0x06, value);
max7219_put(0x07, value);
max7219_put(0x08, value);
}
//////////////////////////////////////////////////////////////
// function that initializes the max7219 to use a matrix of leds
void max7219_init()
{
max7219_put(max7219_reg_scanLimit, 0x07); // use all 8 columns
max7219_put(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
max7219_put(max7219_reg_shutdown, 0x01); // not in shutdown mode
max7219_put(max7219_reg_displayTest, 0x00); // no display test
max7219_all(0x00); // empty registers
max7219_setIntensity(0x0f); // set initial brightness to dim
}
///////////////////////////////////////////
// brightness controller //////////////////
///////////////////////////////////////////
void initBrightnessArray(){
int i;
for(i=0; i<64; i++){
// just to test brightness controller (brightness gradient from first LED row to last)
brightnessArray = i/8;
- }*
}
void brightnessController(){ - byte index;*
- byte bitpos;*
- int i;*
- int j;*
- int k;*
- // set all to zero*
- for(i=0; i<8; i++){*
_ maxArray = 0;_
* }*
* // 8 brightness values*
* for(i=0; i<8; i++){*
* // 64 LEDs*
* for(j=0; j<64; j++){*
* //*
* if (brightnessArray[j] <= i){*
* index = j/8;*
* bitpos = j%8;*
* maxArray[index] = maxArray[index] | 1<<bitpos;*
* }*
* }*
* // output to max7219*
* for(k=0; k<8; k++){*
* max7219_put(0x0+(k+1), maxArray[k]);
_ }_
_ // delay (although when there is no delay the programm seems to be too slow!?)_
_ delayMicroseconds(1);_
_ }_
_}_
_//////////////////////////////////////////_
_// program initialization routine*_
void setup()
{
* beginSerial(57600);*
* setPinModes();*
* max7219_init();
_ initBrightnessArray();_
_}_
_// program loop*_
void loop()
{
* // brightness*
* brightnessController();*
}