[Problem] PWM duty setting issue by (PWM-HAL)

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

cross mob
rsiigod
Level 3
Level 3
Distributor - WPG(GC)
25 replies posted 50 sign-ins First solution authored

Operating environment:
Win10
ModusToolBox Version: 2.4.0
Eclipse Build ID: 2460
Evk: CY8CKIT-062-WIFI-BT
mtb-hal-cat1 release-v2.0.1

When I use cyhal_pwm_set_duty_cycle to set the duty equal to 0% or 100%, it seems the output is not correct!
result = cyhal_pwm_set_duty_cycle(&pwm_obj, 0.0f, 10000);  ==> Then PWM output is 100%(keep high level output)
result = cyhal_pwm_set_duty_cycle(&pwm_obj, 100.0f, 10000); ==> Then PWM output is 99.98%

I wonder does there is any bugs in the function library cyhal_pwm_set_duty_cycle() & cyhal_pwm_set_period_and_compare().

 

Below is my sample code:

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

float fCompareValue = 0.0f;

int main(void)
{
    cy_rslt_t result;
    cyhal_pwm_t pwm_obj;

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

    __enable_irq();
    cyhal_gpio_init(CYBSP_LED9, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);

	// Initialize PWM on the supplied pin and assign a new clock
    result = cyhal_pwm_init(&pwm_obj, CYBSP_LED8, NULL);
	// Set a duty cycle of 100% and frequency of 10KHz
    result = cyhal_pwm_set_duty_cycle(&pwm_obj, fCompareValue, 10000);
	// Start the PWM output
    result = cyhal_pwm_start(&pwm_obj);

    for (;;)
    {
		if(fCompareValue <= 100.0f)
		{
			result = cyhal_pwm_set_duty_cycle(&pwm_obj, fCompareValue, 10000);
			cyhal_gpio_toggle(CYBSP_LED9);
			Cy_SysLib_Delay(250);
			fCompareValue += 10.0f;
		}
    }
}

 

Actually output waveform:

SAC_Ade_Chen_0-1642733839794.png

 

1 Solution
rsiigod
Level 3
Level 3
Distributor - WPG(GC)
25 replies posted 50 sign-ins First solution authored

When I modify part of the code in cyhal_pwm.c, as shown on the right side of the figure below:

SAC_Ade_Chen_0-1642734129004.png

SAC_Ade_Chen_1-1642734231810.png

Re-build & Program, the output waveform is fine.

SAC_Ade_Chen_2-1642734923630.png

 

View solution in original post

0 Likes
4 Replies
rsiigod
Level 3
Level 3
Distributor - WPG(GC)
25 replies posted 50 sign-ins First solution authored

When I modify part of the code in cyhal_pwm.c, as shown on the right side of the figure below:

SAC_Ade_Chen_0-1642734129004.png

SAC_Ade_Chen_1-1642734231810.png

Re-build & Program, the output waveform is fine.

SAC_Ade_Chen_2-1642734923630.png

 

0 Likes
Ekta_N
Moderator
Moderator
Moderator
750 replies posted First like given 250 solutions authored

Hi @rsiigod 

Sorry for the delay in replying to this thread. This thread was missed.

The code for PWM is correct.

The CY8CKIT-062-WIFI-BT contains CY8C6247BZI-D54 device. If you see the device datasheet for this you will observe that the IMO (Internal Main Oscillator) which has an accuracy of +/-2 percent. This is also mentioned in the device datasheet.

This is why you are observing a slight deviation in the PWM expected output.

Best Regards
Ekta

0 Likes
rsiigod
Level 3
Level 3
Distributor - WPG(GC)
25 replies posted 50 sign-ins First solution authored

Hi @Ekta_N 

Good day~

There should still be problems with the underlying code, independent of oscillator accuracy.
Because 0% of the output is wrong, not just inaccurate.
You can view the following figure, when the output is set to 0%, but the actual output is about 100%....

SAC_Ade_Chen_0-1647843562728.png

 

0 Likes
Ekta_N
Moderator
Moderator
Moderator
750 replies posted First like given 250 solutions authored

Hello @rsiigod 

Thanks for pointing to this. I have discussed this issue with our internal team and will create an internal ticket to get this fixed.

Thanks
Ekta