3 Replies Latest reply on Sep 30, 2020 1:53 AM by MoTa_728816

    PWM counter reset on falling edge


      I'm looking for the best approach to setup a PWM timer of 10KHz, where the counter can be resetted by the falling edge of external pin at higher frequency. That would mean that if there is a falling edge on a specific pin, the counter will be reset, thus, immediately the PWM output pin will be set high until compare match. Furthermore I would like to know the number of counts between two falling edges (freq measurement). Can all this be achieved with only one hardware timer?


      I've already tryed to setup PWM timer with reset input, but this doesn't seems to work on these relatively high frequencies. Maybe this is due to the missing pulse, also have a look at the attached picture:


      For some reason the first pulse is skipped it seems?

        • 1. Re: PWM counter reset on falling edge



          I checked this with PSoC 4200M (CY8CKIT-044/CY8C4247AZI-M485).


          I think that if you set the PWM "Right align" and use the "line_n" for output, it could be done.




          1 of 1 people found this helpful
          • 2. Re: PWM counter reset on falling edge

            Hi, thanks, already tried this,but it doesn't look well (duty cycle is wrong as well some pulses are skipped):


            First signal is input signal, second is PWM output signal.

            I'm using: CY8CKIT-042


            I'm looking for a hardware timer implementation due to performance.

            • 3. Re: PWM counter reset on falling edge



              I created the following test project for CY8CKIT-042.







              Please Jumper Pulse_out (P1[0]) with ExtIn(P2[0])

              P0[4] to P12[6] (PSoC 5LP)

              P0[5] to P12[7] (PSoC 5LP)



              Note: I also cheated by using my tty_utils utility from

              Re: tty_utils a utility sample for CLI type program


              #include "project.h"

              #include "stdio.h"

              #include "tty_utils.h"


              uint16_t period ;

              uint16_t compare ;


              void init_hardware(void) ;

              void do_command(char *str) ;

              void do_help(void) ;


              int main(void)


                  init_hardware() ;

                  splash("CY8CKIT-042 PWM Test") ;


                  do_help() ;


                  prompt() ;



                      if (get_line()) {

                          do_command(str) ;

                          prompt() ;





              void init_hardware(void)


                  CyGlobalIntEnable; /* Enable global interrupts. */


                  tty_init() ;



                  PWM_Start() ;

                  SIG_Gen_Start() ;

                  period = SIG_Gen_ReadPeriod() ;

                  compare = SIG_Gen_ReadCompare() ;



              void config_pwm(uint16_t period, uint16_t compare)


                  SIG_Gen_Stop() ;

                  SIG_Gen_WritePeriod(period) ;

                  SIG_Gen_WriteCompare(compare) ;

                  SIG_Gen_WriteCounter(0) ;

                  SIG_Gen_Start() ;



              void report_status(void)


                  print("Test Signal: ") ;

                  snprintf(str, STR_BUF_LEN, "%u %u\n\r", period, compare) ;

                  print(str) ;



              void do_help(void)


                  print("=== usage ===\n\r") ;

                  print("start                  : start test signal\n\r") ;

                  print("stop                   : stop test signal\n\r") ;

                  print("pwm <period> <compare> : (ex) pwm 1199 599\n\r") ;

                  print("period <period>        : (ex) period 2399\n\r") ;

                  print("compare <compare>      : (ex) compare 999\n\r") ;

                  print("status                 : report current config\n\r") ;

                  print("help                   : show this message\n\r") ;

                  print("\n\r") ;



              void do_command(char *str)


                  if (strncmp(str, "pwm", 3) == 0) {

                      sscanf(str, "%*s %hu %hu", &period, &compare) ;

                      config_pwm(period, compare) ;

                  } else if (strncmp(str, "period", strlen("period")) == 0) {

                      sscanf(str, "%*s %hu", &period) ;

                      config_pwm(period, compare) ;

                  } else if (strncmp(str, "compare", strlen("compare")) == 0) {

                      sscanf(str, "%*s %hu", &compare) ;

                      config_pwm(period, compare) ;

                  } else if (strncmp(str, "start", strlen("start")) == 0) {

                      SIG_Gen_Start() ;

                  } else if (strncmp(str, "stop", strlen("stop")) == 0) {

                      SIG_Gen_Stop() ;

                  } else if (strncmp(str, "status", strlen("status")) == 0) {

                      report_status() ;

                  } else {

                      do_help() ;





              Tera Term Log


              At first I set test signal to 3599 1799

              Red : Pulse_out (P1[0]) Test Signal

              Yellow: Output of PWM (P1[1])


              Then I set it to 1199 and 599, which is exactly same with the PWM


              Then I tried to assign shorter period for test signal 999 and 499



              So far, the PWM seems to start at the falling edge of the test signal.

              But as the source clock of both PWM and SIG_Gen (test signal) are the same,

              may be applying an async input causes different behavior.