Exploring STC 8051 Microcontrollers – Coding

Wakeup Timer and Low Power Mode

Wakeup timer is an interesting feature that is not seen in most microcontrollers. It allows us to automatically wakeup a microcontroller from sleep or low power mode after a fixed interval. It is especially useful in cases where we need to take periodic short measurements and then keep our device in low power state to conserve power. A good example is a refrigerator temperature controller. The temperature controller would periodically check interior temperature and decide when to operate the compressor for cooling. Since the compressor would work for short times, the control system of the refrigerator need not to be kept active all the times. Periodic short measurements are all that are needed. This, in turn, would help in energy saving. This tactic becomes even more vital if refrigerator of the discussion is battery-operated.    

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
void setup(void);
 
    
void main(void)
{
    unsigned char i = 0;
    setup();
    
    while(1)
    {
      WKT_disable;
      
      for(i = 0; i <= 15; i++)  
      {
        P55_toggle;
        delay_ms(100);
      }
      
      WKT_enable;
      Go_to_Power_Down_State;
    };
}
 
void setup(void)
{
    CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
    
    P55_open_drain_mode;
    
    WKT_set_interval(9000);
}

Schematic

Explanation

In this example, two features of STC8A8k64S4A12 have been shown. Firstly, low power mode is demoed and secondly, the wakeup timer.

First, let’s see how the wakeup timer is set. Wakeup timer is clocked with internal 32kHz oscillator. This keeps it independent from the system clock. However, the internal 32KHz oscillator is not very accurate and so there could be some deviations. To set the wakeup timer, all we need to do is to load its internal 15-bit counter with some value so that we get our desired wakeup time. The wakeup time is calculated according to the following formula:

In the code, the counter value is set to 9000. This would roughly give a wake-up time of 4.5s once the core goes to low-power state.

 WKT_set_interval(9000); 

Note that although the counter is loaded the wake-up time is not started. This is so because we want to wake the micro after it enters low-power mode.

In the main loop, the wake-up timer is disabled. The onboard LED is flashed a few times to indicate that the code in the main loop is running. After flashing the LED, the wake-up timer is started and low-power mode is entered.

 WKT_disable;
      
for(i = 0; i <= 15; i++)  
{
   P55_toggle;
   delay_ms(100);
}
      
WKT_enable;
Go_to_Power_Down_State;

Entering low power mode is easy. Just add the following command:

 Go_to_Power_Down_State; 

After entering low-power mode, the LED seems to freeze and there seems no activity at all for some time. After about 4.5s, the LED repeats flashing just like before. This time the LED flashing not only indicates main loop code execution but it also indicates that the wake timer ran and woke up the CPU core from low-power sleep mode.

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

7 comments

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

  • 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

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