// octagonal star // has STC 8G1k17 mcu with 17K flash, 1.25K RAM, 3 timer/counters // ceptimus 2023-12-06 // compile with sdcc octagonalstar.c // erase chip flash memory with stcgal -p (port) -e // flash with stcgal -p (port) octagonalstar.ihx #include <8052.h> __sfr __at (0x8E) AUXR; __sfr __at (0x91) P1M1; __sfr __at (0x92) P1M0; __sfr __at (0xAF) IE2; __sfr __at (0xB1) P3M1; __sfr __at (0xB2) P3M0; __sfr __at (0xBA) P_SW2; __sbit __at (0xCC) P5_4; __sbit __at (0xCD) P5_5; __sfr __at (0xC9) P5M1; __sfr __at (0xCA) P5M0; __sfr __at (0xD6) T2H; __sfr __at (0xD7) T2L; __xdata volatile unsigned char __at(0xFE01) CLKDIV; #define RED P1_1 #define GREEN P1_0 #define BLUE P3_7 #define BUZZ P5_5 #define uint8_t unsigned char __code uint8_t primes[24] = {127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251}; // used by LED pattern 0 __code uint8_t buttonToNote[7] = {12,14,16,17,19,21,23}; // which notes to play for each single button in organ mode // 16-bit timer values to generate half-cycle delays for 37 chromatic notes from C3 up to C6 at default SYSCLK of 18.432 MHz __code unsigned int timerNoteVal[37] = {59666,59996,60307,60600,60877,61139,61386,61619,61839,62046,62242,62427, 62602,62766,62922,63069,63207,63338,63461,63578,63688,63792,63890,63982, 64069,64152,64229,64303,64372,64437,64499,64557,64612,64664,64713,64759,64803}; // which LED(s) to light for each note. Digits 0 to 6 for LED_1 to LED_7 represent C D E F G A B. Sharp/Flat notes light the adjacent pair. F means 'no LED' __code uint8_t noteToLeds[37] = {0xF0, 0x10, 0xF1, 0x21, 0xF2, 0xF3, 0x43, 0xF4, 0x54, 0xF5, 0x65, 0xF6, 0xF0, 0x10, 0xF1, 0x21, 0xF2, 0xF3, 0x43, 0xF4, 0x54, 0xF5, 0x65, 0xF6, 0xF0, 0x10, 0xF1, 0x21, 0xF2, 0xF3, 0x43, 0xF4, 0x54, 0xF5, 0x65, 0xF6, 0xF0}; #define NUM_TUNES 7 __code unsigned int numNotes[NUM_TUNES] = {406, 29, 107, 57, 73, 108, 60}; __code uint8_t tempoShift[NUM_TUNES] = {6, 7, 6, 6, 6, 5, 6}; // Eg. for first tune, lengths[] are shifted 6 bits to multiply by 64 // units for NOTE_GAP are ticks ~1500 Hz, 1 tick ~667 us #define NOTE_GAP 32 // Not particularly accurate version of Super Mario Overworld Theme. Original was Composed by Koji Kondo for Nintendo in 1985. __code uint8_t smNotes[406] = {17,17,17,0,13,17,20,0,13,0,8,0,5,10,12,11,10,8,17,20,22,18,20,17,13,15,12,0,13,0,8,0,5,10,12,0,11,10,8,17,20,22,18,20,17,0,13,15,12,0,0, 20,19,18,16,17,0,9,10,13,10,13,15,0,20,19,18,16,17,25,25,25,0,0,20,19,18,16,17,9,10,13,0,10,13,15,0,16,0,15,0,13,0,0,20,19,18,16,17,0,9,10,13,10,13,15,0,20,19,18,16,17, 25,25,25,0,0,20,19,18,16,17,9,10,13,0,10,13,15,0,16,0,15,0,13,0,13,13,13,13,15,17,13,10,8,0,13,13,13,0,13,15,17,0,13,13,13,13,15,17,13,0,10,8,0,17,17,17,13,17,20,0, 13,0,8,0,5,10,12,0,11,10,8,17,20,22,18,20,17,0,13,15,12,0,13,0,8,0,5,10,12,11,10,8,17,20,22,18,20,17,13,15,12,0,17,13,8,0,9,10,18,18,10,0,12,22,22,22,20,18,17,13,0,10,8,0, 17,13,8,0,9,10,18,18,10,0,12,18,18,18,17,15,13,0,17,13,8,0,9,10,18,18,10,0,12,22,22,22,20,18,17,13,10,8,0,17,13,8,0,9,10,18,18,10,0,12,18,18,18,17,15,13,0,13,13,13,13,15, 17,13,0,10,8,0,13,13,13,13,15,17,0,13,13,13,0,13,15,17,13,10,8,0,17,17,17,0,13,17,20,0,17,13,8,0,9,10,18,18,10,0,12,22,22,22,20,18,17,13,10,8,0,17,13,8,0,9,10,18,18,10,0, 12,18,18,18,17,15,13,0,13,13,13,13,15,17,13,0,10,8,0,13,13,13,13,15,17,0,13,13,13,0,13,15,17,13,10,8,0,17,17,17,0,13,17,20,0}; __code uint8_t smLengths[406] ={3,3,6,3,3,6,12,12,6,3,3,6,6,6,9,3,6,4,4,4,6,3,3,9,3,3,3,6,6,3,3,6,6,6,6,3,3,6,4,4,4,6,3,3,6,3,3,3,3,6,6,3,3, 3,3,6,3,3,3,6,3,3,3,6,3,3,3,3,6,9,3,6,6,6,3,3,3,3,9,3,3,3,3,3,3,3,6,6,3,3,6,6,18,6,3,3,3,3,6,3,3,3,6,3,3,3,6,3,3,3,3,6,9,3,6,6,6, 3,3,3,3,9,3,3,3,3,3,3,3,6,6,3,3,6,6,18,3,3,9,3,6,3,6,3,6,6,3,3,6,3,3,3,3,24,3,3,9,3,6,3,3,3,3,6,6,3,3,9,3,6,6,18, 6,3,3,6,6,6,6,3,3,6,4,4,4,6,3,3,6,3,3,3,3,6,6,3,3,6,6,6,9,3,6,4,4,4,6,3,3,9,3,3,3,6,3,3,6,6,6,3,6,3,6,6,4,4,4,4,4,4,3,3,3,3,6,6, 3,3,6,6,6,3,6,3,6,6,3,6,3,4,4,4,6,18,3,3,6,6,6,3,6,3,6,6,4,4,4,4,4,4,3,6,3,6,6,3,3,6,6,6,3,6,3,6,6,3,6,3,4,4,4,6,18, 3,3,9,3,6,3,3,3,3,6,6,3,3,9,3,3,3,24,3,3,6,3,3,6,3,6,3,6,6,3,3,6,3,3,6,6,18,3,3,6,6,6,3,6,3,6,6,4,4,4,4,4,4,3,6,3,6,6,3,3,6,6,6, 3,6,3,6,6,3,6,3,4,4,4,6,18,3,3,9,3,6,3,3,3,3,6,6,3,3,9,3,3,3,24,3,3,6,3,3,6,3,6,3,6,6,3,3,6,3,3,6,6,36}; // Coventry Carol tempoShift: 7 __code uint8_t ccNotes[29] = {20,20,19,20,23,22,20,19,20,22,23,25,22,20,27,25,23,22,23,22,20,19,20,19, 20,25,22,24, 0}; __code uint8_t ccLengths[29] = {8,8,8,16,8,16,8,24,8,8,8,8,16,40,8,16,8,16,8,16,8,24,8,8,8,8,16,48,48}; // Jingle Bells tempoShift: 6 __code uint8_t jbNotes[107] = {13,13,22,20,18,13,13,13,13,22,20,18,15,15,15,23,22,20,17,25,27,25,23,20, 22,0,13,13,22,20,18,13,13,13,13,22,20,18,15,15,15,23,22,20,25,25,25,25,27,25,23,20,18,0,25,22,22,22,22, 22,22,22,25,18,20,22,23,23,23,23,23,22,22,22,22,22,20,20,22,20,25,22,22,22,22,22,22,22,25,18,20,22,23, 23,23,23,23,22,22,22,22,25,25,23,20,18,0}; __code uint8_t jbLengths[107] = {10,10,10,10,10,30,5,5,10,10,10,10,30,10,10,10,10,10,30,10,10,10,10,10, 20,10,10,10,10,10,10,30,5,5,10,10,10,10,30,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,20,10,10,20,10, 10,20,10,10,10,10,40,10,10,10,10,10,10,10,5,5,10,10,10,10,20,20,10,10,20,10,10,20,10,10,10,10,40,10,10, 10,10,10,10,10,5,5,10,10,10,10,40,80}; // Rocking tempoShift: 6 __code uint8_t rNotes[57] = {18,22,25,25,27,27,24,25,27,27,24,25,23,23,20,22,22,18,20,20,17,18,22,25, 23,22,20,22,20,13,22,25,23,22,20,22,20,13,18,22,25,25,27,27,24,25,23,23,20,22,22,18,20,20,17,18,0}; __code uint8_t rLengths[57] = {16,16,16,16,16,8,8,32,16,8,8,32,16,8,8,16,8,8,16,8,8,32,16,16,16,16,16, 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,8,8,32,16,8,8,16,8,8,16,8,8,32,80}; // The First Nowell tempoShift: 6 __code uint8_t fnNotes[73] = {17,15,13,15,17,18,20,22,24,25,24,22,20,22,24,25,24,22,20,22,24,25,20,18, 17,17,15,13,15,17,18,20,22,24,25,24,22,20,22,24,25,24,22,20,22,24,25,20,18,17,17,15,13,15,17,18,20,25, 24,22,22,20,25,24,22,20,22,24,25,20,18,17,0}; __code uint8_t fnLengths[73] = {8,8,24,8,8,8,32,8,8,16,16,16,32,8,8,16,16,16,16,16,16,16,16,16,32,8,8, 24,8,8,8,32,8,8,16,16,16,32,8,8,16,16,16,16,16,16,16,16,16,32,8,8,24,8,8,8,32,8,8,32,16,48,16,16,16,16, 16,16,16,16,16,32,96}; // Walking In The Air tempoShift: 5 __code uint8_t witaNotes[108] = {22,27,27,25,25,22,0,22,27,27,25,25,22,18,20,0,20,23,23,22,22,20,15,18, 18,17,17,15,0,22,27,27,25,25,22,0,22,27,27,25,25,22,18,20,0,20,23,23,22,22,20,15,18,18,17,17,15,14,15, 17,18,20,22,24,25,27,24,20,16,18,20,22,23,25,22,27,26,22,0,22,27,27,25,25,22,0,22,27,27,25,25,22,18,20, 0,20,23,23,22,22,20,15,18,18,17,17,15,0}; __code uint8_t witaLengths[108] = {26,13,13,13,13,130,13,13,13,13,13,13,39,13,78,13,13,13,13,13,13,39, 13,13,13,13,13,130,13,13,13,13,13,13,130,13,13,13,13,13,13,39,13,78,13,13,13,13,13,13,39,13,13,13,13, 13,156,13,13,78,13,13,78,13,13,52,26,104,13,13,78,13,13,52,26,78,26,78,13,13,13,13,13,13,130,13,13,13, 13,13,13,39,13,78,13,13,13,13,13,13,39,13,13,13,13,13,130,130}; // Zither Carol tempoShift: 6 __code uint8_t zcNotes[60] = {15,17,15,19,20,19,22,24,22,24,14,14,17,15,14,12,10,15,17,15,19,20,19,22, 24,22,24,14,14,15,0,24,26,27,26,24,24,22,22,20,22,24,22,20,20,19,19,17,19,17,19,17,15,19,22,24,14,14, 15,0}; __code uint8_t zcLengths[60] = {8,8,16,8,8,16,8,8,16,16,16,16,8,8,8,8,16,8,8,16,8,8,16,8,8,16,16,16,16, 32,16,8,8,16,8,8,16,16,16,8,8,16,8,8,16,16,16,8,8,16,8,8,16,16,16,16,16,16,48,80}; __code uint8_t *firstNote[NUM_TUNES] = {smNotes, ccNotes, jbNotes, rNotes, fnNotes, witaNotes, zcNotes}; __code uint8_t *firstLength[NUM_TUNES] = {smLengths, ccLengths, jbLengths, rLengths, fnLengths, witaLengths, zcLengths}; __xdata unsigned int currentTimerNoteVal; __xdata unsigned int noteTickCount; __xdata unsigned int pauseTickCount; uint8_t music = 0; // allows main routine to signal music on/off to interrupt routine uint8_t currentTune; unsigned int currentNote; // position within tune uint8_t note; // currently playing note: 0 for rest, 1 for C3, up to 36 for C6 __xdata volatile unsigned int red[8]; // colour control for each of the 8 LEDS. range is 4- (off) to 2000 (fully on) __xdata volatile unsigned int green[8]; __xdata volatile unsigned int blue[8]; uint8_t fade = 0; // on/off flag for LED fading. uint8_t pwmStage; uint8_t pwmLED[3]; unsigned int pwmReload[3]; volatile uint8_t mode = 0; // selects operating mode - LED patterns, playing a tune, organ mode, etc. volatile uint8_t buttons = 0xFF; // bits 7 to 0 correspond to buttons S8 to S1: 0 when pressed, 1 when released. volatile uint8_t latchedButtons = 0x00; // like buttons, but opposite logic. Bit set when button goes down. Main routine must reset. uint8_t prevButtons = 0xFF; // used within interrupt routine to control latchedButtons volatile unsigned int ticks = 0; // increments every 667 us. wraps after 43.7 seconds void T0INT(void) __interrupt (1) __using(1) { // with default SYSCLK of 18.432 MHz: ~1500 Hz, 1 tick ~667 us static uint8_t stage; static uint8_t currentLed; unsigned int noteLength; unsigned int minrgb, r, g, b; uint8_t i, x = 0; ticks++; // wraps every 43.7 seconds // multiplex LEDS - change selected LED every other interrupt - 1500 / 8 / 2 = ~93.75.5 Hz, each LED on for 1.33 ms if (++stage & 0x01) { RED = 0; GREEN = 0; BLUE = 0; if (++currentLed & 0xF8) { currentLed = 0; P5_4 = 1; // LED_8 off P3 = 0x7E; // LED_1 on, LED_2 to LED_7 off (BLUE also off, but it already was) } else { P3 |= 0x7F; // LED_1 to LED_7 off (BLUE also off, but it already was) if (currentLed == 7) { P5_4 = 0; // LED_8 on } else { P3 &= ~(1 << currentLed); // switch on one of LED_2 to LED_7 } } r = red[currentLed]; g = green[currentLed]; b = blue[currentLed]; pwmStage = 0; // here, how many LEDs to switch off (T2INT will decrement it) if (r > 4) { ++pwmStage; } else { r = 0; } if (g > 4) { ++pwmStage; } else { g = 0; } if (b > 4) { ++pwmStage; } else { b = 0; } i = pwmStage; while (i) { // set up pwmLED[] and pwmReload[] ready for T2INT interrupt(s) and switch LED(s) on --i; if (r) { minrgb = r; x = 0; } else { minrgb = 0xFFFF; } if (g && g < minrgb) { minrgb = g; x = 1; } if (b && b < minrgb) { minrgb = b; x = 2; } minrgb = 0xFFFF - minrgb; if (i == pwmStage - 1) { T2L = minrgb & 0x00FF; T2H = minrgb >> 8; AUXR |= 0x10; // start timer 2 } else { pwmReload[i + 1] = minrgb; } pwmLED[i] = x; if (!x) { if (g) { g -= r; if (g < 5) { g = 5; } } if (b) { b -= r; if (b < 5) { b = 5; } } r = 0; RED = 1; } else if (x == 1) { if (r) { r -= g; if (r < 5) { r = 5; } } if (b) { b -= g; if (b < 5) { b = 5; } } g = 0; GREEN = 1; } else { if (r) { r -= b; if (r < 5) { r = 5; } } if (g) { g -= b; if (g < 5) { g = 5; } } b = 0; BLUE = 1; } } } else if (fade && !(stage & 0x07)) { // fade a LED every 8 interrupts. Results in fade from bright to off in about a second i = (stage & 0x38) >> 3; // select one of eight LEDs to fade red[i] -= red[i] >> 2; green[i] -= green[i] >> 2; blue[i] -= blue[i] >> 2; } else if ((stage & 0x07) == 0x02) { // scan a column of buttons. 3 columns to scan, but operates in 4 phases for simplicity switch (stage & 0x18) { case 0x00: // phase 0 latchedButtons |= prevButtons & ~buttons; prevButtons = buttons; P1_5 = 0; // select column 0 break; case 0x08: // phase 1 buttons &= 0xF8; buttons |= (P1 & 0x1C) >> 2; // read buttons S1, S2, S3 P1_5 = 1; // deslect column 0 P1_6 = 0; // select column 1 break; case 0x10: // phase 2 buttons &= 0xC7; buttons |= (P1 & 0x1C) << 1; // read butons S4, S5, S6 P1_6 = 1; // deselect column 1 P1_7 = 0; // select column 2 break; default: // phase 3 buttons &= 0x3F; buttons |= (P1 & 0x0C) << 4; // read buttons S6, S7 P1_7 = 1; // deselect column 2 } } if (music) { if (!noteTickCount) { // current note has finished music = 0x05; // indicate to T1INT to silence interval between notes, or rest if (!pauseTickCount) { // current pause ends - start next note or rest note = firstNote[currentTune][currentNote]; noteLength = firstLength[currentTune][currentNote]; noteLength <<= tempoShift[currentTune]; if (!note) { // rest pauseTickCount = noteLength; blue[7] = 2000; // pulse LED_7 blue for rests } else { note--; // convert to zero-based i = noteToLeds[note]; if (i & 0x80) { // a white keyboard note green[i & 0x07] = 2000; // pulse one of LED_1 to LED_7 for notes C D E F G A B green } else { // a black keyboard note (sharp or flat) i &= 0x07; red[i] = 2000; i++; red[i] = 2000; // pulse the pair of LEDs adjacent to the note red } pauseTickCount = NOTE_GAP; noteTickCount = noteLength; noteTickCount -= NOTE_GAP; currentTimerNoteVal = timerNoteVal[note]; TH1 = currentTimerNoteVal >> 8; TL1 = currentTimerNoteVal & 0xFF; music = 0x03; TR1 = 1; // enable timer1 } if (++currentNote >= numNotes[currentTune]) { currentNote = 0; if (++currentTune >= NUM_TUNES) { currentTune = 0; } } } else { --pauseTickCount; } } else { --noteTickCount; } } } void T1INT(void) __interrupt (3) __using (2) { // 'note' interrupt on half-cycles of note, when playing unsigned int t1; TR1 = 0; // stop timer while writing to TH1/TL1 registers t1 = TH1 << 8 | TL1; t1 += currentTimerNoteVal; TH1 = t1 >> 8; TL1 = t1 & 0xFF; TR1 = 1; if (music == 0x03) { // actually playing a note BUZZ = !BUZZ; // toggle speaker bit } else { BUZZ = 1; // silence (if they have an active buzzer, don't want it sounding at its own frequency) TR1 = 0; // and timer1 can remain off until the start of the next note } } /*for each T0INT, which, amongst other duties, sometimes multiplexes a LED, this interrupt may occur zero, one, two, or three times. Its job is to switch off one of the RED, GREEN, or BLUE outputs, and, if necessary, restart timer 2 to generate the remaining one or two interrupts. The global variable pwmStage has the value 1, 2, or 3 when this interrupt occurs, and is decremented at the start of this interrupt routine. If it is then non-zero, timer 2 is restarted before the interrupt routine returns. within the interrupt routine the global arrays pwmLED[3] and pwmReload[3] are referred to. pwmLED[pwmStage] is a uint8_t indicating which LED to extinguish: 0=RED, 1=GREEN, 2=BLUE when pwmStage > 0, pwmReload[pwmStage] is an unsigned 16-bit containing the reload value for timer 2. The T0INT is responsible for correctly setting up the arrays and variable, and for switching on the LEDs */ void T2INT(void) __interrupt (12) __using(3) { // switches off red and/or green and/or blue LED for PWM colour control uint8_t b; unsigned int i; AUXR &= 0xEF; // disable timer 2 --pwmStage; b = pwmLED[pwmStage]; if (!b) { RED = 0; } else if (b == 1) { GREEN = 0; } else { BLUE = 0; } if (pwmStage) { i = pwmReload[pwmStage]; T2L = i & 0x00FF; T2H = i >> 8; AUXR |= 0x10; // restart timer 2 } } uint8_t rampUpDown(unsigned int *value, uint8_t amount, uint8_t subtract) { __xdata uint8_t reverse = 0; __xdata int u; u = (int)*value; if (subtract) { u -= amount; if (u < 0) { u = -u; reverse = 1; } } else { u += amount; if (u > 1300) { u = 2600 - u; reverse = 1; } } *value = (unsigned int)u; return reverse; } void patterns(uint8_t elapsed) { // called repeatedly from main() with elapsed containng ticks since previous call __xdata static uint8_t pattern = 0; __xdata static unsigned int timer = 0; // counts down by elapsed, on wrap... __xdata static unsigned int timerReset = 0; // ... adds (resets) to this value and... __xdata uint8_t timerFlag = 0; // ... sets this flag __xdata static uint8_t focussed = 0; // which LED (0 to 7) has the current focus __xdata static uint8_t stage = 0; // used by each pattern to keep track of its current stage __xdata static unsigned int redPWM = 0; __xdata static unsigned int greenPWM = 0; __xdata static unsigned int bluePWM = 0; __xdata static uint8_t redDirection = 0; __xdata static uint8_t greenDirection = 0; __xdata static uint8_t blueDirection = 0; if (!elapsed) return; if (!(ticks & 0x7FFF)) { pattern++; } if (elapsed >= timer) { if (timerReset < elapsed) { // catch startup glitch timerReset = elapsed; } timer += timerReset; timerFlag = 1; } timer -= elapsed; switch (pattern) { case 0: // pseudo random order blinking of saturated colours fade = 0; timerReset = 599; focussed += (uint8_t)(ticks & 0x00FF); stage ^= ((ticks & 0x0038) >> 3); if (timerFlag) { ++focussed; focussed &= 0x07; red[focussed] = stage & 0x01 ? 1250 : 0; green[focussed] = stage & 0x02 ? 1250 : 0; blue[focussed] = stage & 0x04 ? 1250 : 0; } break; case 1: // ramp r,g,b of each LED up and down over a cycle time of about 2s. fade = 0; timerReset = 23; if (timerFlag) { focussed++; focussed &= 7; if (rampUpDown(&(red[focussed]), primes[focussed], redDirection & (1 << focussed))) { redDirection ^= (1 << focussed); } if (rampUpDown(&(green[focussed]), primes[focussed + 8], greenDirection & (1 << focussed))) { greenDirection ^= (1 << focussed); } if (rampUpDown(&(blue[focussed]), primes[focussed + 16], blueDirection & (1 << focussed))) { blueDirection ^= (1 << focussed); } } break; case 2: // same colour rotates around 9 leds, then a colour change. Fade enabled fade = 1; timerReset = 105; if (timerFlag) { focussed++; focussed &= 7; red[focussed] = redPWM; green[focussed] = greenPWM; blue[focussed] = bluePWM; if (++stage >= 9) { stage = 0; redPWM = (ticks & 0x000F) << 7; greenPWM = (ticks & 0x0078) << 4; bluePWM = (ticks & 0x00F0) << 3; } } break; default: pattern = 0; } } void main(void) { uint8_t i; __xdata unsigned int currentTicks, prevTicks = 0; __xdata uint8_t elapsed; P_SW2 |= 0x80; // set EAXFR - unlocks access to XFR extended RAM area special function registers CLKDIV = 0; // set system clock to run at the default rate 18.432 MHz (0 and 1 both divide by 1) P_SW2 &= ~0x80; // clear EAXFR - prohibits access to XFR extended RAM area special function registers (this is default) for (i = 0; i < 8; i++) { red[i] = 0; green[i] = 0; blue[i] = 0; } P1M0 = 0x03; P1M1 = 0x00; // make P1_0 and P1_1 strong outputs P3M0 = 0xFF; P3M1 = 0x00; // make all P3 outputs strong P5M0 = 0x30; P5M1 = 0x00; // make P5_4 and P5_5 strong outputs TMOD = 0x10; // timer 1 mode 1 (16-bit), timer 0 mode 0 (16-bit) TH0 = 0xFC; // reload value +FC00. With 18.432 MHz SYSCLK, T0INTs will occur every 667 us IP = 1 << 3; // set timer 1 higher priority than timer 0 ET0 = 1; // enable timer 0 interrupts ET1 = 1; // enable timer 1 interrupts IE2 |= 0x04; // enable timer 2 interrupts TR0 = 1; // run timer 0 EA = 1; // enable interrupts // currentTune = noteTickCount = pauseTickCount = currentNote = 0; // fade = music = 0; while(1) { EA = 0; currentTicks = ticks; EA = 1; elapsed = currentTicks - prevTicks; prevTicks = currentTicks; switch(mode) { case 0: // displaying LED patterns if (latchedButtons & 0x80) { // button 8 pressed - swap to organ mode latchedButtons &= 0x7F; mode = 1; // organ mode } else if (latchedButtons) { // other button pressed - play music mode = 2; // play music } else { patterns(elapsed); } break; case 1: // organ mode if (latchedButtons & 0x80) { // button 8 pressed - back to patterns mode latchedButtons &= 0; music = 0; mode = 0; // patterns mode } else { fade = 1; if ((buttons & 0x7F) != 0x7F) { // play a note for (i = 0; i < 7; i++) { if (!(buttons & (1 << i))) { if ((i < 6) && !(buttons & (2 << i))) { // sharp or flat note (E# is F) green[i] = 0; green[i + 1] = 0; red[i] = 2000; red[i + 1] = 2000; currentTimerNoteVal = timerNoteVal[buttonToNote[i] + 1]; break; } else { green[i] = 2000; currentTimerNoteVal = timerNoteVal[buttonToNote[i]]; break; } } } if (!music) { noteTickCount = 0xFFFF; TH1 = currentTimerNoteVal >> 8; TL1 = currentTimerNoteVal & 0xFF; music = 0x03; TR1 = 1; // enable timer1 } } else { music = 0; } } break; case 2: // play music if (!music) { latchedButtons = 0; currentTune = 1; for (i = 0; i < 7; i++) { // which button pressed (1 to 7) selects starting tune if (!(buttons & (1 << i))) { currentTune = i; break; } } noteTickCount = pauseTickCount = currentNote = 0; fade = 1; music = 1; } else if (latchedButtons) { latchedButtons = 0; music = 0; mode = 0; // swap back to patterns mode on any button press } break; default: mode = 0; } } }