Exploring STC 8051 Microcontrollers – Coding

Watchdog Timer (WDT)

As with any other microcontroller, reset has many sources and watchdog timer is one of them. STC8A8K64S4A12’s watchdog timer is a typical independent watchdog timer that will reset the MCU core in the event of an unforeseen software failure. The watchdog timer of STC8A8K64S4A12 has no alternative use. In some other microcontroller families like TI MSP430s, watchdog timer can be used like an ordinary timer.

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
void setup(void);   
 
void main(void)
{
    unsigned char i = 0;
  
    setup();
    
    while(1)
    {
        P11_toggle;
        
        if(P52_get_input == FALSE)
        {
            for(i = 0; i <= 9; i++)
            {
              P10_toggle;
              delay_ms(100); 
            }
            
            while(1);
        }
        
        delay_ms(200);   
        WDT_reset;        
    };
}
 
void setup(void)
{
    CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
    
    P52_input_mode;
    P52_pull_up_enable;
  
    P10_push_pull_mode;
    P11_push_pull_mode;  
 
    WDT_setup(WDT_continue_counting_in_idle_mode, WDT_div_factor_32);
}

Schematic

Explanation

For demoing WDT functionality, a simple LED blinking code is used. Two LEDs are used – one with pin P1.0 and the other with P1.1. P5.2 pin is used for a push button and the system clock is set at 12MHz.

 CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
    
P52_input_mode;
P52_pull_up_enable;
  
P10_push_pull_mode;
P11_push_pull_mode;  

STC8A8K64S4A12’s WDT is dependent on system clock and this is an old concept. I say it is primitive concept because many microcontrollers nowadays use a totally independent clock for WDT. Having an independent clock for WDT reduces the chance of WDT getting affected by issues with main or system clock.

WDT is setup according to the following formula:

From the formula, we can clearly see that WDT overflow timing is dependent on WDT prescalar and system clock. The following table shows some typical values.

In this code, a prescalar value of 32 is set with 12MHz system clock. Thus, the WDT will reset after about one second if not refreshed or reset.

 WDT_setup(WDT_continue_counting_in_idle_mode, WDT_div_factor_32); 

In the main loop, a LED connected to pin P1.1 is toggled every 200ms while P1.0 LED is kept turned on. The WDT is kept resetting continuously under this condition and it does not overflow. 

 P11_toggle;
        
if(P52_get_input == FALSE)
{
    for(i = 0; i <= 9; i++)
    {
        P10_toggle;
        delay_ms(100); 
    }
            
    while(1);
}
        
delay_ms(200);   
WDT_reset;      

If, however, P5.2 push button is pressed, the code enters a new loop which simulates an undesired condition. In this condition the state of P1.1 LED seems to get stuck. P1.0 LED flashes briefly, suggesting the code entered undesired loop and WDT is not being reset. Thus, after one second the CPU resets and P1.1 LED again starts to toggle. This suggests that the WDT has triggered a reset due to its overflow.

Lastly before signing off from this topic, please note the following setting in programmer GUI. These can also be set in the GUI too apart from coding.

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

2 comments

  • 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 *