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.
Adam21
Level 1
Level 1
10 sign-ins 5 replies posted 5 sign-ins

Hi, I may be missing simple here and am hoping that is all it is. I setup this logic circuit in order to create a simple state machine (I tried with the LUT as well and encountered the same issue). The way it is supposed to work is if the button is pressed the LED turns on, if the button is pressed again nothing happens. If it is pressed again the LED turns off, then the cycle repeats. The issue I am encountering is that the LED always follows the clock pulse and always turns on whenever the button is pressed.

I attempted switching the clock input for the flip-flops with the button, however the same issue persisted. The clock (tied to the Clk_pin) was made from two cascading watchdog timers for 1 Hz. I changed this to an on board clock at 367 Hz (minimum frequency division for CY8CKIT-044 Pioneer kit) and the issue still persisted. 

I have attached the code I made for the 1 Hz clock I created with the schematic design and timer setup.

------------------------------------------

void Clk()
{

    ClkPin_Write(~ClkPin_Read());

}


int main()
{


/* Enable the global interrupt */
CyGlobalIntEnable;


CySysWdtSetInterruptCallback(CY_SYS_WDT_COUNTER1,Clk);
CySysWdtSetCascade(CY_SYS_WDT_CASCADE_01);

CySysWdtSetMatch(0,512);
CySysWdtSetMatch(1,64); //So our total divide is 512*64

for(;;)
    {
          CySysPmDeepSleep();
     }
}

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Adam,

I give your issue a try.

Attached is a VERY SIMPLE project using your select PSoC4 device.

Here is the simplified schematic.

Len_CONSULTRON_2-1669058237823.png

 

 

It has the following specs:

  • Button/LED logic is ALL in HW state machine.  The code only starts the timer.  There is NO SW intervention after that.
  • The Button/LED logic is in a LUT with registered outputs.
  • LED drive is active high.  If you need active low logic, you can invert the LED output in the LUT or place a NOT gate in the output.
  • The Button input is active low.  If you need active high logic, you can invert LUT logic or place a NOT gate in the input.
  • The HW state machine logic is clocked at 10Hz.  The timer is based on the HFCLK.  The LFCLK is not needed here.
  • The Button debounce time is 100ms.  This should be slow enough to filter out switch bounces.
  • The Button and LED port pin assignments were system default.  You may need to assign them to the correct pins in your 044 kit.

Here is a flow diagram of the HW state machine.

Len_CONSULTRON_0-1669220925368.png

Here is the LUT table.  Note: some of the states are "illegal".  If these LUT states are encountered, it "resets" to the Button idle state.

Len_CONSULTRON_1-1669058163836.png

I do not have a PSoC4 of the type you selected to test the project.  HOWEVER ...  I did convert the project to a PSoC5LP of which I have a kit.   It does work.  So I have confidence the PSoC4 project also work and the project builds without error.

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

View solution in original post

0 Likes
16 Replies
PandaS
Moderator
Moderator
Moderator
250 replies posted 100 solutions authored 5 likes given

Hi @Adam21 ,

Could you please share the stripped-down version of your project in which you are implementing this state machine.

And have you used button debouncing for this purpose?

Thanks and regards

Sobhit

0 Likes
Adam21
Level 1
Level 1
10 sign-ins 5 replies posted 5 sign-ins

It is my first time using these forums, how do I submit files if it does not allow the cywrk file type?

0 Likes
PandaS
Moderator
Moderator
Moderator
250 replies posted 100 solutions authored 5 likes given

Hi @Adam21 ,

Create an archive for your project and upload the zip file in your reply.

PandaS_0-1669006303885.png

See the above image for reference.

Thanks and regards

Sobhit

0 Likes
lock attach
Attachments are accessible only for community members.
Adam21
Level 1
Level 1
10 sign-ins 5 replies posted 5 sign-ins

Sorry for the late reply, here is the zip file of my original project. 

0 Likes
lock attach
Attachments are accessible only for community members.
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Adam,

I give your issue a try.

Attached is a VERY SIMPLE project using your select PSoC4 device.

Here is the simplified schematic.

Len_CONSULTRON_2-1669058237823.png

 

 

