Exploring STC 8051 Microcontrollers – Coding

Using PCA to Generate PWM

Unlike enhanced PWM module, PCA module can used to generate PWMs of different resolutions. However, PCA-generated PWMs are like general purpose PWMs and do not have additional functionalities. These PWMs can be used for simple tasks like driving LEDs, servo motors, simple motor controls, simple DC-DC converters, etc.  

The following block diagram shows PCA module in 8-bit PWM mode. The 9-bit internal comparator compares the values CL and CCAPnL + EPCnL. When CL is equal or greater than CCAPnL + EPCnL, the PWM output is set high and when CL is less than CCAPnL + EPCnL, the PWM output is set low. Thus, CCAPnL + EPCnL are responsible for the duty cycle of PWM because CL keeps counting with each tick of PCA clock. When CL overflows, CCAPnL + EPCnL are reloaded automatically with the contents of CCAPnH + EPCnH. Updating CCAPnH + EPCnH, results in the alternation of PWM duty cycle. The same concept is true for PCA PWMs of other resolutions. Similar strategies are applied for PCA PWMs of other resolutions.

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
void setup(void);
void PWM_idle(void);
 
void main(void)
{
  signed int j = 0x0000;
  
  setup();
  
  while(1)
  {
    for(j = 0; j < 256; j++)
    {
      PCA_0_8_bit_PWM_reload_value(255 - j);
      PCA_1_8_bit_PWM_reload_value(j);
      PCA_2_8_bit_PWM_reload_value(255 - j);
      PCA_3_8_bit_PWM_reload_value(j);
      delay_ms(10);
    }     
      
    for(j = 255; j > -1; j--)
    {
      PCA_0_8_bit_PWM_reload_value(255 - j);
      PCA_1_8_bit_PWM_reload_value(j);
      PCA_2_8_bit_PWM_reload_value(255 - j);
      PCA_3_8_bit_PWM_reload_value(j);
      delay_ms(10);
    }     
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
  PCA_pin_option(0x10);
  
  PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_1);
  
  PCA_n_PWM(0, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
  PCA_n_PWM(1, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
  PCA_n_PWM(2, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
  PCA_n_PWM(3, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
  
  PCA_0_8_bit_PWM_compare_value(0); 
  PCA_0_8_bit_PWM_reload_value(0);  
  
  PCA_1_8_bit_PWM_compare_value(0); 
  PCA_1_8_bit_PWM_reload_value(0);  
  
  PCA_2_8_bit_PWM_compare_value(0); 
  PCA_2_8_bit_PWM_reload_value(0);  
  
  PCA_3_8_bit_PWM_compare_value(0); 
  PCA_3_8_bit_PWM_reload_value(0);  
  
  PCA_load_counter(0);
  
  PCA_start_counter;
}

Schematic

Explanation

In this example, four PCA PWM channels are simply used to drive LEDs of different colors.

The system clock is set to 12MHz.

 CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54); 

Pins P2.3 to P2.6 are used for PWM outputs. Each of these channels are set to 8-bit PWM mode and their compare and reload values are set to zero. Thus, initially all PWM channels are at same logic state. The PCA counter is set to run at system clock speed. All PWM channels will be running at same clock frequency as all are sharing the same PCA counter.

 PCA_pin_option(0x10);
 
/*
ECI         CCP0        CCP1      CCP2      CCP3        Hex            Option
P1.2        P1.7        P1.6      P1.5      P1.4        0x00           option 1
P2.2        P2.3        P2.4      P2.5      P2.6        0x10           option 2
P7.4        P7.0        P7.1      P7.2      P7.3        0x20           option 3
P3.5        P3.3        P3.2      P3.1      P3.0        0x30           option 4
*/
 
  
PCA_setup(PCA_continue_counting_in_idle_mode, PCA_clk_sys_clk_div_1);
  
PCA_n_PWM(0, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
PCA_n_PWM(1, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
PCA_n_PWM(2, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
PCA_n_PWM(3, PCA_PWM_without_interrupt, PCA_8_bit_PWM);
  
PCA_0_8_bit_PWM_compare_value(0); 
PCA_0_8_bit_PWM_reload_value(0);  
  
PCA_1_8_bit_PWM_compare_value(0); 
PCA_1_8_bit_PWM_reload_value(0);  
  
PCA_2_8_bit_PWM_compare_value(0); 
PCA_2_8_bit_PWM_reload_value(0);  
  
PCA_3_8_bit_PWM_compare_value(0); 
PCA_3_8_bit_PWM_reload_value(0);  
  
PCA_load_counter(0);
  
PCA_start_counter;

The PWM frequency of PCA channels is calculated to be as follows.

Thus, the period of the PWM is 21.33µs.

The logic analyser data confirms that the frequency is indeed around 46 kHz.

The duty cycles of the PWMs are calculated using the following formula.

Inside the main loop, each of the channels are updated every 10ms by updating their reload values.

 for(j = 0; j < 256; j++)
{
  PCA_0_8_bit_PWM_reload_value(255 - j);
  PCA_1_8_bit_PWM_reload_value(j);
  PCA_2_8_bit_PWM_reload_value(255 - j);
  PCA_3_8_bit_PWM_reload_value(j);
  delay_ms(10);
}     
      
for(j = 255; j > -1; j--)
{
  PCA_0_8_bit_PWM_reload_value(255 - j);
  PCA_1_8_bit_PWM_reload_value(j);
  PCA_2_8_bit_PWM_reload_value(255 - j);
  PCA_3_8_bit_PWM_reload_value(j);
  delay_ms(10);
}     

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 *