PWM counter reset on falling edge

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
MaHo_4757026
Level 3
Level 3
25 replies posted 10 replies posted 5 replies posted

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:

missing_pulse.png

For some reason the first pulse is skipped it seems?

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I created the following test project for CY8CKIT-042.

schematic

010-schematic.JPG

pins

011-Pins.JPG

Note:

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

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

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

main.c

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() ;

    for(;;)

    {

        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

012-teraterm-log.JPG

At first I set test signal to 3599 1799

Red : Pulse_out (P1[0]) Test Signal

Yellow: Output of PWM (P1[1])

IMG_4504.JPG

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

IMG_4505.JPG

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

IMG_4506.JPG

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.

moto

View solution in original post

0 Likes
3 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

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.

001-PWM-Config.JPG

moto

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

output.png

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.

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I created the following test project for CY8CKIT-042.

schematic

010-schematic.JPG

pins

011-Pins.JPG

Note:

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

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

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

main.c

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() ;

    for(;;)

    {

        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

012-teraterm-log.JPG

At first I set test signal to 3599 1799

Red : Pulse_out (P1[0]) Test Signal

Yellow: Output of PWM (P1[1])

IMG_4504.JPG

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

IMG_4505.JPG

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

IMG_4506.JPG

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.

moto

0 Likes