MAX7219 Binary count

Used Products:

1. Arduino UNO
2. MAX7219CNG

Content:

Çeşme: http://playground.arduino.cc/LEDMatrix/Max7219

Code:

int dataInPin = 4; int loadPin = 6; int clockPin = 5; int maxInUse = 4; //here you have to change this varialbe to how many max 7219 you want to use // 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 = 0x08; byte max7219_reg_digit7 = 0x01; 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; int counter = 0; void putByte(byte data) { byte i = 8; byte mask; while(i > 0) { mask = 0x01 << (i - 1); // get bitmask digitalWrite(clockPin, LOW); // tick if (data & mask) // choose bit { digitalWrite(dataInPin, HIGH); // send 1 } else { digitalWrite(dataInPin, LOW); // send 0 } digitalWrite(clockPin, HIGH); // tock --i; // move to lesser bit } } void maxPut (byte reg, byte col) // use this { putByte(reg); // specify register putByte(col);//((data & 0x01) * 256) + data >> 1); // put data } void maxIni (byte reg, byte col) // put the initialisation to all the max 7219 in the system { int c = 0; digitalWrite(loadPin, HIGH); for (c = 1; c <= maxInUse; c++) { maxPut(reg, col); // use all 8 columns } digitalWrite(loadPin, LOW); digitalWrite(loadPin, HIGH); } void setup () { pinMode(dataInPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(loadPin, OUTPUT); Serial.begin(9600); maxIni(max7219_reg_scanLimit, 0x07); maxIni(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) maxIni(max7219_reg_shutdown, 0x01); // not in shutdown mode maxIni(max7219_reg_displayTest, 0x00); // no display test for (int i=1; i <= 8; i++) // empty registers, turn all LEDs off { maxIni(i,0); } maxIni(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set } void loop () { int i = 0; for (i=1; i<=8; i++) { digitalWrite(loadPin,HIGH); maxPut(i, counter); digitalWrite(loadPin, LOW); digitalWrite(loadPin,HIGH); } Serial.print("counter: "); Serial.print(counter++); Serial.println(); delay(500); for (i=1; i<=8; i++) // a longer way to clean the dispaly { digitalWrite(loadPin,HIGH); maxPut(i, 0); digitalWrite(loadPin, LOW); digitalWrite(loadPin,HIGH); } }