Starting/stopping a loop when SW1 or SW2 is pressed

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

 I am currently working on a board designed by a professor at my university using the microcontroller CY8C29466-24PXI, this board controls 12 motors (12 8bit PWMs), has 4 LEDs, 2 switches and 1 LCD display.

   

So far, using ¨PWMs I've made the robot walk on a straight line but without control, it begins walking as the board and the power source are both ON, so I need to use the two switches to start and stop the motion of the robot, and for that we thought to use one for each action, but I don't know how to integrate the button action to the Block diagram and the main.c code.

   

Also, I've assigned LED_1 and LED_2 to ports P1_2 and P1_3 respectively, but when i try to iniciate them from the main.c, they don't appear, I begin writting "LED_1_Start();" but the code helper does not show any LED connected.

   

Thanks beforehand to anyone that could help.

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

Welcome in the forum!

   

It is always advisable to post your complete project, so that we all can have a look at all of your settings. To do so, use
Designer ->File-> Archive Project (minimal)
and attach the resulting .zip file.



Bob
 

View solution in original post

0 Likes
11 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum!

   

It is always advisable to post your complete project, so that we all can have a look at all of your settings. To do so, use
Designer ->File-> Archive Project (minimal)
and attach the resulting .zip file.



Bob
 

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Here it is!

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

I am using latest Designer 5.4 and the LED_1_Start() is shown in code completion (Did you switch it off?)

   

The program code looks a bit unstructured, better to use a self-written Delay() function instead of repeating the code-lines. Much better would be to use a timer, since the program could do different things until a timer fires its interrupt.

   

When you can write a function that performs a step of the robot, it would be easier to start and stop it with a button.

   

 

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I now am able to program the start of LED1 and LED2 it may be because i didn't compile the code later, thanks.

   

About the delay, I am writting these methods:

   

void DelaySimple (void){

   

for (i=0;i<=30000;i++) {}

   

}

   

void  DelayLong(void){

   

for (i=0;i<=30000;i++) {}

   

for (i=0;i<=30000;i++) {}

   

for (i=0;i<=30000;i++) {}

   

}

   

And using them like this: 

   

PWM8_4_WritePulseWidth(11);

   

DelaySimple();

   

PWM8_5_WritePulseWidth(17);

   

DelaySimple();

   

PWM8_7_WritePulseWidth(17);

   

DelaySimple();

   

 

   

And is thowing me  5 errors and 38 warnings, can you please tell me why?, I've only coded before on JAVA  and on void methods you don't put a return.

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

your variable i is not declared within your functions.

   

and I would write

   

void  DelayLong(void){

   

uint8 i;

   

for (i=0;i<;i++) {DelaySimple()}

   

}

   

 

   

Here is a link to my favourite C-manual

   

 

   

Bob

Anonymous
Not applicable

 Bob, when you say:

   

"When you can write a function that performs a step of the robot, it would be easier to start and stop it with a button."

   

How can i read the input from Port_1_0 and Port_1_1, when the button is pressed, my professor said that in a microcontroller an interruption the reading does not have to be in the code itself, but how then can I read that interruption? I was thinking if I can use the state on P1_0 like a boolean variable, so it can be used inside an if  as the conditional,

   

i.e.:

   

while (1){

   

       int decide;

   

       if(Port1_0_Read()){

   

          //Reads when SW1 is pressed

   

         decide = 1;

   

       }

   

      if(decide==1){

   

          //Robot code here

   

        if(Port_1_1_Read()){

   

          //Reads when SW2 is pressed

   

         decide == 0;

   

        LCD_1_PrCString("STOPPED");

   

        }

   

     }

   

}

   

But in code completion does not appear that Read() command, what command is simmilar to the one I need?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

When you read a mechanical switch like a push button you have

   

to debounce it. You can poll it or use an interrupt.

   

 

   

This ap note shows how to read a button. Also this shows how to use

   

shadow registers for read modify writes to the port.

   

 

   

    

   

         

   

http://www.cypress.com/?rID=2900     AN2094 - PSoC® 1 - Getting Started with GPIO

   

 

   

https://www.youtube.com/watch?v=tei6q5M3C0g       Shadow Registers

   

 

   

http://www.cypress.com/?rID=39497     Shadow Registers

   

 

   

Link to ways of debouncing - www.dropbox.com/s/r8vfqea5fhiuzw1/Debounce.zip

   

 

   

Delay functions for PSOC 1 - www.cypress.com/

   

 

   

 

   

Regards, Dana.

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

there are two things you have to solve: the electrical and the program.

   

A Button usually connects a pin to Vdd or to GND. to the pin must be connected to the opposite using a resistor. This can be done internal to the PSoC with the pin drive mode. Additionally you ought to give the pin a characteristical name as "StartButton".

   

Then the function StartButton_Read() will give you a value of the current input pin's voltage level, High or Low, TRUE or FALSE

   

 

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 Thanks Dana and Bob!

   

Bob, the board already came with the resistor needed  for both switches, but I don't understand when you say:

   

"Then the function StartButton_Read() will give you a value of the current input pin's voltage level, High or Low, TRUE or FALSE"

   

I have the Port_1_2 and Port_1_3 assigned as the StartButton and StopButton respectively, but when I compile it, it does not show at the main in the code completion anything similar to StartButton_Read(). What am I doing wrong?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Read of a port is done like this -

   

 

   

uint8 myport1;

   

uint8 mypin3;

   

 

   

     myport1 = PRT1DR;     //PRT1DR is a macro in Designer for port1

   

 

   

Then you use a mask to extract the pin state. For example mask for pin 3

   

(pins numbered 0 - 7 in a port) = 0b00001000 = 0x08

   

 

   

So mypin3 = ( PRT1DR & 0X08 );

   

 

   

If you wanted mypin3 to be just a logical something like this -

   

 

   

mypin3 = ( PRT1DR & 0X08 ) ? 1 : 0;

   

 

   

The ap note posted earlier shows this.

   

 

   

Regards, Dana.

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

...and to make things easier to read, thanks to the C preprocessor you may use

   

#define BitMask(BitNum) (0x01 << BitNum)
#define ReadPort(PortNo,BitNo) ((PRT ## PortNo ## DR & BitMask(BitNo))?1:0)

void main(void)
{
   
    if(ReadPort(0,3)) HandleButtonPressed();

   

 

   

Bob