Exploring STC 8051 Microcontrollers – Coding

Timer 3 as Real Time Clock & Calendar (RTCC)

Since timers can time event precisely, we can use timers for time keeping too. Although there is no dedicated real-time clock-calendar (RTCC) hardware in STC8A8K64S4A12, we can make one by using a timer and some coding. 

Code

 include "STC8xxx.h"
#include "BSP.h"
#include "LCD.c"
 
#define required_time           1000
#define max_tmr_cnt             0xFFFF
#define tmr_val                 (max_tmr_cnt - required_time)
 
#define PM                      0
#define AM                      1
 
#define ETR                     1
#define INC                     2
#define DEC                     3
#define ESC                     4
#define NON                     0
 
#define ETR_button              !P10_get_input
#define INC_button              !P11_get_input
#define DEC_button              !P12_get_input
#define ESC_button              !P13_get_input
 
unsigned char dow = 1;
unsigned char s = 10;
unsigned char hr = 10;
unsigned char min = 10;
unsigned char date = 1;
unsigned char month = 1;
unsigned char toggle = 0;
unsigned char leap_year = 0;
unsigned char ampm = AM;
 
unsigned int ms = 0;
unsigned int year = 2000;
 
void setup(void);
void LCD_print_value(unsigned char x_pos, unsigned char y_pos, unsigned char value);
void LCD_print_int_value(unsigned char x_pos, unsigned char y_pos, unsigned int value);
void display_time(void);
void AM_PM_disp(unsigned char state);
void dow_disp(unsigned char dow);
unsigned char get_buttons(void);
unsigned char set_value(unsigned char x_pos, unsigned char y_pos, signed char value, unsigned char max, unsigned char min, unsigned char type);
void setup_RTC(void);
 
void TMR_3_ISR(void)        
interrupt 19
{
  ms++;
  if(ms > 999)
  {  
    s++;
    ms = 0;
    toggle ^= 0x01;
    P54_toggle;
  }
 
  if(s > 59)
  {
    s = 0;
    min++;
 
    if(min > 59)
    {
      min = 0;
      hr++;
 
      if(hr > 12)
      {
          hr = 1;
      }
 
      if((hr == 12) && (min == 0) && (s == 0))
      {
        ampm ^= 1;
 
        if(ampm == AM)
        {
          date++;
          dow++;
        }
 
        if(dow > 7)
        {
          dow = 1;
        }
 
        if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
        {
          if(date > 31)
          {
            date = 1;
            month++;
          }
        }
 
        else if((month == 4) || (month == 6) || (month == 9) || (month == 11))
        {
          if(date > 30)
          {
            date = 1;
            month++;
          }
        }
 
        else
        {
          if((year % 4) == 0)
          {
            if((year % 100) == 0)
            {
              if((year % 400) == 0)
              {
                leap_year = 1;
              }
 
              else
              {
                leap_year = 0;
              }
            }
 
            else
            {
              leap_year = 1;
            }
          }
 
          else
          {
              leap_year = 0;
          }
 
          if((leap_year) && (date > 29))
          {
             date = 1;
             month++;
          }
 
          else if((!leap_year) && (date > 28))
          {
             date = 1;
             month++;
          }
        }
 
        if(month > 12)
        {
          month = 1;
          year++;
        }
      }
    }
  }
  
  clear_TMR_3_overflow_flag;
}
 
