Delay On Timer 1-99 seconds

Wichit Sirichote, wichit.sirichote@gmail.com

Simple delay circuit, 1-99 seconds is built with the 8-pin MCU, STC8GK08 chip. The preset delay 1-99 is saved in EEPROM memory at location 0. The output delay on at P3.3 is actived high when time is over. The LED, D1 is a ongoing timer indicator, blinks every one second. S1 is optional test pin. When pressed, will restart the timer.

The source code is shown below.

/*
  Delay On Timer 1-99 seconds           
 (C) 2025 W.SIRICHOTE
 MCU: STC8G1K08 8-pin, 6MHz
 EEPROM settings when program the code and eeprom
   0x0000 0x05 5 seconds 0-99 enter in BCD
 OUTPUT_H P3.3 delay ON active high
   OUTPUT_L P3.2 delay OFF active low
   LED P3.1 tick LED blink every one second
   TEST P3.0 TEST button, active low restart
*/
           
// STC SFR declarations
sfr unsigned short P3M1 absolute 0xB1;
             sfr unsigned short P3M0 absolute 0xB2;
             sfr unsigned short P1M1 absolute 0x91;
             sfr unsigned short P1M0 absolute 0x92;
             sfr unsigned short P5M1 absolute 0xC9;
             sfr unsigned short P5M0 absolute 0xCA;
sfr unsigned short P50 absolute 0xC8;
sfr unsigned short ADC_CONTR absolute 0xBC;
             sfr unsigned short ADC_RES absolute 0xBD;
             sfr unsigned short ADC_RESL absolute 0xBE;
             sfr unsigned short ADCCFG absolute 0xDE;
             sfr unsigned short IAP_CONTR absolute 0xC7;
             sfr unsigned short PSW2 absolute 0xBA;
             sfr unsigned short IE absolute 0xA8;
sfr short IAP_DATA absolute 0xC2;
             sfr short IAP_ADDRH absolute 0xC3;
             sfr short IAP_ADDRL absolute 0xC4;
             sfr short IAP_CMD absolute 0xC5;
             sfr short IAP_TRIG absolute 0xC6;
 
unsigned int count, i;
unsigned short no_of_second=1;
           
char IapRead(int addr);
sbit TEST at P3.B0;
             sbit LED at P3.B1;
sbit OUTPUT_H at P3.B3; // delay on
             sbit OUTPUT_L at P3.B2; // delay off
 void port_setup()
   {
           
 P3M0 = 0xFF;
   P3M1 = 0x00; // P3.3 PUSH-PULL mode
           
 P1M0 = 0x02; // P1.1 is push-pull mode
   P1M1 = 0xC0; // P1.6 and P1.7 are ADC6 and ADC7
 P5M0 = 0;
   P5M1 = 0;
 P3=0x07;
 P50 =0; // P5.5 = 0
 Delay_ms(10);
 
 }
           
 unsigned char bin2bcd(unsigned val)
             {
   return val + 6 * (val / 10);
   }
           
unsigned char bcd2bin(unsigned val)
             {
   return val - 6 * (val >> 4);
   }
void IapIdle()
             {
   IAP_CONTR = 0;
   IAP_CMD = 0;
   IAP_TRIG = 0;
   IAP_ADDRH = 0x80;
   IAP_ADDRL = 0;
   }
#define WT_6M 0x84
char IapRead(int addr)
             {
   char dat;
 IAP_CONTR = WT_6M;
   IAP_CMD = 1;
   IAP_ADDRL = addr;
   IAP_ADDRH = addr >> 8;
   IAP_TRIG = 0x5a;
   IAP_TRIG = 0xa5;
   Delay_us(10);
   dat = IAP_DATA;
   IapIdle();
 return dat;
             }
 void main(void)
   {
 port_setup();
               
   OUTPUT_H = 0;
   OUTPUT_L = 1;
 no_of_second = IapRead(0x0000); // read clock format from eeprom, 0x12 or 0x24
   count = bcd2bin(no_of_second);
   
           
 for(i=0; i< count; i++)
   {
 Delay_ms(8000);
 LED=0;
   Delay_ms(10);
   LED=1;
           
 }
 OUTPUT_H = 1;
   OUTPUT_L = 0;
 TEST =1;
 while(P3&1)
   continue;
   IAP_CONTR = 0x20; // trigger software reset
           
}


Main code, after port setup, set the initial outputs, will read the first byte of EEPROM. Convert it from BCD to bin, the preset delay 1-99 seconds. Then running the number of delay loop. When complete, set the output bit, keep reading P3.0, if it was low then restart the timer using software reset.

Using the IAP loader

Set MCU type to STC8G1K08A-8PIN. Input IRC frequency to 6 MHz. Then open the code file.

Edit the preset delay value, 1-99 in EEPROM window. The example shows 10 in BCD number. Value can be 01 to 99.

To program the chip, click Download/Program.

 

Download Schematic, source code delay.c , code file.

More information, please contact wichit.sirichote@gmail.com


Main page

Last updated, March 2025