Exploring STC 8051 Microcontrollers – Coding

Using PCA to Extend External Interrupt Capability

An alternative way of using the hardware PCA module is to use it to extend external interrupt capability of our STC micro. 

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
void setup(void);
 
void PCA_ISR(void)          
interrupt 7
{
  if(check_PCA_0_flag)
  {
    clear_PCA_0_flag;
    P54_low;
  }
  if(check_PCA_1_flag)
  {
    clear_PCA_1_flag;
    P54_high;
  }  
}
 
void main(void)
{
 
  setup();
  
  while(1)
  {
    P55_toggle;
    delay_ms(400);
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
  P54_push_pull_mode;
  P55_open_drain_mode;
  
  PCA_pin_option(0x00);
  
  PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_1);
  
  PCA_0_mode(PCA_16_bit_falling_edge_capture);
  PCA_1_mode(PCA_16_bit_falling_edge_capture);
    
  PCA_load_counter(0);
  
  _enable_PCA_0_interrupt;
  _enable_PCA_1_interrupt;
  _enable_global_interrupt;
  
  PCA_start_counter;
}

Schematic

Explanation

Perhaps this is the one and only PCA module example that doesn’t require precise selection of timing parameters and so we will directly go to PCA settings. In this example, a LED connected to pin P5.4 is either turned on or off using two buttons. These buttons are attached to default PCA channel 0 and 1 pins.

 PCA_pin_option(0x00);
  
PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_1);
  
PCA_0_mode(PCA_16_bit_falling_edge_capture);
PCA_1_mode(PCA_16_bit_falling_edge_capture);
    
PCA_load_counter(0);
  
_enable_PCA_0_interrupt;
_enable_PCA_1_interrupt;
_enable_global_interrupt;
  
PCA_start_counter;

Both PCA channel input pins are set to capture falling edges. It is not mandatory to use falling edge capture. In this case however, I used so because the buttons have external pullup resistors and so pressing them would lead to a high-to-low pin transition or falling edge.

The whole PCA setup seem to look like PCA capture mode setup but the exception is the fact that we won’t be needing the capture counts this time.

The state of the P5.4 LED is altered inside PCA interrupt subroutine and everything will appear as if the things are being done with external interrupts. Note that though this external interrupt technique works just like ordinary external interrupts, there is no way of setting interrupt priorities amongst PCA channels themselves.

 void PCA_ISR(void)          
interrupt 7
{
  if(check_PCA_0_flag)
  {
    clear_PCA_0_flag;
    P54_low;
  }
  if(check_PCA_1_flag)
  {
    clear_PCA_1_flag;
    P54_high;
  }  
}

The only task that is done in the main loop is a simple onboard LED blinking. This is not a must but this is only needed to show that PCA operations are done without blocking anything of the main loop.

 P55_toggle;
delay_ms(400);

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 *