void main(void)
{
  setup();
 
  while(1)
  {
    if(get_buttons() == ESC)
    {
        while(get_buttons() == ESC);
        setup_RTC();
    }
    else
    {
        display_time();
    }
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
  P10_input_mode;
  P11_input_mode;
  P12_input_mode;
  P13_input_mode;
  
  P54_push_pull_mode;
 
  LCD_init();
  LCD_clear_home();
 
  TMR3_setup(TMR3_sysclk, TMR3_clk_prescalar_12T, TMR3_no_clk_out);
  TMR3_load_counter_16(tmr_val);
  TMR3_start;
  _enable_TMR_3_interrupt;
  _enable_global_interrupt;
}
 
void LCD_print_value(unsigned char x_pos, unsigned char y_pos, unsigned char value)
{
  LCD_goto(x_pos, y_pos);
  LCD_putchar((value / 10) + 0x30);
  LCD_goto((x_pos + 1), y_pos);
  LCD_putchar((value % 10) + 0x30);
}
 
void LCD_print_int_value(unsigned char x_pos, unsigned char y_pos, unsigned int value)
{
  unsigned char temp = 0x00;
 
  temp = (value / 100);
  LCD_print_value(x_pos, y_pos, temp);
 
  temp = (value % 100);
  LCD_print_value((x_pos + 2), y_pos, temp);
 
}
 
void display_time(void)
{
  LCD_print_value(2, 0, hr);
  LCD_print_value(5, 0, min);
  LCD_print_value(8, 0, s);
 
  switch(toggle)
  {
    case 1:
    {
      LCD_goto(4, 0);
      LCD_putchar(':');
      LCD_goto(7, 0);
      LCD_putchar(':');
      break;
    }
    default:
    {
      LCD_goto(4, 0);
      LCD_putchar(' ');
      LCD_goto(7, 0);
      LCD_putchar(' ');
      break;
    }
  }
 
  AM_PM_disp(ampm);
  dow_disp(dow);
 
  LCD_print_value(6, 1, date);
 
  LCD_goto(8, 1);
  LCD_putchar('.');
  LCD_print_value(9, 1, month);
 
  LCD_goto(11, 1);
  LCD_putchar('.');
  LCD_print_int_value(12, 1, year);
}
 
void dow_disp(unsigned char dow)
{
  LCD_goto(0, 1);
 
  switch(dow)
  {
    case 1:
    {
      LCD_putstr("MON");
      break;
    }
    case 2:
    {
      LCD_putstr("TUE");
      break;
    }
    case 3:
    {
      LCD_putstr("WED");
      break;
    }
    case 4:
    {
      LCD_putstr("THR");
      break;
    }
    case 5:
    {
      LCD_putstr("FRI");
      break;
    }
    case 6:
    {
      LCD_putstr("SAT");
      break;
    }
    case 7:
    {
      LCD_putstr("SUN");
      break;
    }
    default:
    {
      LCD_putstr("   ");
      break;
    }
  }
}
 
void AM_PM_disp(unsigned char state)
{
  LCD_goto(12, 0);
 
  switch(state)
  {
    case AM:
    {
      LCD_putstr("AM");
      break;
    }
    default:
    {
      LCD_putstr("PM");
      break;
    }
  }
}
 
unsigned char get_buttons(void)
{
  if(ETR_button)
  {
    return ETR;
  }
 
  else if(INC_button)
  {
    return INC;
  }
 
  else if(DEC_button)
  {
    return DEC;
  }
 
  else if(ESC_button)
  {
    return ESC;
  }
 
  else
  {
    return NON;
  }
}
 
unsigned char set_value(unsigned char x_pos, unsigned char y_pos, signed char value, unsigned char max, unsigned char min, unsigned char type)
{
  unsigned char tgl = 0;
 
  while(1)
  {
    tgl ^= 0x01;
    delay_ms(90);
 
    if(get_buttons() == INC)
    {
        value++;
    }
 
    if(value > max)
    {
        value = min;
    }
 
    if(get_buttons() == DEC)
    {
        value--;
    }
 
    if(value < min)
    {
        value = max;
    }
 
    switch(type)
    {
      case 1:
      {
        switch(tgl)
        {
          case 1:
          {
            LCD_print_value(x_pos, y_pos, value);
            break;
          }
 
          default:
          {
            LCD_goto(x_pos, y_pos);
            LCD_putstr("  ");
            break;
          }
        }
 
        break;
      }
 
      case 2:
      {
        switch(tgl)
        {
          case 1:
          {
            AM_PM_disp(value);
            break;
          }
 
          default:
          {
            LCD_goto(12, 0);
            LCD_putstr("  ");
            break;
          }
        }
 
        break;
      }
 
      default:
      {
        switch(tgl)
        {
          case 1:
          {
            dow_disp(value);
            break;
          }
          default:
          {
            dow_disp(0);
            break;
          }
        }
        break;
      }
    }
 
    if((get_buttons() == ETR) && (tgl == 1))
    {
       return value;
    }
  };
}
 
void setup_RTC(void)
{
  unsigned int yr1 = 0;
  unsigned int yr2 = 0;
 
  TMR3_stop;
  _disable_TMR_3_interrupt;
 
  yr1 = (year / 100);
  yr2 = (year % 100);
 
  hr = set_value(2, 0, hr, 12, 1, 1);
  delay_ms(200);
  min = set_value(5, 0, min, 59, 0, 1);
  delay_ms(200);
  s = set_value(8, 0, s, 59, 0, 1);
  delay_ms(200);
  ampm = set_value(12, 0, ampm, 1, 0, 2);
  delay_ms(200);
  dow = set_value(0, 1, dow, 7, 1, 0);
  delay_ms(200);
  date = set_value(6, 1, date, 31, 1, 1);
  delay_ms(200);
  month = set_value(9, 1, month, 12, 1, 1);
  delay_ms(200);
  yr1 = set_value(12, 1, yr1, 99, 0, 1);
  delay_ms(200);
  yr2 = set_value(14, 1, yr2, 99, 0, 1);
  delay_ms(200);
 
  year = ((yr1 * 100) + yr2);
 
  if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
  {
    if(date > 31)
    {
        date = 1;
    }
  }
 
  else if((month == 4) || (month == 6) || (month == 9) || (month == 11))
  {
    if(date > 30)
    {
        date = 1;
    }
  }
 
  else
  {
    if((year % 4) == 0)
    {
      if((year % 100) == 0)
      {
          if((year % 400) == 0)
          {
              leap_year = 1;
          }
 
          else
          {
              leap_year = 0;
          }
      }
 
      else
      {
          leap_year = 1;
      }
    }
 
    else
    {
        leap_year = 0;
    }
 
    if((leap_year) && (date > 29))
    {
         date = 1;
    }
 
    else if((!leap_year) && (date > 28))
    {
         date = 1;
    }
  }
 
  ms = 0;
  TMR3_setup(TMR3_sysclk, TMR3_clk_prescalar_12T, TMR3_no_clk_out);
  TMR3_load_counter_16(tmr_val);
  TMR3_start;
  _enable_TMR_3_interrupt;
}

Schematic

Explanation

For making a RTCC, we would need a display and a set of buttons for displaying and setting time respectively. The system clock in this example is set to 12MHz.

 CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
P10_input_mode;
P11_input_mode;
P12_input_mode;
P13_input_mode;
  
P54_push_pull_mode;
 
LCD_init();
LCD_clear_home();

Timer 3 is used in example. It is driven with the 12MHz system clock and this clock is precaled by 12. Thus, the timer is ticking at 1MHz speed.

 TMR3_setup(TMR3_sysclk, TMR3_clk_prescalar_12T, TMR3_no_clk_out);
TMR3_load_counter_16(tmr_val);
TMR3_start;
_enable_TMR_3_interrupt;
_enable_global_interrupt;

Lastly, the timer is started with its interrupt enabled.

In order to ensure accuracy of timing the following should be maintained as much as possible:

  • Clock source must be as precise as possible. I recommend using a temperature-compensated crystal oscillator (TCXO).
  • Precalar should be as less as possible. This is not possible here with STC8A8K64S4A12 though due to hardware limitations.
  • Accuracy can be increased by synchronizing time with GPS at periodic interval. This can be achieved by adding extra codes and synchronizing with a GPS receiver or NTP server.

Since the timer is now ticking at 0.001ms rate and 1 second equals 1000ms, a variable called “ms” is incremented at every timer overflow interrupt. When this get larger than 999, one second is registered. In similar way, minute, hour, day, month and year are increased. The process is just like a software timer implementation. At the end of the interrupt, timer overflow flag is cleared.

 void TMR_3_ISR(void)        
interrupt 19
{
  ms++;
  if(ms > 999)
  {  
    s++;
    ms = 0;
    toggle ^= 0x01;
    P54_toggle;
  }
 
  if(s > 59)
  {
    s = 0;
    min++;
 
    if(min > 59)
    {
      min = 0;
      hr++;
 
      if(hr > 12)
      {
          hr = 1;
      }
 
      if((hr == 12) && (min == 0) && (s == 0))
      {
        ampm ^= 1;
 
        if(ampm == AM)
        {
          date++;
          dow++;
        }
 
        if(dow > 7)
        {
          dow = 1;
        }
 
        if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) \\
            || (month == 12))
        {
          if(date > 31)
          {
            date = 1;
            month++;
          }
        }
 
        else if((month == 4) || (month == 6) || (month == 9) || (month == 11))
        {
          if(date > 30)
          {
            date = 1;
            month++;
          }
        }
 
        else
        {
          if((year % 4) == 0)
          {
            if((year % 100) == 0)
            {
              if((year % 400) == 0)
              {
                leap_year = 1;
              }
 
              else
              {
                leap_year = 0;
              }
            }
 
            else
            {
              leap_year = 1;
            }
          }
 
          else
          {
              leap_year = 0;
          }
 
          if((leap_year) && (date > 29))
          {
             date = 1;
             month++;
          }
 
          else if((!leap_year) && (date > 28))
          {
             date = 1;
             month++;
          }
        }
 
        if(month > 12)
        {
          month = 1;
          year++;
        }
      }
    }
  }
  
  clear_TMR_3_overflow_flag;
}

