HowTo: Generate Pulse Width Modulation (PWM) signals on S7G2

s7g2

#1

Summary

The purpose of this tutorial is to explain how to generate Pulse Width Modulation (PWM) signals. PWM output the most commonly used method to control the speed of motors, the brightness of LED lights and Servo drives, and lots more. This tutorial will help anyone who wants to use PWM those kinds of functions.

Requirements for this tutorial

  • Renesas Synergy Starter Kit SK-S7G2

  • Development IDE E2Studio on your PC

  • Digital Multi Meter to measure Hz/Duty cycles

  • SEGGER JLink Driver software

  • LED and connecting wires

Please note: You can click to enlarge all of the screenshots in this tutorial.

Step 1: Start E2Studio and choose/create a workspace

If you already know how to create a Synergy Project in E2Studio ignore this screenshot. Here I have combined multiple steps into a single screen shot.

I have chosen Blinky with Threadx so that the LEDs on the board will blink ON/OFF continuously to indicate the application is running without any issue.

Please refer to the steps in this tutorial if something is not clear in the screenshot:

Step 2: Adding a General Purpose Timer (GPT) timer to your application

Please follow the numbered steps in order in the above screenshot.

Step 3: GPT Configuration

By default GPT will be set to periodic. The Renesas SSP has an option to change these timer properties to PWM. We need to refer to the SKS7G2 board hardware manual to find which channels and which IO Port is mapped for GPT. The SKS7G2 board supports 14 timers (GPT0 to GPT13) and each timer can control 2 IO ports (A/B).

Just for example, assume we are going to choose GPT7 which is used to generate PWM on IOPORT_03_04(CLOCKA) and PORT_03_03 (CLOCKB). By default SSP Configurator uses this IO port for some other purpose. So we need to disable that first. Otherwise we can’t use this IP port for PWM.

Step 4 Disable IO_PORT

Step 5: Configuration:

Now expand “Peripherals” -> Timer:GPT to configure the IO Port we want to use to generate PWM. Each GPT can generate PWM in two IP ports. We can configure either one or both IO PORTs.

For this tutorial we are going to choose only one port which is IOPORT_03_03. This is connected with GTIOCB. So choose GTIOCA or GTIOCB from operation mode which allows to you select the IO PORT.

Please refer to the following screenshot:

OK, we are now done with the IO port selection. Now we need to configure the GPT timer properties to make PWM on the selected channel/IO PORT.

About Properties

  • We chose GTP7 so the channel should be 7
  • Mode: PWM
  • Period: 900Hz. It depends on motor/device specification in order to set the appropriate HZ/KHz.
  • Period Unit: Hz
  • Duty Cycle: 90%. You can set any % value since we are going to change this dynamically.
  • Duty Cycle Unit: Percentage
  • Auto Start: False. We are going to start the timer on PWM Thread.
  • GTIOCA Enabled: True. In case you switch the IOPORT 03_04
  • GTIOCB Enabled: True. By default this is False. However, if you don’t get any pulse output please make sure this is set to True

OK, we are done with GTP/PWM settings.

Step 4 Dynamic control of Duty Cycle

To change Duty Cycle (which controls the pulse output on the IO PORT) we need to add Thread and write a few lines of code. In our Demo we are going to change the Duty Cycle every few millisecs or seconds. This allows us to measure PWM Hz or Duty Cycle % and make sure our code works correctly.

Look at the following screenshot to add Thread to your project/Application.

Once you click (3) Generate project content you will see a new thread source .c file added into your src/ folder.

Replace your pwm_thread source code with this source code here:

  #include "pwm_thread.h"

  #define CLOCK_A         0
  #define CLOCK_B         1

  /* Servo Motor Control entry function */
  void pwm_thread_entry(void)
  {
       //open timer object
       g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg);

       //Change Duty cycle around 30% i.e. overriding properties 
       g_timer0.p_api->dutyCycleSet(g_timer0.p_ctrl, (30 %100), TIMER_PWM_UNIT_PERCENT, CLOCK_B);
      
       //start the timer which will product PWM
       g_timer0.p_api->start(g_timer0.p_ctrl);

      uint8_t dutycycle = 0;
      while (1) {
            tx_thread_sleep (150);
            dutycycle = (dutycycle  + 5) % 100; // Every iteration change Duty Cycle 5% more
            g_timer0.p_api->dutyCycleSet(g_timer0.p_ctrl, dutycycle, TIMER_PWM_UNIT_PERCENT, CLOCK_B);
      }
}

NOTE

If you choose GTIOCA clock then change the last parameter CLOCK_B to CLOCK
_A like this:

   g_timer0.p_api->dutyCycleSet(g_timer0.p_ctrl, dutycycle, TIMER_PWM_UNIT_PERCENT, CLOCK_A);

Also you need to make changes in the GPT properties and Peripherals settings accordingly.

Here is the screenshot showing how to build and install the application on the board.

To measure PWM Duty Cycle or connect and control any device we need to wire the IO port accordingly. For demo purpose I connected to the GND and IO PORT 03_03 to Multi meter and set Hz/% mode.

Below is a screenshot of the PWM output. If you connect servo motors (you will need to follow servo motors PWM specifications) or DC low power motors we can see speed variation.

Green wire connected to GND
RED wire connected to IOPORT_03_03

Here is the example output screen shot captured while running this demo. The Duty Cycle was set to 45% at that instant and the multi meter shows 44%.

10

Hz output on this port is set to 900 Hz but measured 899.9.

11

WHY IOPORT 03_03

In case if you are wondering why I chose IO PORT 03_03 for this tutorial, here is the answer.
If you refer to the SKS7G2 board diagram, IOPORT 03_03 is connected with an Arduino interface shield Digital 03 (PWM). This example makes a replica of Arduino PWM usage.

So basically we now know how to use Arduino interface ports on the SKS7G2 board!

PWM to LED

Instead of Multi meter you can connect LEDs between GND and IO PORT 03_03. When you do, you can see the brightness of the LED changed according to the PWM Duty Cycle %.

Hope this tutorial is easy to follow. if you have any questions or issues with above information, please drop me a line here.

For complete project and source code: