Psoc4 PWM led light flash one time randomly when use array of sine waves

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

cross mob
lock attach
Attachments are accessible only for community members.
Leo_liu1
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

Hi SIr,

我通过定时器设置中断时间写compare值实现呼吸灯,这些值来自正弦波的数组,256个等级,实现8s一个周期,所以定时16ms。

问题描述:灯光随机会出现闪一下的现象。

问题排查:1.我怀疑是写进去的值与实际的值不一样,就打印了XX_ReadCompare()值,结果与数组的值一致,用示波器看波形,发现灯闪的时候,波形也没有跳变的过程。

2.当PWM的clock从256k改到12M时,这种闪一下的概率会非常低,但是还是会出现。

附件是我测试用的工程,请帮忙分析一下。

谢谢。

BR,

Leo

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

Dear Leo-san,

I was paying attention to the PWM's correct function only.

And I re-opened the project and ported it to CY8CKIT-042 and tested it.

Yes, LED seems to be darker and I noticed flickers.

Then I notice some points

(1) Timer's frequency and PWM's frequency is too close, this can cause alias effect

I changed the clock of PWM to 16MHz

(2) Although I was not paying attention to the method of positioning, but using only "==" is usually dangerous,

    so I modified it to

=================

for(;;) //forever

{

        if (pit_flag) {

            pit_flag = 0 ;

            PWM_R_Stop() ;

            PWM_R_WriteCounter(0) ;

            if(position <= 255 && !pwm_direct) {

                PWM_R_WriteCompare(Sine_Lookup[position++]);

                if (position >= 256) { /* was == */

                    pwm_direct = 1;

                    position = 255;

                }

            } else if (pwm_direct) {

                PWM_R_WriteCompare(Sine_Lookup[position--]);

                if(position <= 0) { /* waa == */

                    pwm_direct = 0;

                    position = 0 ;

                }

            }

            PWM_R_Start() ;

        } 

    }

=================

Now on my CY8CKIT-042, LED is brighter and I I don't notice flicker anymore.

I hope this serves you better now.

Best Regards,

20-Jun-2019

Motoo Tanaka

View solution in original post

0 Likes
10 Replies
LinglingG_46
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 10 questions asked

CY_ISR( Timer_Int_Handler )

{

if(position <= 255 && !pwm_direct) 

    {

        PWM_R_WriteCompare(Sine_Lookup[position++]);

        if (position == 256)

        {

            pwm_direct = 1;

            position = 255;

        }

    }

else if (pwm_direct)

   {

        PWM_R_WriteCompare(Sine_Lookup[position--]);

        if(position == 0)

        {

            pwm_direct = 0;

        }

    }

Timer_ClearInterrupt(Timer_INTR_MASK_TC);

}

检查      PWM_R_WriteCompare(Sine_Lookup[position++]);  这个值可以到     PWM_R_WriteCompare(Sine_Lookup[256]);; 这种情况下出现了数组越界的情况。

position=255;这样下面down的过程中, PWM_R_WriteCompare(Sine_Lookup[position--]); 只能执行到PWM_R_WriteCompare(Sine_Lookup[254]);检查一下这两个地方的数组。

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

Hi,

I would suggest stopping PWM before writing compare

=============

CY_ISR( Timer_Int_Handler )

{

PWM_R_Stop() ;

PWM_R_WriteCounter(0) ;

if(position <= 255 && !pwm_direct) 

    {

        PWM_R_WriteCompare(Sine_Lookup[position++]);

        if (position == 256)

        {

            pwm_direct = 1;

            position = 255;

        }

    }

else if (pwm_direct)

   {

        PWM_R_WriteCompare(Sine_Lookup[position--]);

        if(position == 0)

        {

            pwm_direct = 0;

        }

    }

PWM_R_Start() ;

Timer_ClearInterrupt(Timer_INTR_MASK_TC);

}

=============

moto

0 Likes

Hi Moto,

I used your method to test,but the light is darker than before and the lights look noticeably shakeable.