In the main, either we can set time or read it.

 if(get_buttons() == ESC)
{
    while(get_buttons() == ESC);
    setup_RTC();
}
else
{
    display_time();
}

Demo

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

Related Posts

17 comments

  • hello.
    this is a very good effort to document all and still share with us. thank you very much.
    I have one doubt . which programming tool are you using ?

  • Hi, I am trying to understand the STC15w408as chip, and found this site after weeks of searching for something that sets the output of the GPIO pins to a different state. I have a the 28 pin stc15w and have connected it up with a FTDI board and can write to it using PlatformIO. The thing is, the GPIO ports if just switched on or do a reset they are in the HIGH state and I am trying to make them LOW when you do a reset.
    Is your BSP code doing this and for what port or GPIO pin is it setting? I could change your P52 and P55 in your SETUP to the GPIO pins on my development board but not under standing the BSP Code.
    Wonder if you get this post? but any help would be gratefully received.

  • Hi,
    How Purchase the development board. Please,give the purchase link for this Development board.

  • How To read and write string data using IAP into memory

    • void IAP_erase(unsigned int address)
      {
      IAP_CONTR = 0x80; //?? IAP
      IAP_TPS = 12;
      // IAP_CONTR = IAP_WT;
      IAP_CMD = IAP_erase_command;
      IAP_address(address);
      IAP_trigger;
      _nop_();
      _nop_();
      _nop_();
      IAP_clear;
      }

      void IAP_send_string(unsigned int uc_send_addr,unsigned char *uca_send_string,unsigned int uc_number_of_bytes)
      {
      unsigned int buff_cntr=0;
      do
      {
      IAP_CONTR = 0x80; //?? ISP/IAP ??
      IAP_TPS = (unsigned char)(11509200 / 1000000L); //??????
      IAP_CMD = IAP_write_command;

      // IAP_CMD = IAP_write_command;
      IAP_ADDRH = uc_send_addr / 256; //??????(??????????????)
      IAP_ADDRL = uc_send_addr % 256; //??????
      IAP_DATA = uca_send_string[buff_cntr]; //???? ISP_DATA,????????????
      IAP_trigger;//IAP_TRIG();
      _nop_();
      _nop_();
      _nop_();
      uc_send_addr++;
      // uca_send_string++;
      buff_cntr++;

      IAP_clear;
      delay_ms(8);
      }while(–uc_number_of_bytes);
      }

      void IAP_read_string(unsigned int uc_read_addr,unsigned char *data_read,unsigned int uc_number_of_bytes)
      {
      unsigned int buff_cntr=0;
      do{
      IAP_CONTR = 0x80; //?? ISP/IAP ??
      IAP_TPS = (unsigned char)(11059200 / 1000000L); //??????
      IAP_CMD = IAP_read_command;

      // IAP_CMD = IAP_read_command;
      IAP_ADDRH = uc_read_addr / 256; //??????(??????????????)
      IAP_ADDRL = uc_read_addr % 256; //??????
      IAP_trigger;//IAP_TRIG(); //?? 5AH,?? A5H ? ISP/IAP ?????,
      //???????
      //?? A5H ?, ISP/IAP ?????????
      //CPU ?? IAP ???,?????????
      _nop_();
      _nop_();
      _nop_();
      data_read[buff_cntr] = IAP_DATA; //???????
      uc_read_addr++;
      // data_read++;
      buff_cntr++;

      IAP_clear;
      delay_ms(8);
      }while(–uc_number_of_bytes);
      }

      stores only last byte to all bytes of flash memory sector… memory sector selected is 0xF600

  • Hi, I am using STC MCU since 10 years. Tech support is ZERO. but they are low cost, very stable. Now I have a problem when the chip that I used is obsolete. Now start to use STC8C2K64S4-28I-LQFP32 but no stc8Cxx.h file, I am using stc8Hxx.h file which compiles but in some stage freeze, the existing firmware. With stc8hxx.h file I can compile STC8F2K64S4-28I-LQFP32 and works not bad
    .
    I wrote them many times for the stc8Cxx.h file never got answer. Where Can I find that file?
    Thank you

  • Hi. Can you explain how to use I2C in the slave mode ?

    • I tried STC8G1K08A i2c in slave mode. Doesn’t work (no response). It does not enter interrupt, even on a start condition (everything according to the code in the documentation). I also tried master mode – it works.

  • Thanks for these tutorials. I’m getting back into STCmicro coding now, having left them alone for the past several years. Back then I only used the STC89C52RC (and C54RD) but this time I’m also using the more powerful STC15 and STC8 types. Your blogs provide a wealth of useful information.

  • Hello,

    You have done great job with all these tutorials. I am an electronics engineer trying to learn some new stuff. I am located in Greece , Europe and I would like to purchase the development board that you are using and download some datasheets in English if possible but I cannot find them anywhere. Could you please help me?

  • i always get excited when you release new tutorials ,you are really doing a great job i wish i could write code and develop libraries like you.

  • Well, this is very nice and thorough tutorial indeed, many thanks!
    Unfortunately I doubt there is good any reason to learn the STC platform beyond curiosity.
    The STC 8051, although pretty evolved from the original 8051 ISA, does not offer anything crucial to justify the relatively high price of these micros and development tools along with certain cumbersomeness of this ancient platform.
    They simply can not compete even with the legacy Cortex M0 in any way. I am even not aware about any affordable debugger/emulator for them.
    All in all, I would never recommend anybody to start learning/using any 8051 without some very good reason to do so.

Leave a Reply to ceptimus Cancel reply

Your email address will not be published. Required fields are marked *