0 Replies Latest reply on Jun 17, 2020 8:09 PM by NoTa_4591161

    PWM to change LED using compare - A simple program for beginners using Modus Toolbox2.1 with PSoC6(M4)


      Hi all,


      I am making some programs for beginners using Modus ToolBox 2.1. When migrating the development environment from PSoc Creator to Modus, I had a hard time creating a simple program. Especially, there are few simple samples using the Device Configuration tool.

      Here, a simple program for the M4 core of PSoC 6 using the Device Configuration tool of Modus Toolbox2.1 is introduced here.


      The environment is as follows.




      This time, we will use PWM to change the LED brightness using the compare. This is an example of changing the brightness by using compare as the base of "PWM that changes the brightness of LED".


      Select the CY8CKIT-062-BLE kit from the New Application tool in the Quick Panel and use it based on what was created in the empty PSoC 6 app.


      About the pin setting, start the Device Configuration tool from the Quick Panel in the IDE of Modus Toolbox2.1, and set it according to the following procedure.



      Select the “Pin” tab.

      Check P0[3], name it "LED" and select "Drive Mode" as "Strong Drive, Input buffer off".


      The peripherals that can be connected to P0[3] is displayed in the pull-down menu in Digital Output in Parameters on the right. Select "TCPWM[0] 32bit Counter 1 pwm_n" from the pull-down menu.




      Go to “Peripheral” tab and set TCPWM (Timer Counter PWM) as PWM.

      Check TCPWM [0] 32bit Counter 1 selected as Digital output of Pin0 [3]. Then, a selection window will appear. Select "PWM-1.0". Enter Name as "PWM".


      On the right side of the Peripheral tab, there is a place to set the PWM parameters. Set the comparison Compare that determines the period and the pulse width.




      Select the input clock source from the pull-down menu.

       Clock Signal:16bit Divider 0 clk


      make sure that what is set as Pin output is selected as PWM output.




      Go to the Peripheral-Clocks tab.

      Check "16bit Divider 0" specified in PWM.

      The division is 8.

      Divider: 8

      Since the clock source is 100MHz, 12.5MHz divided by 8 is output.

      Make sure that "TCPWM [0] 32bit Counter (PWM)" is selected as the Peripheral connection destination.



      The program is as follows.

      int main(void)
          cy_rslt_t result;
          int compare = 0 ;
          /* Initialize the device and board peripherals */
          result = cybsp_init() ;
          if (result != CY_RSLT_SUCCESS)
          Cy_TCPWM_PWM_Init(PWM_HW, PWM_NUM, &PWM_config);
          Cy_TCPWM_PWM_Enable(PWM_HW, PWM_NUM);
          Cy_TCPWM_TriggerStart(PWM_HW, PWM_MASK);
          for (;;)
              Cy_TCPWM_PWM_SetCompare0 (PWM_HW, PWM_NUM, compare++);
              if (compare >= 1000) {
                  compare = 0 ;
              CyDelay(1) ;


      It's very easy. Why don't you get used to Modus toolbox with this program?