Exploring STC 8051 Microcontrollers – Coding

Timer 1 as a Counter

Apart from timing events, the other role of timers is to count pulses. Pulse counting has number of applications like motor speed control, object counting, frequency measurements, encoder counting, etc.

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
void setup(void);
 
void main(void)

  setup();
  
  while(1)
  {
    if(TMR1_get_counter() >= 32768)
    {
      P55_high;
    }
    else
    {
      P55_low;
    }    
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
  
  P34_push_pull_mode;
 
  P35_input_mode;
  
  P55_open_drain_mode;
  
  TMR1_setup(TMR1_16_bit_non_auto_reload, \
             TMR1_ext_T1_P35_clk, \
             TMR1_clk_prescalar_12T, \
             TMR1_int_gate, \
             TMR1_P34_clk_out);
  
  TMR1_load_counter_8(0x00, 0x00);
  
  TMR1_start;
}

Schematic

Explanation

This example is same as the previous timer example, the only difference is the clock source of the timer involved here. Timer 1 is used and it is externally clocked by using a signal generator. In this example, the blinking rate of the onboard LED changes with the clock speed of the timer.

First let’s see how the timer is set up.

 TMR1_setup(TMR1_16_bit_non_auto_reload, \
           TMR1_ext_T1_P35_clk, \
           TMR1_clk_prescalar_12T, \
           TMR1_int_gate, \
           TMR1_P34_clk_out);
  
TMR1_load_counter_8(0x00, 0x00);
  
TMR1_start;

Here, we can, firstly, see that the timer is set in non-auto reload mode. This means that timer needs periodic reloads after overflow or else it will restart counting from 0. In our case, this won’t matter because we want the timer to count from 0 after overflow. Secondly, we can see that the timer is externally clocked and this is achieved by feeding clock pulses to P3.5 (T1) GPIO pin. Since the timer is externally clocked, it doesn’t matter what system clock settings are. The next thing to note is the timer’s clock prescalar. This is needed because we want to visually see the outcome with LEDs. If the LEDs blink too fast, we won’t not be able to realize what is going on. Lastly, internal gating is used because we don’t the timer to depend on other hardware events and we want to get timer output. Timer 1 will toggle the logic state of P3.4 (T1CLKO) GPIO pin when it overflows. Both GPIO pins P3.4 and P3.5 need to configured.

 P34_push_pull_mode;
 
P35_input_mode;

State of P5.5 LED changes according to the value of timer’s internal counter. For one half of the 16-bit count the LED is held low and for the other half it is held high.

 if(TMR1_get_counter() >= 32768)
{
  P55_high;
}
 
else
{
  P55_low;
}    

Thus, in the main, this is only job done. Rate of blinking depends on external clock speed. The higher the clock frequency, the higher is the blinking rate.

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

6 comments

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