Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this tunes the accuracy of _delay_ms
- #define F_CPU 1000000
- // common modules that we use in this program
- #include <avr/io.h>
- #include <avr/sleep.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- // these are the I/O lines that each LED is attached to. Used in the interrupt driver.
- #define RED0IO PA0
- #define GREEN0IO PA1
- #define BLUE0IO PA2
- #define RED1IO PA3
- #define GREEN1IO PA4
- #define BLUE1IO PA7
- #define SWITCH0 PA5
- #define SWITCH1 PA6
- // FULL ON = 32
- #define RED 0
- #define GREEN 1
- #define BLUE 2
- volatile char displaybuffer[16][3];
- /***********************************************************************************/
- // pattern subroutines here
- //
- // rules for writing pattern functions:
- // - call ExitCheck often, leave when it returns 1. Putting large delays between ExitCheck()
- // calls will result in bad button performance.
- // - you must set loops=0 at the beginning of your function so ExitCheck() knows how
- // long you have been running.
- // - you can use loops yourself if you need to
- // - use _delay_ms() for timing
- // this is used for timing patterns. It gets incremented every time the display driver
- // completes a cycle. Current value for 1 second defined below.
- // this is the number of hertz that the display is updating at also.
- volatile int loops = 0;
- #define ONESECOND 64
- // button tracking - this counts up every 1/xxx of a second that the button is down.
- // when the count reaches defined values, things happen.
- // or for some functions, when the button is released, different things happen based on how long it was down
- int button0_down_count = 0;
- int button1_down_count = 0;
- /***********************************************************************************/
- // update this to the max mode when you add more modes
- // should be one more than actual modes - the biggest one runs all patterns
- #define max_mode 20
- /***********************************************************************************/
- // These indicate which mode we should be running
- // the ISR changes button_mode immediately when the button is clicked
- // the main program notices that the two no longer match and switches modes
- volatile int button_mode = max_mode; // this is changed by the button within the ISR
- volatile int current_mode = max_mode; // this follows button_mode when the main code reacts
- volatile int shutdown_now = 0;
- /**********************************************************************/
- // use for millisecond delay. This is not terribly accurate, but probably good enough
- // accuracy to whatever the loop counter is
- // this will exit as soon as an exit condition is met.
- // if this returns 1, leave immediately.
- #define DELAY(x) if (Delay(x)) return
- int Delay(int ms)
- {
- int mscount = 0;
- while (mscount < ms)
- {
- _delay_ms(1);
- if (button_mode != current_mode)
- return 1;
- if (shutdown_now)
- return 1;
- mscount++;
- }
- return 0;
- }
- // set loops to 0 before beginning a function that will call this
- // mostly depricated, probably want to just use Delay() instead.
- int ExitCheck()
- {
- if (button_mode != current_mode)
- return 1;
- if (loops > (ONESECOND*60))
- return 1;
- return 0;
- }
- /**********************************************************************/
- // utility functions
- int cddelay = 0; // hack because we have no default parameters in this C
- void ClearDisplay()
- {
- for (int x=0; x<16; x++)
- {
- if (cddelay > 0)
- DELAY(cddelay); // makes a clock wipe)
- for (int color = 0; color < 3; color++)
- displaybuffer[x][color] = 0;
- }
- }
- void DimAll()
- {
- for (int x=0; x<16; x++)
- for (int color = 0; color < 3; color++)
- if (displaybuffer[x][color] > 0)
- displaybuffer[x][color]--;
- }
- // original posted by odokemono on avrfreaks:
- //uint16_t rng16(uint16_t seed) {
- // // Call four times with non-zero args to seed.
- // // Period is greater than one quintillion.
- // static uint32_t k=1148543851;
- // static uint32_t i=1234567891;
- // if(seed) {
- // i=(i<<16)+((k<<16)>>16);
- // k=(k<<16)+seed;
- // return(0);
- // }
- // k=30903*(k&65535)+(k>>16);
- // i=31083*(i&65535)+(i>>16);
- // return(k+i);
- //}
- uint16_t myrand() {
- static uint32_t k=1148543851;
- static uint32_t i=1234567891;
- k=30903*(k&65535)+(k>>16);
- i=31083*(i&65535)+(i>>16);
- return(k+i);
- }
- /**********************************************************************/
- // call when mode switches, it will indicate the new mode for 1 second
- void ModeSwitchPattern()
- {
- ClearDisplay();
- for (int x=0; x<16; x++)
- {
- // max_mode is "rotate all modes" - display all the LEDs
- if ((current_mode == max_mode) || (current_mode & (1<<x)) > 0)
- displaybuffer[x][RED] = 15;
- else
- displaybuffer[x][RED] = 0;
- }
- loops = 0;
- while (loops < ONESECOND)
- {
- if (ExitCheck())
- return;
- }
- }
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- // the /8 is because for this circuit, full on = 32
- void Wheel(int WheelPos, int LEDnum)
- {
- if (WheelPos < 85)
- {
- displaybuffer[LEDnum][RED] = ((WheelPos * 3)/8);
- displaybuffer[LEDnum][GREEN] = ((255 - WheelPos * 3)/8);
- displaybuffer[LEDnum][BLUE] = 0;
- }
- else if(WheelPos < 170)
- {
- WheelPos -= 85;
- displaybuffer[LEDnum][RED] = ((255 - WheelPos * 3)/8);
- displaybuffer[LEDnum][GREEN] = 0;
- displaybuffer[LEDnum][BLUE] = ((WheelPos * 3)/8);
- }
- else
- {
- WheelPos -= 170;
- displaybuffer[LEDnum][RED] = 0;
- displaybuffer[LEDnum][GREEN] = ((WheelPos * 3)/8);
- displaybuffer[LEDnum][BLUE] = ((255 - WheelPos * 3)/8);
- }
- }
- void rainbow()
- {
- int i, j;
- ClearDisplay();
- loops = 0;
- while (loops < 60 * ONESECOND)
- {
- for (j=0; j<256; j++)
- {
- for(i=0; i<16; i++)
- {
- Wheel((i*8+j) & 255, i);
- }
- DELAY(10);
- }
- }
- }
- void DumbCircle()
- {
- ClearDisplay();
- // add a prime number to the value every time around
- int wheelpos = 0;
- loops = 0;
- while (loops < 60 * ONESECOND)
- {
- for (unsigned char x=0; x<16; x++)
- {
- Wheel(wheelpos, x);
- DELAY(100);
- }
- wheelpos = (wheelpos+71)%256;
- }
- }
- void Chaser()
- {
- ClearDisplay();
- loops = 0;
- int colors = 0;
- while (loops < 60 * ONESECOND)
- {
- colors += 8;
- if (colors++ > 255)
- colors = 0;
- for (int x=0; x<16; x++)
- {
- // dim each LED by 4 values for each color each round
- for (int y=0; y<4; y++)
- {
- DimAll();
- DELAY(20);
- }
- // then set a new color
- Wheel(colors, x);
- }
- }
- }
- void SlowColorChase()
- {
- int i, j;
- ClearDisplay();
- loops = 0;
- int startpos = 0;
- int foo = 0;
- while (loops < 60 * ONESECOND)
- {
- for (j=0; j<256; j++)
- {
- Wheel((startpos*8+j) & 255, startpos);
- int next = (startpos+1 % 16);
- Wheel((next*8+j) & 255, next);
- DELAY(10);
- if (foo++ == 100)
- {
- displaybuffer[startpos][RED] = 0;
- displaybuffer[startpos][GREEN] = 0;
- displaybuffer[startpos][BLUE] = 0;
- foo = 0;
- startpos = (startpos+1)%16;
- }
- }
- }
- }
- void updownleftright()
- {
- loops = 0;
- int colorpos = 0;
- int dir = 0;
- int start = 0;
- int two[2];
- while (loops < 60 * ONESECOND)
- {
- ClearDisplay();
- dir = (dir + 1) % 2;
- colorpos = (colorpos + 37) % 256;
- for (int x=0; x < 8; x++)
- {
- if (dir == 0) // up
- {
- two[0] = 7 - x;
- two[1] = 8 + x;
- }
- if (dir == 1) // down
- {
- two[0] = x;
- two[1] = 15 - x;
- }
- two[0] = (two[0] + start)%16;
- two[1] = (two[1] + start)%16;
- Wheel(colorpos, two[0]);
- Wheel(colorpos, two[1]);
- DELAY(150);
- ClearDisplay();
- }
- if (dir == 0)
- start = (start+1) % 16;
- }
- }
- void incspin()
- {
- loops = 0;
- int colorpos = 0;
- int numon = 0;
- int bigcount = 1;
- ClearDisplay();
- while (loops < 60 * ONESECOND)
- {
- for (int x=0; x<=numon; x++)
- {
- Wheel(colorpos, (bigcount+(16/(numon+1))*x)%16);
- }
- if (bigcount % 128 == 0)
- numon = (numon + 1) % 2;
- colorpos = (colorpos + 1) % 256;
- // dim each LED by 4 values for each color each round
- for (int y=0; y<4+numon; y++)
- {
- DimAll();
- DELAY(20);
- }
- // DELAY(750);
- // ClearDisplay();
- bigcount++;
- }
- }
- void smiley()
- {
- loops = 0;
- ClearDisplay();
- int leds[] = {1, 14, 4, 5,6,7,8,9,10,11, -1};
- for (int x=0; leds[x] > 0; x++)
- {
- // yellow = ffff00
- displaybuffer[leds[x]][RED] = 24;
- displaybuffer[leds[x]][GREEN] = 32;
- }
- while (loops < 5 * ONESECOND)
- {
- DELAY(1000);
- }
- }
- void slowspin(int direction)
- {
- loops = 0;
- int delay=100;
- int colorpos = 0;
- int numon = 0;
- int bigcount = 1;
- ClearDisplay();
- while (loops < 60 * ONESECOND)
- {
- for (int x=0; x<=numon; x++)
- {
- int lednum = (bigcount+(16/(numon+1))*x)%16;
- if (direction < 0)
- lednum = 15-lednum;
- Wheel(colorpos, lednum);
- }
- // if (bigcount % 128 == 0)
- // numon = (numon + 1) % 2;
- colorpos = (colorpos + 1) % 256;
- // dim each LED by 4 values for each color each round
- for (int y=0; y<3; y++)
- {
- DimAll();
- DELAY(delay);
- }
- // DELAY(750);
- // ClearDisplay();
- bigcount++;
- }
- }
- void quadwipe()
- {
- int delay=200;
- loops = 0;
- int colorpos = 0;
- int bigcount = 1;
- ClearDisplay();
- while (loops < 60 * ONESECOND)
- {
- for (int root = 4; root>0; root--)
- {
- for (int x=0; x<=4; x++)
- Wheel(colorpos, ((root-1+bigcount)+4*x)%16);
- DELAY(delay);
- }
- for (int root = 4; root>0; root--)
- {
- for (int x=0; x<=4; x++)
- {
- displaybuffer[((root-1+bigcount)+4*x)%16][RED] = 0;
- displaybuffer[((root-1+bigcount)+4*x)%16][GREEN] = 0;
- displaybuffer[((root-1+bigcount)+4*x)%16][BLUE] = 0;
- }
- if (root>1)
- DELAY(delay);
- }
- colorpos = (colorpos + 37) % 256;
- bigcount++;
- }
- }
- void sparkle(int fill)
- {
- void addcolor(int lednum, int color, int value)
- {
- int cv = displaybuffer[lednum][color];
- cv += value - 4; // slight preference for brightening
- if (cv > 32)
- cv = 32;
- if (cv < 0)
- cv = 0;
- displaybuffer[lednum][color] = cv;
- }
- loops = 0;
- int foo = 0;
- while (loops < ONESECOND*60)
- {
- // find an led to flash on
- int lednum = myrand() % 16;
- addcolor(lednum, RED, myrand() % 16);
- addcolor(lednum, GREEN, myrand() % 16);
- addcolor(lednum, BLUE, myrand() % 16);
- // if not filling, then dim every step, otherwise let it build up.
- if (fill == 0)
- {
- int dimamt = myrand() % 4;
- for (int x=0; x<dimamt; x++)
- {
- DimAll();
- DELAY(150);
- }
- }
- else
- {
- DELAY(250);
- if ((foo++ % 75) == 0)
- {
- cddelay = 100;
- ClearDisplay();
- cddelay = 0;
- }
- }
- }
- }
- void Quadrature()
- {
- ClearDisplay();
- loops = 0;
- int colorpos = 0;
- int state = 0;
- while (loops < ONESECOND*60)
- {
- Wheel(colorpos, state);
- Wheel(colorpos, 15-state);
- Wheel(colorpos, 8+state);
- Wheel(colorpos, 7-state);
- state++;
- if ((state == 4) || (state == 8))
- {
- colorpos = (colorpos+97)%256;
- }
- if (state == 8)
- state = 0;
- DimAll();
- DELAY(300);
- }
- }
- void ColorPulse()
- {
- loops = 0;
- int wheelpos = myrand() % 256;
- while (loops < ONESECOND*60)
- {
- wheelpos = (wheelpos + 157) % 256;
- for (int x=0; x<16; x++)
- Wheel(wheelpos, x);
- for (int x=0; x<32; x++)
- {
- DELAY(20);
- DimAll();
- }
- DELAY(1000);
- }
- }
- void rndfill()
- {
- loops = 0;
- int color = 0;
- while (loops < ONESECOND*60)
- {
- color = (color + 37) % 256;
- for (int x=0; x<8; x++)
- {
- Wheel(color, 8+x);
- Wheel(color, 7-x);
- DELAY(100);
- }
- }
- }
- void rndspin()
- {
- loops = 0;
- while (loops < ONESECOND*60)
- {
- // pick a random spot and color and direction
- int spot = myrand() % 32;
- int dir = 1;
- if (spot > 16)
- {
- spot -= 16;
- dir = -1;
- }
- int color = myrand() % 256;
- for (int x=0; x<16; x++)
- {
- int pos = (x*dir) + spot;
- if (pos < 0)
- pos += 16;
- else if (pos > 15)
- pos -= 16;
- Wheel(color, pos);
- DELAY(50);
- }
- DELAY(100);
- }
- }
- int countval = 0;
- void binarycount()
- {
- ClearDisplay();
- loops = 0;
- while (loops < ONESECOND*60)
- {
- countval++;
- if (countval > 65536)
- countval = 0;
- for (int x=0; x<16; x++)
- {
- if ((countval & (1<<x)) > 0)
- displaybuffer[x][RED] = 15;
- else
- displaybuffer[x][RED] = 0;
- }
- DELAY(100);
- }
- }
- void afspin()
- {
- loops = 0;
- int color = 0;
- int start = 0;
- int colorcount = 0;
- while (loops < ONESECOND*60)
- {
- ClearDisplay();
- for (int x=0; x<4; x++)
- Wheel(color,(x*4+start)%16);
- DELAY(200);
- start--;
- if (start < 0)
- start = 3;
- if (colorcount++ > 32)
- {
- color = (color+97)%256;
- colorcount = 0;
- }
- }
- }
- int cired = 0;
- int cigreen = 0;
- int ciblue = 0;
- void clockish()
- {
- loops = 0;
- ClearDisplay();
- while (loops < ONESECOND*60)
- {
- displaybuffer[cired][RED] = 0;
- displaybuffer[cigreen][GREEN] = 0;
- displaybuffer[ciblue][BLUE] = 0;
- cired++;
- if (cired > 15)
- {
- cired = 0;
- cigreen++;
- if (cigreen > 15)
- {
- cigreen = 0;
- ciblue++;
- if (ciblue > 15)
- ciblue = 0;
- }
- }
- displaybuffer[cired][RED] = 16;
- displaybuffer[cigreen][GREEN] = 16;
- displaybuffer[ciblue][BLUE] = 16;
- DELAY(50);
- }
- }
- // this is supposed to look like a Newton's cradle toy. Not finished
- void Newton()
- {
- ClearDisplay();
- loops = 0;
- for (int x=-5; x<5; x++)
- {
- for (int y=0; y<7; y++)
- {
- int z = x+y;
- if (z>0 && y==0)
- displaybuffer[z-1][RED] = 0;
- if (z>=0)
- displaybuffer[z][RED] = 32;
- DELAY(150);
- }
- }
- while (loops < ONESECOND*60)
- {
- DELAY(1000);
- // raise right one
- for (int x=5; x>2; x--)
- {
- displaybuffer[x][RED] = 0;
- displaybuffer[x-1][RED] = 32;
- DELAY(500);
- }
- // hold for a second
- DELAY(1000);
- int h=3;
- }
- }
- void LEDTestPWM()
- {
- for (int color=0; color<3; color++)
- {
- for (int LED = 0; LED < 16; LED++)
- {
- for (int val=0; val<32; val++)
- {
- displaybuffer[LED][color] = val;
- _delay_ms(15);
- }
- for (int val=32; val>0; val--)
- {
- displaybuffer[LED][color] = val-1;
- _delay_ms(15);
- }
- }
- }
- }
- /************************************************************************************
- ** this stuff is for writing to the serial shift register
- ************************************************************************************/
- // write a given byte out to the shift register (74HC595)
- #define SEROUT PB0
- #define SERCLK PB1
- void ShiftOutByte(unsigned char val)
- {
- PORTB = 0;
- DDRB = 1<<SEROUT | 1<<SERCLK;
- for (unsigned char x=0; x<8; x++)
- {
- // set or clear serial out bit
- if (val & 0x80)
- PORTB |= 1<<SEROUT;
- else
- PORTB &= ~(1<<SEROUT);
- PORTB |= 1<<SERCLK;
- PORTB &= ~(1<<SERCLK);
- val <<= 1;
- }
- }
- // shift one bit in
- void ShiftOver(unsigned char val)
- {
- DDRB = 1<<SEROUT | 1<<SERCLK;
- if (val & 0x01)
- PORTB |= 1<<SEROUT;
- else
- PORTB &= ~(1<<SEROUT);
- PORTB |= 1<<SERCLK;
- PORTB &= ~(1<<SERCLK);
- }
- // this is to run BEFORE the display driver is up and the interrupts are running
- // strictly bit bang, the CPU can be programmed during this phase because we haven't bumped
- // the CPU speed yet.
- void LEDTest()
- {
- PORTA = 0;
- DDRA = 1<<RED0IO | 1<<GREEN0IO | 1<<BLUE0IO | 1<<RED1IO | 1<<GREEN1IO | 1<<BLUE1IO;
- unsigned char colors[] = {1<<RED0IO, 1<<RED1IO, 1<<GREEN0IO, 1<<GREEN1IO, 1<<BLUE0IO, 1<<BLUE1IO};
- for (int color = 0; color<6; color++)
- {
- PORTA = colors[color];
- ShiftOutByte(0xfe);
- for (int led=0; led < 8; led++)
- {
- _delay_ms(250);
- ShiftOver(1);
- }
- }
- }
- void SetupDisplayInterrupt()
- {
- // _delay_ms(1000); // once we change the clock speed, the device won't be programmable anymore, so give a second to do the programming
- cli(); // clear interrupt flag in SREG so we don't get interrupted here.
- // Setup Timer 0
- TCCR0A = 0; // normal mode
- TCCR0B = (1 << CS00); // system clock, no prescaler
- TCNT0 = 0; // Set timer count to initial value (not actually necessary)
- // enable interrupts
- TIMSK0 = (1<<TOIE0); // Timer Interrupt Mask: Enable interrupt on Timer Overflow Interrupt Event 0
- sei(); // set enable interrupts
- // speed up chip to 4MHz
- CLKPR = 1<<CLKPCE; // Clock Prescale register - turn on bit Change Enable
- CLKPR = 1<<CLKPS0; // divide (8mhz) by 4
- }
- int main()
- {
- // Set up basic I/O
- // LEDs output, other pins input (switches)
- DDRA = 1<<RED0IO | 1<<GREEN0IO | 1<<BLUE0IO | 1<<RED1IO | 1<<GREEN1IO | 1<<BLUE1IO;
- PORTA = 1<<SWITCH0 | 1<<SWITCH1; // PA switch pull-ups on
- // begin state for shift register.
- ShiftOutByte(0xFE);
- // if top button is pressed on power up, go into programming mode - basically just wait forever
- if ((PINA & 1<<SWITCH0) == 0) // button pressed = 0 on PB0
- {
- while (1)
- {
- PORTA = 1<<RED0IO;
- _delay_ms(50);
- PORTA = 0;
- _delay_ms(1000);
- PORTA = 1 << RED1IO;
- _delay_ms(50);
- PORTA = 0;
- _delay_ms(1000);
- }
- }
- // do one or the other to allow programming
- // LEDTest(); // this is the raw one with no PWM - may be hard on the LEDs.
- // _delay_ms(1000);
- int rotator = 0; // used for rotating all modes when in demo mode
- ClearDisplay();
- SetupDisplayInterrupt();
- // if bottom button is pressed on power up, do a LED test
- if ((PINA & 1<<SWITCH1) == 0) // button pressed = 0 on PB0
- {
- while (1)
- {
- LEDTestPWM();
- }
- }
- //while (1)
- //clockish();
- //afspin();
- //binarycount();
- // LEDTEST();
- // displaybuffer[0][0] = 31;
- // while (1)
- // LEDTestPWM();
- // rainbow(10);
- // it'd be nice to figure out a way to actually make this be random
- // seed = 0xfff - (TCNT0 | TCNT0<<8);
- ClearDisplay();
- while (1)
- {
- if (button_mode != current_mode)
- {
- current_mode = button_mode;
- ModeSwitchPattern(); // this displays the current mode in binary for 1 second
- }
- else
- {
- int runmode;
- if (current_mode == max_mode)
- {
- runmode = rotator;
- rotator++;
- }
- else
- {
- runmode = current_mode;
- }
- switch (runmode)
- {
- case 1:
- rainbow();
- break;
- case 2:
- SlowColorChase();
- break;
- case 3:
- Chaser();
- break;
- case 4:
- DumbCircle();
- break;
- case 5:
- updownleftright();
- break;
- case 6:
- incspin();
- break;
- case 7:
- sparkle(0);
- break;
- case 8:
- slowspin(-1);
- break;
- case 9:
- smiley();
- break;
- case 10:
- quadwipe();
- break;
- case 11:
- slowspin(1);
- break;
- case 12:
- sparkle(1);
- break;
- case 13:
- Quadrature();
- break;
- case 14:
- ColorPulse();
- break;
- case 15:
- rndspin();
- break;
- case 16:
- rndfill();
- break;
- case 17:
- clockish();
- break;
- case 18:
- afspin();
- break;
- case 19:
- binarycount();
- break;
- }
- }
- }
- return 0;
- }
- #define cyclecount 35
- #define RED0 0
- #define GREEN0 1
- #define BLUE0 2
- #define RED1 3
- #define GREEN1 4
- #define BLUE1 5
- // Interrupt Service Routine
- // for Timer 0 Overflow
- ISR(TIM0_OVF_vect)
- {
- static short loopcount=0;
- static unsigned char PWMCount = 0; // number of times we enter this function per PWM cycle (full brightness)
- static short LEDNumberA, LEDNumberB; // points to the set of 3 in displaybuffer that we're on right now
- static unsigned char compare[6];
- // these are the values we calculate each time for the next entrance
- static unsigned char pinlevelA = 0;
- // this section disables specific LEDs as their PWM time expires
- if (compare[RED0] == PWMCount) { pinlevelA &= ~(1<<RED0IO);}
- if (compare[GREEN0] == PWMCount) { pinlevelA &= ~(1<<GREEN0IO);}
- if (compare[BLUE0] == PWMCount) { pinlevelA &= ~(1<<BLUE0IO);}
- if (compare[RED1] == PWMCount) { pinlevelA &= ~(1<<RED1IO);}
- if (compare[GREEN1] == PWMCount) { pinlevelA &= ~(1<<GREEN1IO);}
- if (compare[BLUE1] == PWMCount) { pinlevelA &= ~(1<<BLUE1IO);}
- PORTA = pinlevelA;
- ++PWMCount;
- TCNT0 = 128; // Initial value = closer to 255 = we come in here more times per second
- if (PWMCount == cyclecount) // giving a little off time even at full on gives a little more dynamic range control
- PWMCount = 0; // and drop down below to go to the next LED in the rotation
- else
- return;
- // switch banks every full PWM cycle
- // every 16, we drop down here. PWMcount will be zero and values will get reloaded next round.
- if (loopcount == 8)
- {
- // this is a good place to read the switch
- // the switch is attached to PA5. If it's low, the switch is pressed.
- // PORTA = 1<<SWITCH0 + 1<<SWITCH1; // PA pull-ups on
- if ((PINA & 1<<SWITCH0) == 0) // button pressed = 0 on PB0
- {
- // if button has been down for a long time, power off
- button0_down_count++;
- // increment once immediately, then 4 per second after one second depressed
- if ((button0_down_count == 5) || (button0_down_count == ONESECOND))
- {
- if (button_mode == max_mode)
- button_mode = 1;
- else
- button_mode++;
- if (button0_down_count == ONESECOND)
- button0_down_count -= (ONESECOND/4); // repeat speed
- }
- } else {
- button0_down_count = 0;
- if ((PINA & 1<<SWITCH1) == 0) // button pressed = 0 on PB0
- {
- // if button has been down for a long time, power off
- button1_down_count++;
- // increment once immediately, then 4 per second after one second depressed
- if ((button1_down_count == 5) || (button1_down_count == ONESECOND))
- {
- if (button_mode == 1)
- button_mode = max_mode;
- else
- button_mode--;
- if (button1_down_count == ONESECOND)
- button1_down_count -= (ONESECOND/4); // repeat speed
- }
- }
- else
- {
- button1_down_count = 0;
- }
- }
- loops++; // this is used for rough timing
- loopcount = 0; // start cycle over after last LED
- }
- if (loopcount == 0)
- {
- ShiftOver(0);
- } else {
- ShiftOver(1);
- }
- LEDNumberA = loopcount;
- LEDNumberB = loopcount+8;
- pinlevelA = 1<<RED0IO | 1<<GREEN0IO | 1<<BLUE0IO | 1<<RED1IO | 1<<GREEN1IO | 1<<BLUE1IO
- | 1<<SWITCH0 | 1<<SWITCH1; // PA pull-ups on
- // Load the values for each LED at the beginning of the cycle for it.
- compare[RED0] = displaybuffer[LEDNumberA][RED];
- compare[GREEN0] = displaybuffer[LEDNumberA][GREEN];
- compare[BLUE0] = displaybuffer[LEDNumberA][BLUE];
- compare[RED1] = displaybuffer[LEDNumberB][RED];
- compare[GREEN1] = displaybuffer[LEDNumberB][GREEN];
- compare[BLUE1] = displaybuffer[LEDNumberB][BLUE];
- loopcount++;
- }
- RBG595.c
- Open with
- Displaying RBG595.c.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement