/* code for max 7219 from maxim, reduced and optimised for useing more then one 7219 in a row, ______________________________________ Code History: -------------- The orginal Code where written for the wiring board by: * Nicholas Zambetti and Dave Mellis * Interaction Design Institute Ivrea * Dec 2004 * http://www.potemkin.org/uploads/Wiring/MAX7219.txt The first modification by: * Marcus Hannerstig * 2006 * K3, malmš hšgskola * http://www.xlab.se | http://arduino.berlios.de The second modification by: * tomek ness * Feb 2007 * FH-Potsdam * http://design.fh-potsdam.de/ This Version is modified by: * michael shaub * Jul 2007 * LA_CA_USA * http://www.cageybirds.com/ * @acknowledgements: eric f. -if you just use one max7219 >>> use the funktion maxSingle to controll the little guy ---maxSingle(register (1-8), collum (0-255)) -if you useing more then one max7219 and they all should do the same >>> use the funktion maxAll ---maxAll(register (1-8), collum (0-255)) -if you useing more then one max7219 and just want to change some thing at one little guy >>> use the funktion maxOne ---maxOne(Max you wane controll (1== the first one), register (1-8), collum (0-255)) /* take care with the initiation you have to send every part to every max7219 and then upload it. so for example if you have 5 max7219, you have to send the scanLimit 5 times befor you load it, other wise not every max7219 will get the shit. maxInUse is keeping track of this, just tell him how many max7219 you are uesing */ int dataIn = 12; int load = 11; int clock = 10; int maxInUse = 8; //here you have to change this varialbe to how many max 7219 you want to use int e = 0; // just a varialble int delayvar = 300; // a delay varialble int count = 0; // a counting varialble int xspeed, yspeed; // bounce varialbles int xpos, ypos, wdth, ht; // bounce varialbles int xpixel, ypixel; // bounce varialbles // 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; void putByte(byte data) { byte i = 8; byte mask; while(i > 0) { mask = 0x01 << (i - 1); // get bitmask digitalWrite( clock, LOW); // tick if (data & mask){ // choose bit digitalWrite(dataIn, HIGH); // send 1 }else{ digitalWrite(dataIn, LOW); // send 0 } digitalWrite(clock, HIGH); // tock --i; // move to lesser bit } } void maxSingle( byte reg, byte col) { //maxSingle is the more easy way while just useing one max7219 digitalWrite(load, LOW); // begin putByte(reg); // specify register putByte(col);//((data & 0x01) * 256) + data >> 1); // put data digitalWrite(load, LOW); // and load da shit digitalWrite(load, HIGH); } void maxAll (byte reg, byte col) { // put the initialisation to all the max 7219 in the system int c = 0; digitalWrite(load, LOW); // begin for ( c =1; c<= maxInUse; c++) { putByte(reg); // specify register putByte(col);//((data & 0x01) * 256) + data >> 1); // put data } digitalWrite(load, LOW); digitalWrite(load,HIGH); } void maxOne(byte maxNr, byte reg, byte col) { //maxOne is for adressing different max wille having a couple of them casaded int c = 0; digitalWrite(load, LOW); // begin for ( c = maxInUse; c > maxNr; c--) { putByte(0); // means no opperation putByte(0); // means no opperation } putByte(reg); // specify register putByte(col);//((data & 0x01) * 256) + data >> 1); // put data for ( c =maxNr-1; c >= 1; c--) { putByte(0); // means no opperation putByte(0); // means no opperation } digitalWrite(load, LOW); // and load da shit digitalWrite(load,HIGH); } void setup () { pinMode(dataIn, OUTPUT); pinMode(clock, OUTPUT); pinMode(load, OUTPUT); beginSerial(9600); //1200, 2400, 4800, 9600, 19200, 38400, 57600 and 115200 bits per second digitalWrite(13, HIGH); //////////////////////////////////////////////initiation of the max 7219 maxAll(max7219_reg_scanLimit, 0x07); maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode maxAll(max7219_reg_displayTest, 0x00); // no display test for (e=1; e<=8; e++) { // empty registers, turn all LEDs off maxAll(e,0); } maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set // range: 0x00 to 0x0f //ball speed xspeed = 1; yspeed = 1; //ball size wdth = 1; ht = 1; //initial ball placement xpos = 2; ypos = 4; } void loop () { //------------------------------------ if you use just one max7219 he should look like this xpixel = 1 << (7 - xpos); //Bitshift(<<): multiplies the left operand by 2 raised to the right operand power ypixel = ypos; //Selectively Clear Screen for (int count=1; count<8; count++) { //intialize count variable as 1, limit "for" loop to 8, increment count variable by 1 each time 'round if (ypixel!=count){ //test to see that ball isn't being drawn on this row maxOne(1,count,0); //clear the row } } //draw ball maxOne(1,ypixel,xpixel); //draw paddle //maxOne(1,8,xpixel); //draw base maxOne(1,8,255); //upgrade position values xpos+=xspeed; ypos+=yspeed; /*conditionals detects ball collission with matrix edges also accounts for thickness of ball */ if (xpos>=8-wdth/2 || xpos<=wdth/2){ xspeed*=-1; } if (ypos>=7-ht/2 || ypos<=ht/2){ yspeed*=-1; } delay(50); // delay (delayvar) }