Exploring STC 8051 Microcontrollers – Coding

Using Single Channel Enhanced PWM to Drive a Servo Motor

Servo motors have varieties of applications both in industrial automation and robotics arena. They are simple and can be digitally controlled with only one wire and over long distances. However, this one wire digital control mechanism is somewhat unique and sometimes stressful because trains of low duty cycle pulses are needed to be sent constantly in order to lock a particular angular position.

To resolve this issue, we can use GPIOs coupled with software delays or timers or PWMs module to control one or several servo motors. One key advantage of using PWM modules over GPIOs is the accuracy in timing since PWM modules use timer-counters for timing instead of software delays. In this section, we will see how we can use a PWM module to drive an ordinary servo motor. STC8A8K64S4A12’s enhanced PWM module is used as the PWM generator here.

Code

 #include "STC8xxx.h"
#include "BSP.h"
 
#define servo_min_duty     800
#define servo_max_duty    2200
#define step_change          5
 
void setup(void);
void PWM_idle(void);
 
void main(void)

  unsigned int i = 0x0000;
  
  setup();
  
  while(1)
  {
    for(i = servo_min_duty; i < servo_max_duty; i += step_change)
    {
      PWM_set_PWM0_T1(i);  
      delay_ms(4);
    }    
    for(i = servo_max_duty; i > servo_min_duty; i -= step_change)
    {
      PWM_set_PWM0_T1(i);  
      delay_ms(4);
    }
  };
}
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);
 
  PWM_clk_set(PWM_clk_sys_PS, PWM_clk_ps_sys_clk_div_12);  
  PWM_set_counter(20000);
  
  PWM0_setup(PWM_pin_is_PWM_output, \
             PWM_init_lvl_low, \
             PWM_0_pin_P20, \
             PWM_level_normal);
              
  PWM_set_PWM0_T1(1000);
  PWM_set_PWM0_T2(0);  
  
  PWM_start_counter;
}

Schematic

Explanation

Servo motors typically have timing diagrams as shown below:

Each pulse has a period of 20ms but the duty cycle/pulse high time is varied from 5 – 10% to rotate from one direction to the other. This clearly demonstrates why timing is very important for servo motors.

The demo program here is coded to run at 12MHz.  

 CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_no_output, MCLK_out_P54);  

PWM0 hardware is configured to use P2.0 pin as servo control pin. The PWM hardware clock is set to 1MHz by using system clock as clock source and dividing it with a prescalar of 12. This is, however, not the PWM output frequency because the PWM counter is loaded with count value of 20000. The resultant PWM output frequency is 50Hz as shown in the following equation:  

The following codes does all mentioned so far. However, PWM setup is not just about timing only. PWM hardware also need an output. We have to mention which PWM we would be using and what would be its level and polarity.

 PWM_clk_set(PWM_clk_sys_PS, PWM_clk_ps_sys_clk_div_12);
  
PWM_set_counter(20000);
  
PWM0_setup(PWM_pin_is_PWM_output, \
           PWM_init_lvl_low, \
           PWM_0_pin_P20, \
           PWM_level_normal);
              
PWM_set_PWM0_T1(1000);
PWM_set_PWM0_T2(0);  
  
PWM_start_counter;

After setting all these, we just have to start the PWM hardware by starting the PWM counter.

Inside the main loop, the duty cycle of the PWM is slightly varied in steps. This results in smooth servo motion. Note that the maximum and minimum duty cycles are not 2000 and 1000 respectively as they ideally should have been. This is so because these values are typical ones and not always practical. Thus, the maximum and minimum duty cycles are set to 2200 and 800 respectively.

The 1% variation is needed because of non-ideality.

 #define servo_min_duty     800
#define servo_max_duty    2200
#define step_change          5
 
....
 
for(i = servo_min_duty; i < servo_max_duty; i += step_change)
{
  PWM_set_PWM0_T1(i);  
  delay_ms(4);
}
    
for(i = servo_max_duty; i > servo_min_duty; i -= step_change)
{
  PWM_set_PWM0_T1(i);  
  delay_ms(4);
}

The logic analyser data shown below verifies that whatever stated so far has been achieved.

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 *