Exploring STC 8051 Microcontrollers – Coding

Using Timer 4 as Time-base Generator

The very basic use of a timer is to use it for time-base generation. By time-base generation, I mean that we use a timer as a free-running timer and without using any of its interrupts.

Code

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

  setup();
  
  while(1)
  {
      if(TMR4_get_counter() >= 0x9E58)
      {
        P55_high;
      }
      else
      {
        P55_low;
      }
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 24, MCLK_SYSCLK_no_output, MCLK_out_P54);
  
  P55_open_drain_mode;
  
  TMR4_setup(TMR4_sysclk, \
             TMR4_clk_prescalar_12T, \
             TMR4_no_clk_out);
  
  TMR4_load_counter_16(0x3CB0);
  TMR4_start;
}

Schematic

Explanation

For this demo, onboard LED is used as the demo is a simple LED blinking. The desired LED blink rate is 1.67Hz (about 600ms).

The system clock is set to 1MHz. Everything related to internal hardware timing is dependent on system clock and so its selection is very important. The system clock is set to 1MHz as the required blink rate is very small. Every system clock tick is, therefore, 1µs.

 CLK_set_sys_clk(IRC_24M, 24, MCLK_SYSCLK_no_output, MCLK_out_P54); 

Now let’s see how the timer is configured. Firstly, timer 4 is used as it is one of the simplest timers. Obviously, this timer is working in 16-bit auto-reload mode (Mode 0) as no other mode of operation is available for it.

 TMR4_setup(TMR4_sysclk, \
           TMR4_clk_prescalar_12T, \
           TMR4_no_clk_out);
  
TMR4_load_counter_16(0x3CB0);
TMR4_start;

As we can see the timer is fed with internal system clock (1MHz). This is further scaled down by a factor of 12, i.e., the timer is ticking at:

All 16-bit timers start counting from a bottom value to a fixed top value of 65535 (0xFFFF) counts. A timer’s internal counter overflows after exceeding the top value and then rolls over or repeats counting from its bottom value. The bottom value is 0 (0x0000) unless some other value is assigned in the code. Thus, unlike the top value, the bottom value can be changed. In this example, the bottom value is 15536 (0x3CB0). This, in effect, help us achieve 50000 counts.

65536 – 15536 = 50000 counts

Now, we want to equally divide the on and off times of the LED and so the 50000 counts are divided into two halves, each 25000 counts individually. Thus, in the main loop, the timer’s counter count is polled.

 if(TMR4_get_counter() >= 0x9E58)
{
  P55_high;
}
else
{
  P55_low;
}

If the count is greater than or equal to 40536 (0x9E58), P55 is held high or else it is held low. Thus, from 15536 count to 40535 count (40535 – 15536 = 24999), the pin’s state is low and from 40536 count to 65535 count (65535 – 40536 = 24999), the pin state is high.

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

13 comments

Leave a Reply

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