It has the following specs:

  • Button/LED logic is ALL in HW state machine.  The code only starts the timer.  There is NO SW intervention after that.
  • The Button/LED logic is in a LUT with registered outputs.
  • LED drive is active high.  If you need active low logic, you can invert the LED output in the LUT or place a NOT gate in the output.
  • The Button input is active low.  If you need active high logic, you can invert LUT logic or place a NOT gate in the input.
  • The HW state machine logic is clocked at 10Hz.  The timer is based on the HFCLK.  The LFCLK is not needed here.
  • The Button debounce time is 100ms.  This should be slow enough to filter out switch bounces.
  • The Button and LED port pin assignments were system default.  You may need to assign them to the correct pins in your 044 kit.

Here is a flow diagram of the HW state machine.

Len_CONSULTRON_0-1669220925368.png

Here is the LUT table.  Note: some of the states are "illegal".  If these LUT states are encountered, it "resets" to the Button idle state.

Len_CONSULTRON_1-1669058163836.png

I do not have a PSoC4 of the type you selected to test the project.  HOWEVER ...  I did convert the project to a PSoC5LP of which I have a kit.   It does work.  So I have confidence the PSoC4 project also work and the project builds without error.

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

I implemented this project as a PSoC4 project just fine (aside from updating components) however, I noticed that the LED turned on as the project was loaded right away. Pressing the button did change the LED state but only after 4-5 presses (varied on how long you held the button for). I double checked that the logic was the same as the screen shot you provided and it was. Was there a particular way you had set up the "Timer_Debounce" block to avoid this issue?

0 Likes
Adam21
Level 1
Level 1
10 sign-ins 5 replies posted 5 sign-ins

Also forgot to add that after the LED turned off it I couldn't get it turn back on.

0 Likes

Adam,

Can you provide your project where you implemented my LUT?

Since I don't have a 044 kit, I had to convert it to a 059 kit (PSoC5LP).

I can try to take your implementation and convert it to the PSoC5LP to see if I get the same results.

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

Adam,

My implementation on the PsoC5LP seems to work without issue.

Your implementation of my recommended LUT might have some issues.  Hence placing your project on this forum should be helpful.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
lock attach
Attachments are accessible only for community members.

I only loaded the project you produced to see the LED follow the LUT but it didn't seem to quite follow it. I am attaching the file that I loaded to my device although it should be the same as what you initially uploaded. As I said before the LED turns on when the program is first loaded and takes more button presses than the LUT requires to turn it off instead of the single or even double button presses as per your diagram. I am attempting on wiring a external button in place of the clock so I have more control over when input is sent the LUT.

0 Likes

Adam,

What is the polarity of your LED drive?

LED active high:

LED ON 1
LED OFF 0

or

LED active low:

LED ON 0
LED OFF 1

 

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

It is an active low. Implementing the not gate did fix that as it was something I did initially over look. Tying a external button to the pin of the LUT did help me realize that it was a clocking issue. So when the both buttons were pressed (the CLK button would mimic a rising edge) it would change more consistently with the LUT. Feeding the internal clock into the clock input of the LUT without going through the counter fixed the issue I was having. Thank you for your help I greatly appreciate it.  🙂

0 Likes
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

Just a side note. You might be able to implement this logic totally in hardware using the Smart I/O. The Smart I/O can run in deep-sleep if you clock it using the low-frequency clock.

So no need to use the CPU at all here.

0 Likes

Rodolfo,

Good point.  However, I believe the part Adam is using doesn't support SmartIO.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
lock attach
Attachments are accessible only for community members.
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

I see. I looked at the current suggested implementation. The timer that comes with the PSoC is very powerful. There are a bunch of features you can leverage in there, not only a timer. If you use the count and reload terminals, you can implement a debounce into the timer itself. You only need a toggle flip-flop to turn on/off the LED.

Attached you find a simple project (I used PSoC 6, but you can port it to PSoC 4). Here is a screenshot.

RodolfoGL_0-1669707367336.png

You can customize the Period and Compare values to adjust the debouncing time. Note you need to use Clk_Peri/2 to clock your components.

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Adam21,

If you only need to debounce a button, then you can check existing component

ButtonSw32: button switch debouncer component 

It doesn't consume PSoC4 hardware resources, which are quite limited, and allows up to 32 buttons.

 

0 Likes