How to Stop a PWM after a certain amount of pulses in code

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

cross mob
AC
Level 1
Level 1
5 replies posted 5 sign-ins 5 likes given

Hi,

I have followed the steps from this thread: https://community.infineon.com/t5/PSoC-4/PSoC-4-BLE-Timer-Module/m-p/328738 to create a PWM component that pulses 700us ON and 1us OFF. I am using a PSoC 4 BLE chip.

However, when I turn it on (using PWM_Start()), I do not know how to turn it off after a set amount of pulses/periods. What I need to do is after 1000 pulses, the PWM needs to turn off by itself.

Does anyone know how to do this? And can all this be accomplished in the c code, as I need to be able to change the stop count from 1000 pulses to some other number throughout the program?

Thanks, 

AC

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.

AC,

I've uploaded a VERY SIMPLE project for your part that has a PWM of 700us ON and 1us OFF.

In this example it counts "PULSE_CNT_2_STOP" cycles and then stops the PWM.

In this example, PULSE_CNT_2_STOP = 10.   You can modify this variable to a value up to 65536

Additionally the PWM is restarted every 20ms.   You can change this part of the code to determine when you want to restart it (if ever).

I have tested this code on a CY8CKIT-042-BLE.  It works.

Len
"Engineering is an Art. The Art of Compromise."

View solution in original post

6 Replies
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

AC,

What PSoC part # are you using?

Len
"Engineering is an Art. The Art of Compromise."

Len,

It is of the series PSoC 4200 BLE. The full device name is CY8C4247LQI-BL483 56-QFN.

Thanks for pointing this out,

AC

0 Likes
lock attach
Attachments are accessible only for community members.

AC,

I've uploaded a VERY SIMPLE project for your part that has a PWM of 700us ON and 1us OFF.

In this example it counts "PULSE_CNT_2_STOP" cycles and then stops the PWM.

In this example, PULSE_CNT_2_STOP = 10.   You can modify this variable to a value up to 65536

Additionally the PWM is restarted every 20ms.   You can change this part of the code to determine when you want to restart it (if ever).

I have tested this code on a CY8CKIT-042-BLE.  It works.

Len
"Engineering is an Art. The Art of Compromise."

Hi Len,

Thanks for your help! I am able to get the program to stop the PWM. 

However, I do have some additional questions for the program:

If I want to have 4 stepper motors running simultaneously, is there a way to put the 4 stepper motor pins in the same PWM component? I have tried adding 3 more PWM + Timer Counter modules, but I get an error saying I have exceeded the max limit for this (which is 4 and I had 8). In my program I need to sometimes have the motors move one by one, but other times have them move simultaneously (with different stop counts).

Also, I need to disable the motors once their respective PWM stops, but then re-enable them once they start (the PWM gets enabled). Is there a way to write code at the place where the Pulse Counter stops the PWM so that I can disable the motors (and change some other variables)? For example, in a simple Timer component, this place would be the interrupt CY_ISR (which gets called every millisecond if frequency is 24 MHz), so is there something like that for the PWM component?

 

Thank you,

AC

0 Likes

AC,

 

You're welcome.

I've tried to redesign the TopDesign to use HW resources in the part you chose to create a PWM stepper pulse for 4 channels independently.

It uses a continuously running PWM for the 700us ON/1us OFF and 4 HW one-shot counters counters for the pulse counts.   The schematic below is what I came up with that is very efficient on PSoC HW resources.  The bad news ... it won't fit into your part chosen.

Len_CONSULTRON_0-1644164223828.png

Error: mpr.M0014: Resource limit: Maximum number of Datapath Cells exceeded (max=4, needed=8). (App=cydsfit)
Error: mpr.M0014: Resource limit: Maximum number of Status Cells exceeded (max=4, needed=5). (App=cydsfit)

Two choices:

  • Pick a PSoC4 part that has at least double the number of UDB resources
  • Convert the Pulse count HW to SW interrupt-based solution.  This is $$$ efficient but can be prone to issues if the interrupts are not serviced in a timely fashion.

If you are interested in the SW pulse count solution example project, let me know.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

AC,

I've uploaded 4 example projects for independent control of up to 8 stepper motors to:

Example: Multi-Stepper-Motor-Control-using-minimal-resources 

One of the projects will work on the 042-BLE as well as other PSoC4s.

Len
"Engineering is an Art. The Art of Compromise."