BR,

Leo

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

Dear Leo-san,

I'm sorry, it was my bad.

I should not have played with PWM inside an ISR,

so I changed the program as below, and let ISR only set flag

and in the main loop modifying the PWM set up.

main.c (part)

=======================

volatile int pit_flag = 0 ;

u_char pwm_direct = 0;

u_int16_t position = 0;

CY_ISR( Timer_Int_Handler )

{

    pit_flag = 1 ;

    Timer_ClearInterrupt(Timer_INTR_MASK_TC);

}

void Initialize(void)

{

CyGlobalIntEnable;

    PWM_R_Start();

    Timer_Start();

    Timer_Int_StartEx( Timer_Int_Handler );

}

int main()

{

    Initialize();

for(;;) //forever

{

        if (pit_flag) {

            pit_flag = 0 ;

            PWM_R_Stop() ;

            PWM_R_WriteCounter(0) ;

            if(position <= 255 && !pwm_direct) {

                PWM_R_WriteCompare(Sine_Lookup[position++]);

                if (position == 256) {

                    pwm_direct = 1;

                    position = 255;

                }

            } else if (pwm_direct) {

                PWM_R_WriteCompare(Sine_Lookup[position--]);

                if(position == 0) {

                    pwm_direct = 0;

                }

            }

            PWM_R_Start() ;

        }  

    }

}

=======================

Attached is my modified project.

At least when I re-targeted the device and pin for CY8CKIT-044, it seems to be working.

Best Regards,

19-Jun-2019

Motoo Tanaka

0 Likes

Hi Moto,

Thank you for your reply.

I re-built your project on CY8CKIT-042 board with your suggestion , it seems to be working,because the led is small ,i can't tell whether it's darker or not.

But when i use project board which has bigger led ,the light is darker than before and the light looks shakeable noticeably .

Best Regards,

Leo

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

Dear Leo-san,

I was paying attention to the PWM's correct function only.

And I re-opened the project and ported it to CY8CKIT-042 and tested it.

Yes, LED seems to be darker and I noticed flickers.

Then I notice some points

(1) Timer's frequency and PWM's frequency is too close, this can cause alias effect

I changed the clock of PWM to 16MHz

(2) Although I was not paying attention to the method of positioning, but using only "==" is usually dangerous,

    so I modified it to

=================

for(;;) //forever

{

        if (pit_flag) {

            pit_flag = 0 ;

            PWM_R_Stop() ;

            PWM_R_WriteCounter(0) ;

            if(position <= 255 && !pwm_direct) {

                PWM_R_WriteCompare(Sine_Lookup[position++]);

                if (position >= 256) { /* was == */

                    pwm_direct = 1;

                    position = 255;

                }

            } else if (pwm_direct) {

                PWM_R_WriteCompare(Sine_Lookup[position--]);

                if(position <= 0) { /* waa == */

                    pwm_direct = 0;

                    position = 0 ;

                }

            }

            PWM_R_Start() ;

        } 

    }

=================

Now on my CY8CKIT-042, LED is brighter and I I don't notice flicker anymore.

I hope this serves you better now.

Best Regards,

20-Jun-2019

Motoo Tanaka

0 Likes

Hi Moto,

Thanks!

Your method is useful,if i put this code to ISR function,is it possible,the interrupt time is 16ms,it's a long time.

Best Regards,

Leo

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

Dear Leo-san,

If the interrupt happens every 16ms, it will be fine.

But if an ISR (interrupt service routine) takes 16ms,  I feel that it's too long.

But to be honest, if you test it and if the behavior is acceptable for you, it may be OK

after all it is "your" project.

Best Regards,

20-Jun-2019

Motoo Tanaka

Dear Moto,

Got it ,thanks!

Best Regards,

Leo

0 Likes

Hi Lingling,

数组不会出现越界的情况,我也打印看了position的值都没有问题,而且这是个随机发生的事件,应该不是这个问题。

BR,

Leo

0 Likes