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

cross mob
Gone
Level 3
Level 3
First solution authored 25 replies posted 25 sign-ins

I need to output on a small PSoC4 a PWM signal that is varying from 1% to 50% in 1% steps.  Can this be done in hardware on a PSoC4 device.  PSoC used: CY8C4025LQI-S402

I've seen examples using two PWM's and Smart IO but there duty cycle goes from 1% to 50% and then inverts.  I only need 1% to 50% maximum not the inversion portion(breathing in and out LED's examples).  I need to start at 1% go to 50% and stay there.  Does anyone know how this can be done on  small PSoC device as I listed.

My goal is in code,  to adjust the step percentage value based on a voltage measurement.

Thanks

 

0 Likes
1 Solution
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

@Gone ,

Hi.   Welcome to the PSoC world.

If you found examples that do what you want (but with the inversion), use the example project as a starting point. 

Rename the project to something more aligned to your goal. 

 Change the device the project uses to the one you are targeting (CY8C4025LQI-S402).    If you need help to change the part number to the CY8C4025LQI-S402, you'll need to let us know which IDE you are using:  PSoC Creator or ModusToolbox.

Lastly modify the code where needed. Change the pin assignments add a ADC to read the voltage you want to use to determine the 1% to 50%.   Get rid of the inversion phase of the example.

When I start a project where I'm working with functions and features I've never used, I start with 'working' code.  The demo and example projects that have worked are a great place to start.

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

View solution in original post

0 Likes
4 Replies
ADSW
Moderator
Moderator
Moderator
250 sign-ins 50 solutions authored 25 likes received

Hello @Gone , 

Which compiler do you use to build the project ? 
PSoC Creator :
You can try with the sample code CE230991. Remove the pin and other from line_n ( in TopDesign)
Please use PWM_WritePeriod and PWM_WriteCompare function to adjust the duty-cycle. 
Refer to this document 

Modustoolbox
It is better to start by using MTB-HAL.
After create the project, please add the mtb-hal-cat2 from library manager then update. 

ADSW_0-1698297646467.png

And you can try for this code:

#include "cy_pdl.h"
#include "cyhal.h"
#include "cyhal_pwm.h"
#include "cybsp.h"

#define PWM_PIN		P3_4
int main(void)
{
    cy_rslt_t result;

    /* Initialize the device and board peripherals */
    result = cybsp_init() ;
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

	cyhal_pwm_t pwm_obj;
	// Initialize PWM on the supplied pin and assign a new clock
	result = cyhal_pwm_init(&pwm_obj, PWM_PIN, NULL);
	if (result != CY_RSLT_SUCCESS)
		{
			CY_ASSERT(0);
		}
	// Set a duty cycle of 50% and frequency of 1KHz
	result = cyhal_pwm_set_duty_cycle(&pwm_obj, 50, 1000);
	if (result != CY_RSLT_SUCCESS)
		{
			CY_ASSERT(0);
		}
	result = cyhal_pwm_start(&pwm_obj);
	if (result != CY_RSLT_SUCCESS)
		{
			CY_ASSERT(0);
		}
    /* Enable global interrupts */
    __enable_irq();


    for (;;)
    {
    	 cyhal_system_delay_ms(1000);
    }
}

The cyhal_pwm_set_duty_cycle() function can be use to adjust the duty-cycle and frequency of PWM.

Warm regards,
Andri

0 Likes
Gone
Level 3
Level 3
First solution authored 25 replies posted 25 sign-ins

Andri,

thank you but i wanted to run it in hardware not software.  I know it might need some controls in software but running actual PWM i wanted to do in software.

thanks

ScottC

0 Likes
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

@Gone ,

Hi.   Welcome to the PSoC world.

If you found examples that do what you want (but with the inversion), use the example project as a starting point. 

Rename the project to something more aligned to your goal. 

 Change the device the project uses to the one you are targeting (CY8C4025LQI-S402).    If you need help to change the part number to the CY8C4025LQI-S402, you'll need to let us know which IDE you are using:  PSoC Creator or ModusToolbox.

Lastly modify the code where needed. Change the pin assignments add a ADC to read the voltage you want to use to determine the 1% to 50%.   Get rid of the inversion phase of the example.

When I start a project where I'm working with functions and features I've never used, I start with 'working' code.  The demo and example projects that have worked are a great place to start.

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

Len ... I am going to try and use two PWM's with an offset in period with XOR and see if i can use an interrupt to check compare values  ... I am in the process I trying to figure this out.

thanks for the suggestions

0 Likes