// ShiftBrite communication for the AVR using bit banging. // // Pin Assignments (any four free digital I/O pins will do) // // Data PC2 // Latch PC3 // Enable PC4 // Clock PC5 // // Feel free to modify and use this code for your AVR. #define F_CPU 16000000UL //Define clock speed as 16MHz // Include Files --------------------------------------------------------------------------------------- #include //include the IO ports and definitions for the AtMega128 #include //add delay functionality #include /* required for randomize() and random() */ typedef union ShiftBritePacket { unsigned long value; struct { unsigned greenDotCorrect:7; unsigned clockMode:2; unsigned :1; unsigned redDotCorrect:7; unsigned :3; unsigned blueDotCorrect:7; }; struct { unsigned green:10; unsigned red:10; unsigned blue:10; unsigned command:1; }; } ShiftBritePacket; // colorPacket returns a ShiftBritePacket for setting color brightnesses // // red, green, and blue are brightness values from 0 to 1023. 0 is off, and // 1023 is brightest. ShiftBritePacket colorPacket(unsigned int red, unsigned int green, unsigned int blue) { //Make a packet and initialize all of the bits to zero. ShiftBritePacket shiftbrite_packet = {value:0}; shiftbrite_packet.red = red; shiftbrite_packet.green = green; shiftbrite_packet.blue = blue; return shiftbrite_packet; } // commandPacket returns a ShiftBritePacket for sending commands to the A6281. // // redDotCorrect, greenDotCorrect, and blueDotCorrect lets you control what // percentage of current is flowing to each color diode. // Refer to page 8 of the datasheet for more information. // clockMode lets you set the PWM frequency for the diodes. // Refer to page 7 of the datasheet for more information. ShiftBritePacket commandPacket(unsigned int redDotCorrect, unsigned int greenDotCorrect, unsigned int blueDotCorrect, unsigned char clockMode) { //Make a packet and initialize all of the bits to zero. ShiftBritePacket shiftbrite_packet = {value:0}; shiftbrite_packet.redDotCorrect = redDotCorrect; shiftbrite_packet.greenDotCorrect = greenDotCorrect; shiftbrite_packet.blueDotCorrect = blueDotCorrect; shiftbrite_packet.clockMode = clockMode; shiftbrite_packet.command = 1; return shiftbrite_packet; } void sendPacket(ShiftBritePacket shiftbrite_packet) { for(int i = 1; i < 32 + 1; i++) { //Set the appropriate Data In value according to the packet. if ((shiftbrite_packet.value >> (32 - i)) & 1) PORTC |= (1 << PORTC2); else PORTC &= ~(1 << PORTC2); //Toggle the clock bit twice. PORTC ^= (1 << PORTC5); PORTC ^= (1 << PORTC5); } } void latch() { // Set Latch high PORTC |= (1 << PORTC3); // Set Latch low PORTC &= ~(1 << PORTC3); } void sendColorWheelPacket(unsigned int location_on_wheel) { if (location_on_wheel < 400) sendPacket(colorPacket(1000,(location_on_wheel % 400),10)); else if (location_on_wheel < 800) sendPacket(colorPacket(400-(location_on_wheel % 400),1000,10)); else if (location_on_wheel < 1200) sendPacket(colorPacket(10,1000,(location_on_wheel % 400))); else if (location_on_wheel < 1600) sendPacket(colorPacket(10,400-(location_on_wheel % 400),1000)); else if (location_on_wheel < 2000) sendPacket(colorPacket((location_on_wheel % 400),0,1000)); else if (location_on_wheel < 2400) sendPacket(colorPacket(1000,0,400-(location_on_wheel % 400))); } int main() { // Set all to outputs. DDRC |= (1 << PORTC2); // Data In DDRC |= (1 << PORTC3); // Latch DDRC |= (1 << PORTC4); // Enable DDRC |= (1 << PORTC5); // Clock // Set the Enable output low to enable the ShiftBrites. PORTC &= ~(1 << PORTC4); //Have the ShiftBrites change from red to yellow to green to cyan to blue //to purple and back to red. while(1) { // for(int i = 10; i<2400; i=i+10) // { // sendColorWheelPacket(i); // _delay_ms(100); // latch(); // } sendPacket(colorPacket(0,0, 1000)); _delay_ms(10); latch(); _delay_ms(10); while(1) { PORTC|= (1 << PORTC4);// Disable _delay_ms(50); PORTC &= ~(1 << PORTC4); _delay_ms(50); } /* int R = 1000; int G = 1000; int B = 1000; for(int j=0; j < 900; j++) { if(j<100) { sendPacket(colorPacket(R, G, B)); } else if(j<200) { R=R-10; sendPacket(colorPacket(R, G, B)); } else if(j<300) { B=B-10; sendPacket(colorPacket(R, G, B)); } else if(j<400) { R=R+10; sendPacket(colorPacket(R, G, B)); } else if(j<500) { G=G-10; sendPacket(colorPacket(R, G, B)); } else if(j<600) { B=B+10; sendPacket(colorPacket(R, G, B)); } else if(j<700) { R=R-10; sendPacket(colorPacket(R, G, B)); } else if(j<800) { G=G+10; sendPacket(colorPacket(R, G, B)); } else if(j<900) { R=R+10; sendPacket(colorPacket(R, G, B)); } latch(); _delay_ms(70); } */ /* int R = 0; int G = 1000; int B = 0; for(int j=0; j < 600; j++) { if(j<100) { R=R+10; sendPacket(colorPacket(R, G, B)); B=0; } else if(j<200) { G=G-10; sendPacket(colorPacket(R, G, B)); B=0; } else if(j<300) { B=B+10; sendPacket(colorPacket(R, G, B)); G=0; } else if(j<400) { R=R-10; sendPacket(colorPacket(R, G, B)); G=0; } else if(j<500) { G=G+10; sendPacket(colorPacket(R, G, B)); R=0; } else if(j<600) { B=B-10; sendPacket(colorPacket(R, G, B)); R=0; } latch(); _delay_ms(50); }*/ /* // BLUE Red test for(int i = 0; i <1000; i=i+50) { sendPacket(colorPacket(1000,0,i)); latch(); _delay_ms(100); } */ /* for (int i = 0; i < 2400; i++) { sendColorWheelPacket(i); sendColorWheelPacket((i+400) % 2400); sendColorWheelPacket((i+800) % 2400); sendColorWheelPacket((i+1200) % 2400); sendColorWheelPacket((i+1600) % 2400); sendColorWheelPacket((i+2000) % 2400); latch(); }*/ /* //Strobe through Increasing in speed for(int i=500; i>0; i=i-10) { sendPacket(colorPacket(1023, 0, 0)); latch(); _delay_ms(i); sendPacket(colorPacket(0, 1023, 0)); latch(); _delay_ms(i); sendPacket(colorPacket(0, 0, 1023)); latch(); _delay_ms(i); } for (int red = 0; red<500; red = red+175) { for (int blue = 0; blue<500; blue = blue+250) { for (int green = 0; green<500; green = green+500) { sendPacket(colorPacket(red, green, blue)); latch(); _delay_ms(10); } } } */ } //Loop forever while(1); }