Exploring STC 8051 Microcontrollers – Coding

Using PCA as a Software Timer

We have already seen how the PCA module doubles as timer in the capture examples. However, unlike actual hardware timers, we would have to use some software tricks and keep certain things in mind.

Code

 include "STC8xxx.h"
#include "BSP.h"
 
#define T_Load         (1000000L / 12 / 2)
 
unsigned int value = T_Load;
 
void setup(void);
 
void PCA_ISR(void)          
interrupt 7
{
  clear_PCA_0_flag;
  PCA0_load_value(value);
  P55_toggle;
  value += T_Load;
}
 
void main(void)
{
 
  setup();
  
  while(1)
  {
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 24, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
  P55_open_drain_mode;
  
  PCA_pin_option(0x10);
  
  PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_12);
  
  PCA_0_mode(PCA_16_bit_software_timer);
    
  PCA_load_counter(0);
  PCA0_load_value(T_Load);
  
  _enable_PCA_0_interrupt;
  _enable_global_interrupt;
  
  PCA_start_counter;
}

Schematic

Explanation

This example uses the same concepts as in the high-speed pulse output example and yes, again it is a simple onboard LED blinking example. Between the two examples almost everything is same.   

The system clock is set to 1MHz.

CLK_set_sys_clk(IRC_24M, 24, MCLK_SYSCLK_no_output, MCLK_out_P54); 

PCA is setup with just as in the past example except for the clock prescalar and mode of operation. In this example, the PCA clock is set to 83.3kHz and software timer mode is selected. Note that since no PCA input-output channel is needed in this example, it doesn’t matter which pin arrangement is initialized.

 PCA_pin_option(0x10);
  
PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_12);
  
PCA_0_mode(PCA_16_bit_software_timer);
    
PCA_load_counter(0);
PCA0_load_value(T_Load);
  
_enable_PCA_0_interrupt;
_enable_global_interrupt;
  
PCA_start_counter;

The desired LED blinking frequency is 1Hz and so the PCA0 is loaded with the following count value.

Since we are toggling the onboard LED, every half second, it will change its state and so the toggling frequency is 1 Hz.  

Note there is no operation in the main loop.

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 *