Newbie..problem with UDB counter.

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

cross mob
magic_3776931
Level 2
Level 2

Hi, I'm trying to learn how to use a counter, I would like to fire an interrupt (set on and then off a RGB LED) after pushing for three times an external switch.

I can't understand what's wrong. Sometimes the interrupt fires after three times,sometimes after twice or one time I push the switch. I think that should be some problems with the parameters I set on the counter.

Please help me.

pastedImage_1.pngpastedImage_2.png

This is the code:

#include "project.h"

CY_ISR(ISR_COUNTER_HANDLER){

LED_BLU_Write(1);

LED_RED_Write(1);

CyDelay(500);

LED_BLU_Write(!LED_BLU_Read());

LED_RED_Write(!LED_RED_Read());

Counter_ReadStatusRegister();

}

int main(void){

CyGlobalIntEnable;

Counter_Start();

ISR_COUNTER_StartEx(ISR_COUNTER_HANDLER);

   

for(;;)

    {     }

}

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your mechanical switch will definitively deliver some spikes that the counter will see. Reducing  the counter clock to 100Hz will help.

It is easier for us to help when you provide us with your complete project workspace archive. Use

Creator->File->Create Workspace Bundle (minimal)

and attach the resulting .zip.

Bob

View solution in original post

3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your mechanical switch will definitively deliver some spikes that the counter will see. Reducing  the counter clock to 100Hz will help.

It is easier for us to help when you provide us with your complete project workspace archive. Use

Creator->File->Create Workspace Bundle (minimal)

and attach the resulting .zip.

Bob

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

marcogiorio,

Since it is low-speed requirement (manually pushing a button), it is better to use software-defined debouncer and counter instead of UDB components, which are intended for high-speed operation (MHz). Consider using a button-switch debouncer component for this purpose:

ButtonSw32: button switch debouncer component

Just add a counter in the main loop, which increments on each button pressed event.

/odissey1

Thank you Bob, it works! Next time i will follow the rules about attachments.

user_342122993, tomorrow i will have a try with button switch debouncer, thank you for the advice.

0 Likes