PWM/USBUART conflict - Logic problem?

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.
Anonymous
Not applicable

Hello again, everyone!

I'm having (yet another) issue with my variable PWM project, this time it seems to be some sort of conflict between the USBUART and the PWM, oddly enough.  I created a stripped down version of the code to recreate the problem.

Background

When my code is working normally, it's setup to do the following:

T=0

Valve 1 = on, Valve 2,3,4 off

T=1

Valve2 = on, Valve 1,3,4 off

T=3

Valve 3 = on, Valve 1,2,4 off

T=6

Valve 4 = on, Valve 1,2,3 off

T=10

Valve 1,2,3,4 off

V=12=0

Repeat

Below is what this looks like (In order from Pin_Valve_CH1 to Pin_Valve_CH4):

pastedImage_0.png

Here is my hardware setup, for reference (the whole project is included with this post though, if that's easier.  Just trying to save time for anyone looking at this, in case they can eyeball the problem without needing to simulate it):

pastedImage_15.png

The Problem

This will repeat indefinitely until I write to the USBUART (you can basically send anything, I just have shell code that doesn't really do anything with it in this project).  Once I write to the USBUART, CH1 locks high and never seems to recover:

pastedImage_1.png

I was able to isolate the issue by commenting out sections of the ISR so that my logic goes from this:

CY_ISR(PWM_Interrupt_Handler)

{

    Pin_LED3_Write(~Pin_LED3_Read());

    /* Current Code */

    arrayindex = SR_Channel_Read();

   

    // Temporary Bandaid - Need to fix

    if(arrayindex==NumSensors)

    {

        arrayindex = 0; 

    }

    else

    {

        arrayindex += 1;  

    }

    PWM_WritePeriod(OnTimeArray[arrayindex]);

   

    if(arrayindex==NumSensors)

    {

        PWM_WriteCompare(0);

    }

    else

    {

        PWM_WriteCompare(OnTimeArray[arrayindex]);

    }

    /* End of current code */

   

    PWM_WriteCounter(0);

    uint8 tempdata = PWM_ReadStatusRegister();

}

To this:

CY_ISR(PWM_Interrupt_Handler)

{

    Pin_LED3_Write(~Pin_LED3_Read());

    /*Test Code */

//    PWM_WritePeriod(PWM_ReadPeriod());

//    PWM_WriteCompare(PWM_ReadCompare());

    /* End of test code */

      

    PWM_WriteCounter(0);

    uint8 tempdata = PWM_ReadStatusRegister();

}

If I move to the latter code (effectively just writing out the same current period and compare values), the PWM doesn't lock up but obviously doesn't update the PWM correctly (as shown below):

pastedImage_4.png

You might be wondering why the logic for the PWM update is so chaotic and the reason is when I just tried to input the current code to update based on the SR register, it was off by one cycle.  Below is the code and the output:

pastedImage_5.png

CY_ISR(PWM_Interrupt_Handler)

{

    Pin_LED3_Write(~Pin_LED3_Read());

   /* Current Code */

    arrayindex = SR_Channel_Read();

   

    PWM_WritePeriod(OnTimeArray[arrayindex]);

       

    PWM_WriteCompare(OnTimeArray[arrayindex]);

   

    /* End of current code */

  

      

    PWM_WriteCounter(0);

    uint8 tempdata = PWM_ReadStatusRegister();

}

Interestingly enough, the above code doesn't seem to show the same latching issues so there's some issue with my "bandaid" that's causing the problem.  Maybe I just need a way to add some sort of hardware delay so that the SR_Channel_Read() is correct and then the above code will work but, it appears that my temporary solution is the core of the issue and I don't really understand why.  Any information you can give me would be greatly appreciated.

If you have any solutions or can see what the root cause might be, please let me know,  Sorry for the size of the post but I figure too much info is always better than too little info and I've been trying to resolve this issue for longer than I'd care to admit . . . thanks again!

0 Likes
1 Solution
Anonymous
Not applicable

Hi,

Line number 142 in main.c which clears out NumSensors look suspicious. For any input via USBUART, this variable is getting cleared. Here is what is happening-

1. When data is received over USB-UART, NumSensors gets cleared in line 142.

2. Everything works fine until arrayindex reaches 4. When arrayindex reaches 4, it is incremented by 1 in line 60.

3. PWM period is written 0 in line 63 and interrupt is never triggered again

4. In hardware, ch1 is always selected and it remains high

-Rajiv 

View solution in original post

0 Likes
8 Replies