Someone can help me in this problem about buttons?

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.
RoFr_4201436
Level 3
Level 3
50 sign-ins 25 sign-ins 10 replies posted

I created a code in PSOC 4 and is working correctly. This program works this way: When you click in the first button, the engine turn front and when you click in the second button, the engine turn back. My problem with this problem is the when i puted the firts "if" this way:

if(BOTAO1_D3_Read() == 1)

        {

            IN1_Write(1);

            IN2_Write(0);

           

            ENA_Write(1);

}

Working correctly, but when i puted the second "if" this way:

if(BOTAO2_P2_7_Read() == 1)

        {

         IN1_Write(0);

         IN2_Write(1);

       

         ENA_Write(1);

        }

The second button not working. And i after, i puted the "if" of the second button this way:

if(BOTAO2_P2_7_Read() == 0)

        {

         IN1_Write(0);

         IN2_Write(1);

       

         ENA_Write(1);

}

And this way working.

Someone can check my program? And if someone find the error, try to awnser me.

Follow the attachment of my problem.

Thanks!

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

Hi,

I downloaded your project, but I could not open it.

It would be nice if you attach an archive made by

(1) select the project in the Workspace Explorer

(2) Menu Build > Clean <project>

(3) Menu Project > Archive workspace/project...

Having written that I imagined what you wanted to do,

I assumed that you have 2 switches (active low) and 3 output.

To take care of 2 switches there are 4 states

(0) no switches are pushed

(1) sw1 is pushed

(2) sw2 is pushed

(3) both sw1 and sw2 are pushed

So I think that we should take care of these conditions

instead of doing "if" for each case,

which often made us over look some conditions.

Then, to utilize a button, we need to consider if we should use polling mode or interrupt.

At first polling mode seems to be easier, but IMHO interrupt tend to be easier after all.

And last but not least the mechanical switches provide us a lot of chattering

so as /odissey1-san suggested considering to use Debouncer component may be necessary.

Now, thanks for the Friday afternoon,

and to rehabilitate my brain from the post-GoldenWeek numbness,

I tried both polling mode and interrupt.

(Needless to say, there must be many other ways, but these are my first attempts)

=== Polling mode ===

schematic

003-poling-schematic.JPG

TeraTerm log

002-polling-teraterm-log.JPG

main.c (polling mode)

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

#include "project.h"

#include "stdio.h"

char str[128] ; /* print buffer */

void print(char *str)

{

    UART_UartPutString(str) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

   

    UART_Start() ;

    Motor_Forward_Write(0) ;

    Motor_Backward_Write(0) ;

}

void splash(void)

