Exploring STC 8051 Microcontrollers – Coding

Using PCA as a High-Speed Pulse Generator

In many cases, we need to generate high-frequency carrier waveform or pulse trains. For example, this is needed when we need to make an infrared (IR) remote transmitter. This job can easily be done by using a timer or software delays. However, in STC micros, we have PCA hardware to accomplish the same job much more easily. This lets us use general purpose timers for other tasks.

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
#define freq_change         (12000000L / 2 / 38400)
 
unsigned int value = freq_change;
 
void setup(void);
 
void PCA_ISR(void)          
interrupt 7
{
  clear_PCA_0_flag;
  PCA0_load_value(value);
  value += freq_change;
}
 
void main(void)
{
 
  setup();
  
  while(1)
  {
    if(P52_get_input == FALSE)
    {
      P24_high;
      P55_low;
    }
    
    else
    {
      P24_low;
      P55_high;
    }
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
  P01_push_pull_mode;
  P55_open_drain_mode;
  
  P52_input_mode;
  P52_pull_up_enable;
  
  PCA_pin_option(0x10);
  
  PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_1);
  
  PCA_0_mode(PCA_16_bit_high_speed_pulse_output);
    
  PCA_load_counter(0);
  PCA0_load_value(value);
  
  _enable_PCA_0_interrupt;
  _enable_global_interrupt;
  
  PCA_start_counter;
}

Schematic

Explanation

In this example, PCA high-speed pulse mode is used to make an IR remote transmitter.

Since PCA is a time-dependent hardware peripheral, we need to be sure of the system clock settings. As with most examples, the system clock is set to 12MHz

 CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54); 

Four GPIO pins are required for this example and are set right after selecting the system clock. A button is connected with pin P5.2. Pressing the button would change the logic state of both the onboard LED (P5.5) and pin P2.4. Lastly, pin P2.3 is set as the PCA CCP0 pin. This pin is the high-speed pulse output pin. P2.3 and P2.4 are connected to an IR LED.

Note that P2.4 is not set like other GPIOs because it is already set by PCA hardware.

 P01_push_pull_mode;
P55_open_drain_mode;
  
P52_input_mode;
P52_pull_up_enable;
  
PCA_pin_option(0x10);

Now let’s see how the PCA hardware is set. Firstly, the PCA clock and counting mode are set. PCA clock is set to run at 12MHz, i.e., at system clock speed. Next, the PCA channel to be used and its mode of operation is set, here – 16-bit high speed pulse output mode. The PCA counter is set 0 count and PCA0 channel is loaded with a count value.

 PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_1);
  
PCA_0_mode(PCA_16_bit_high_speed_pulse_output);
    
PCA_load_counter(0);
PCA0_load_value(value);
  
_enable_PCA_0_interrupt;
_enable_global_interrupt;
  
PCA_start_counter;

The loaded value is the count value required to generate the desired frequency of 38.4kHz (26µs period).

Finally, the PCA0 and global interrupts are enabled before starting the PCA counter.

Note that PCA counter keeps counting and is not reloaded anywhere in the whole code. It is left on its own and it will rollover after 65535 counts. When the PCA counter counts to 156 counts, PCA0 interrupt is triggered. Note that 156 counts equal 13µs because the PCA clock is running at 12MHz. On each interrupt, PCA0’s channel polarity is altered. This means 13µs is the pulse width of high-speed pulse output waveform.

PCA interrupt is processed by clearing PCA0 interrupt flag and by loading PCA0 with a new count value (previous count + load value, i.e., 156 + 156 = 312) because after that new count (312) PCA0 interrupt will be triggered again and the process repeats over and over again.

 void PCA_ISR(void)          
interrupt 7
{
  clear_PCA_0_flag;
  PCA0_load_value(value);
  value += freq_change;
}

Inside the main loop, the push button pin is polled. If the button is pressed, the IR LED is enabled by setting P2.4 high or else it is disabled. Since P2.3 is the PCA 0 output, there is always 38.4 kHz square pulses present in that pin. Only when P2.4 is set is high, the IR LED lights up. It is just like a simple AND logic.

if(P52_get_input == FALSE)
{
P24_high;
P55_low;
}
else
{
P24_low;
P55_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

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 *