// hourglass kit with 57 LEDs driven by STC15W204 or STC15W201 // this is the 'egg timer' code that allows setting a one to ten minute target time (short press to change) // when started (long press), shows a 'trickling sand' animation for the target time // when the time is elapsed, flashes all LEDs // a button press then returns to the flashing 'time setting' display // a button press while the 'trickling' animation is running also returns to the 'time setting' display // // ceptimus November 2024. This code has a few ugly parts, in order to squeeze the flash upload size down to under 1k to suit the STC15W201 variant // compile with sdcc eggtimer.c // for the STC15W201, you must force the compiler to produce slightly more compact code using: sdcc --acall-ajmp eggtimer.c (this works for 204 too, as long as code < 2k) // flash to chip using stcgal -p /dev/ttyUSB0 -a -t 11059 eggtimer.ihx // (omit -p if you don't have an auto-powercycle feature on your programming adapter) // // LEDs are connected in a 5x6 matrix with two LEDs of opposite polarity at each node: so could do 5x6x2 = 60 LEDs, but the last three are missing. // LED numbering and multiplex stage: // // -P10 +P10 -P11 +P11 -P12 +P12 -P13 +P13 -P14 +P14 -P15 +P15 stage // +P30 0 10 20 30 40 50 0 (P30 is also RXD) // -P30 5 15 25 35 45 55 1 // // +P31 1 11 21 31 41 51 2 (P31 is also TXD) // -P31 6 16 26 36 46 56 3 // // +P33 2 12 22 32 42 52 4 // -P33 7 17 27 37 47 5 // // +P36 3 13 23 33 43 53 6 // -P36 8 18 28 38 48 7 // // +P37 4 14 24 34 44 54 8 // -P37 9 19 29 39 49 9 // // The low-numbered LEDs are the bottom of the hourglass, using the standard kit-supplied program. // // Push button on P32. P30 is also RXD, P31 also TXD. P54(RST) and P55 unused. // // Physical layout: // // 50 0 // 44 7 // 51 39 13 1 // 45 35 18 8 // 52 40 32 22 14 2 // 46 36 30 25 19 9 // 53 41 33 29 28 27 23 15 3 // 47 37 31 26 20 10 // 54 42 34 24 16 4 // 48 38 21 11 // 55 43 17 5 // 49 12 // 56 6 #include<8052.h> __sfr __at (0x8E) AUXR; __sfr __at (0x91) P1M1; __sfr __at (0x92) P1M0; __sfr __at (0xB1) P3M1; __sfr __at (0xB2) P3M0; __sfr __at (0xD6) T2H; __sfr __at (0xD7) T2L; __sfr __at (0xAF) IE2; #define uint8_t unsigned char #define uint16_t unsigned int volatile uint8_t ticks; // increments at 60 Hz, for timing delays, etc. volatile uint8_t columns[10]; volatile uint8_t pushButton; // interrupt routine, besides multiplexing the LEDs, debounces the push button and sets this flag on a fresh (short) press volatile uint8_t longPress; // ... or this flag on a long button press __code uint8_t port3bitTable[5] = {0,1,3,6,7}; __code uint8_t horizStarts[] = {0,7,13,18,22,25,27,28,29,30,32,35,39,44,50,57}; // first LED on each hourglass horizontal row __code uint8_t dropOrder[] = {53,54,52,55,51,56,50,46,47,45,48,44,49,41,42,40,43,39,36,37,35,38,33,34,32,30,31}; // where LEDs disappear from, when falling, in order void T2INT(void) __interrupt (12) __using (1) { // timer 2 interrupt set to happen at 3600 Hz, so overall multiplex cycle at 360 Hz static uint8_t pushButtonDebounce; static uint8_t stage; // multiplex LEDs (ten rows of 6 columns) static uint8_t tickCounter; static uint16_t longPressTimer; uint8_t m0, m1, p; if (!--tickCounter) { // bump tickCounter at 60 Hz ++ticks; tickCounter = 60; } if (!P3_2) { // if button pressed if (++longPressTimer > 2700) { // if button held down for > 0.75 secs longPress = 1; pushButtonDebounce = 0; } else { pushButtonDebounce = 180; // 50ms } } else { longPressTimer = 0; if (pushButtonDebounce) { // run timer when button is released if (!--pushButtonDebounce) { pushButton = 1; // short button press flag is set 50ms after button released } } } m0 = columns[stage] & 0x3F; m1 = m0 ^ 0x3F; // for lit bits m0=1, m1=0 (push-pull); for unlit bits m0=0; m1=1 (input only); P1_6 and P1_7 remain m0=0,m1=0 (standard quasi-bidirectional) p = stage & 0x01 ? m0 : m1; P1M0 = 0x00; // set all 'column' (P1) I/O to input only P1M1 = 0x3F; P3M0 = 0x00; // set all 'row' (P3) I/O input only P3M1 = 0xCB; P1 = p; // preset column output bits high or low P1M0 = m0; P1M1 = m1; // switch lit column bits to push-pull mode, other bits remain input only m0 = 0x01 << port3bitTable[stage >> 1]; m1 = m0 ^ 0xCB; // will set selected I/O to push-pull mode p = (stage & 0x01) ? m1 : m0; // output bits high or low P3 = p | 0x04; // preset row output bit high or low; P3_2 (push button input) always high P3M0 = m0; P3M1 = m1; // set push-pull mode for that row output bit if (!stage) { // each interrupt selects next of 10 rows. stage = 9; } else { --stage; } } void setLED(uint8_t n, uint8_t on) { // n:0 to 56; on:true or false uint8_t mask = 0x01; while (n >= 10) { n -= 10; mask <<= 1; } n = n > 4 ? ((n - 5) << 1) | 1 : n << 1; // row if (on) { columns[n] |= mask; } else { columns[n] &= ~mask; } } void setLEDrange(uint8_t n, uint8_t v, uint8_t min, uint8_t max) { setLED(n, v >= min && v < max); } void main(void) __using (0) { uint8_t mode = 0; // 0 = 'setting timer' display, 1 = timer finished (flashing display), other = 'trickling hourglass' display uint8_t minutes = 5; // how many minutes the timer will run - defaults to five at startup uint8_t i, j, k; uint8_t previousTicks = 0; uint8_t timer1 = 0; // flashes LEDs at 1 Hz for 'setting' and 'finished' displays, works the three centre 'trickling' LEDS for 'timer running' display uint16_t timer2 = 0; // used for interval between each grain dropping, in the 'timer running' display uint16_t ticksPerGrain = 0; // this is set to (129 * minutes). There are 27 grains to drop in 28 stages. 129 X 28 = 3612 ticks, which is aproximately one minute AUXR &= 0xE3; // bit 4, T2R is low (Timer 2 not running); bit 3, T2_C/T low so timer counter 2 works as timer; bit 2 T2x12 is low, so clock source for timer 2 is FOSC/12 // when T2R == 0 (timer 2 not running), writes to T2H and T2L actually write to the hidden registers RL_T2H and RL_T2L (the 16-bit auto-reload value) T2L = 0x00; T2H = 0xFF; // count from 0xFF00 to 0x10000 (256 counts) before interrupt, so interrupt frequency = FOSC/12/256 = 11059200/12/256 = 3600 Hz AUXR |= 0x10; // run timer 2 IE2 |= 0x04; // set bit2 ET2 to enable interrupts from timer 2 EA = 1; // enable interrupts while(1) { if (previousTicks != ticks) { previousTicks = ticks; timer1++; timer2++; } if (mode < 2) { // 'setting timer' or 'finished' 'displays if (pushButton) { if (!mode && ++minutes > 10) { minutes = 1; } pushButton = mode = timer1 = 0; } k = mode ? 16 : minutes; for (i = 0; i < 16; i++) { // show minutes by number of horizontal rows (flashing at 1 Hz) or when mode == 1 (finished) flash all rows for (j = horizStarts[i]; j < horizStarts[i + 1]; j++) { setLED(j, i < k ? timer1 < 30 ? 1 : 0 : 0); } } if (timer1 >= 60) { timer1 -= 60; } if (longPress) { for (i = 0; i < 57; i++) { // empty bottom half of hourglass, fill the top half with sand setLED(i, i > 29); } mode = 2; // modes 2 to 29 inclusive, each drop one LED from dropOrder[mode - 3] to 56 - that position ticksPerGrain = ((uint16_t)minutes << 7) + minutes; // minutes * 129 timer2 = 0; timer1 = longPress = 0; } } else { // running the timer - 'trickling sand' display setLEDrange(27, timer1, 75, 105); setLEDrange(28, timer1, 45, 75); setLEDrange(29, timer1, 15, 45); if (timer1 >= 105) { timer1 -= 105; } if (pushButton) { // pushing the button goes back to 'setting' mode' pushButton = longPress = mode = timer1 = 0; } else if (timer2 >= ticksPerGrain) { timer2 -= ticksPerGrain; ++mode; if (mode == 30) { // timer finished timer1 = longPress = 0; mode = 1; } else { // drop one grain i = dropOrder[mode - 3]; setLED(i, 0); // remove grain from top setLED(56 - i, 1); // add grain to bottom } } } } }