{

    sprintf(str, "Button/Motor Test polling mode (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

int main(void)

{

    int status ;

   

    init_hardware() ;

   

    splash() ;

    for(;;)

    {

        status = Status_Reg_Read() ;

      

        switch(status) {

        case 0x0: /* both switch pushed */

            print("Both SWs pushed, stopping the motor\n") ;

            /* What should we do here? */

            /* Just for an example, I stop the motor */

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

            CyDelay(500) ;

            break ;

        case 0x2: /* SW1 pushed */

            print("SW1 Pushed: Motor Forward\n") ;

            Motor_Forward_Write(1) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

            CyDelay(500) ;

            break ;

        case 0x1: /* SW2 pushed */

            print("SW2 Pushed: Motor Backward\n") ;

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(1) ;

            ENA_Write(1) ;

            CyDelay(500) ;

            break ;

        case 0x3:

        default:

            ENA_Write(0) ;

            break ;

        }

        CyDelay(100) ; /* 0.1sec interval */

    }

}

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

=== interrupt ===

schematic

001-interrupt-schematic.JPG

TeraTerm log

000-interrupt-teraterm-log.JPG

main.c (interrupt)

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

#include "project.h"

#include "stdio.h"

volatile int sw1_pushed = 0 ;

volatile int sw2_pushed = 0 ;

CY_ISR(sw1_isr)

{

    SW1_ClearInterrupt() ;

    sw1_pushed = 1 ;

}

CY_ISR(sw2_isr)

{

    SW2_ClearInterrupt() ;

    sw2_pushed = 1 ;

}

char str[128] ; /* print buffer */

void print(char *str)

{

    UART_UartPutString(str) ;

}

void init_hardware(void)

{

    UART_Start() ;

   

    SW1_ClearInterrupt() ;

    SW1_INT_ClearPending() ;

    SW1_INT_StartEx(sw1_isr) ;

   

    SW2_ClearInterrupt() ;

    SW2_INT_ClearPending() ;

    SW2_INT_StartEx(sw2_isr) ;

    Motor_Forward_Write(0) ;

    Motor_Backward_Write(0) ;

   

    CyGlobalIntEnable; /* Enable global interrupts. */

}

void splash(void)

{

    sprintf(str, "Button/Motor Test with interrupt (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

int main(void)

{

    init_hardware() ;

   

    splash() ;

    for(;;)

    {

        if (sw1_pushed && !sw2_pushed) {

            sw1_pushed = 0 ;

            print("SW1 pushed: Motor Forward\n") ;

            Motor_Forward_Write(1) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

        } else if (!sw1_pushed && sw2_pushed) {

            sw2_pushed = 0 ;

            print("SW2 pushed: Motor Backward\n") ;

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(1) ;

            ENA_Write(1) ;

        } else if (sw1_pushed && sw2_pushed) {

            print("Both SWs pushed: stopping the motor\n") ;

            sw1_pushed = 0 ;

            sw2_pushed = 0 ;

            /* What should we do here? */

            /* Just for an example, I stop the motor */

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

        } else { /* no switch pushed */

            ENA_Write(0) ;

        }

        CyDelay(100) ;

    }

}

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

I hope that these could be a little hint for you.

moto

View solution in original post

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

framcordrigognr,

For button processing consider using a custom component ButtonSw32, which simplfies user code by taking care of most underlying processing:

ButtonSw32: button switch debouncer component

/odissey1

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

Hi,

I downloaded your project, but I could not open it.

It would be nice if you attach an archive made by

(1) select the project in the Workspace Explorer

(2) Menu Build > Clean <project>

(3) Menu Project > Archive workspace/project...

Having written that I imagined what you wanted to do,

I assumed that you have 2 switches (active low) and 3 output.

To take care of 2 switches there are 4 states

(0) no switches are pushed

(1) sw1 is pushed

(2) sw2 is pushed

(3) both sw1 and sw2 are pushed

So I think that we should take care of these conditions

instead of doing "if" for each case,

which often made us over look some conditions.

Then, to utilize a button, we need to consider if we should use polling mode or interrupt.

At first polling mode seems to be easier, but IMHO interrupt tend to be easier after all.

And last but not least the mechanical switches provide us a lot of chattering

so as /odissey1-san suggested considering to use Debouncer component may be necessary.

Now, thanks for the Friday afternoon,

and to rehabilitate my brain from the post-GoldenWeek numbness,

I tried both polling mode and interrupt.

(Needless to say, there must be many other ways, but these are my first attempts)

=== Polling mode ===

schematic

003-poling-schematic.JPG

TeraTerm log

002-polling-teraterm-log.JPG

main.c (polling mode)

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

#include "project.h"

#include "stdio.h"

char str[128] ; /* print buffer */

void print(char *str)

{

    UART_UartPutString(str) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

   

    UART_Start() ;

    Motor_Forward_Write(0) ;

    Motor_Backward_Write(0) ;

}

void splash(void)

{

    sprintf(str, "Button/Motor Test polling mode (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

int main(void)

{

    int status ;

   

    init_hardware() ;

   

    splash() ;

    for(;;)

    {

        status = Status_Reg_Read() ;

      

        switch(status) {

        case 0x0: /* both switch pushed */

            print("Both SWs pushed, stopping the motor\n") ;

            /* What should we do here? */

            /* Just for an example, I stop the motor */

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

            CyDelay(500) ;

            break ;

        case 0x2: /* SW1 pushed */

            print("SW1 Pushed: Motor Forward\n") ;

            Motor_Forward_Write(1) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

            CyDelay(500) ;

            break ;

        case 0x1: /* SW2 pushed */

            print("SW2 Pushed: Motor Backward\n") ;

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(1) ;

            ENA_Write(1) ;

            CyDelay(500) ;

            break ;

        case 0x3:

        default:

            ENA_Write(0) ;

            break ;

        }

        CyDelay(100) ; /* 0.1sec interval */

    }

}

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

=== interrupt ===

schematic

001-interrupt-schematic.JPG

TeraTerm log

000-interrupt-teraterm-log.JPG

main.c (interrupt)

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

#include "project.h"

#include "stdio.h"

volatile int sw1_pushed = 0 ;

volatile int sw2_pushed = 0 ;

CY_ISR(sw1_isr)

{

    SW1_ClearInterrupt() ;

    sw1_pushed = 1 ;

}

CY_ISR(sw2_isr)

{

    SW2_ClearInterrupt() ;

    sw2_pushed = 1 ;

}

char str[128] ; /* print buffer */

void print(char *str)

{

    UART_UartPutString(str) ;

}

void init_hardware(void)

{

    UART_Start() ;

   

    SW1_ClearInterrupt() ;

    SW1_INT_ClearPending() ;

    SW1_INT_StartEx(sw1_isr) ;

   

    SW2_ClearInterrupt() ;

    SW2_INT_ClearPending() ;

    SW2_INT_StartEx(sw2_isr) ;

    Motor_Forward_Write(0) ;

    Motor_Backward_Write(0) ;

   

    CyGlobalIntEnable; /* Enable global interrupts. */

}

void splash(void)

{

    sprintf(str, "Button/Motor Test with interrupt (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

int main(void)

{

    init_hardware() ;

   

    splash() ;

    for(;;)

    {

        if (sw1_pushed && !sw2_pushed) {

            sw1_pushed = 0 ;

            print("SW1 pushed: Motor Forward\n") ;

            Motor_Forward_Write(1) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

        } else if (!sw1_pushed && sw2_pushed) {

            sw2_pushed = 0 ;

            print("SW2 pushed: Motor Backward\n") ;

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(1) ;

            ENA_Write(1) ;

        } else if (sw1_pushed && sw2_pushed) {

            print("Both SWs pushed: stopping the motor\n") ;

            sw1_pushed = 0 ;

            sw2_pushed = 0 ;

            /* What should we do here? */

            /* Just for an example, I stop the motor */

            Motor_Forward_Write(0) ;

            Motor_Backward_Write(0) ;

            ENA_Write(1) ;

        } else { /* no switch pushed */

            ENA_Write(0) ;

        }

        CyDelay(100) ;

    }

}

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

I hope that these could be a little hint for you.

moto