Continue to Site

# button routine in ht-picc

Status
Not open for further replies.

#### arash_micro

##### Member level 5
picc press button

Hi.
I want to make button routine in ht-picc.
I write this code ( without debounce routine ) but it doesn't work.What's my wrong.

if(RB4=0){
DelayMs (50);
chanal++;
}

Thanks.

#### fireball003

##### Full Member level 3
picc button routine

Hi,
I have no idea about your code. But I can see the syntax mistake in this part you mentioned above. if (RB4 = 0) will be replaced by if(RB4 == 0).

You know single equal is to assign a value and double equal is to check

Best regards.

#### arash_micro

##### Member level 5
I was correct the error.It works in pruteuse but not in real.
Why?

#### gidimiz

Hi,

It all depands on how your hardware is connected! Is your button connected to Vcc or Gnd? i.e. when you push the button, what do you get on your PIN 1 or 0?
Let say you get a 0, so you button is connectet on one side to GND, and the other to your RB4 with a Pull up ( you must have that ).
Then you code will look like this:
Code:
if (RB4 == 0)
{
DelayMs (50);
if (RB4 == 0)     // If its still on
chanal++;
}

If you found my answer useful, click on the button that says Helped me. ( NO points will be taken from you! And you will get 3 Points)

Good luck.

#### waseem

##### Full Member level 5
Hi!
The idea that i got from ur code is that the pin is normally pulled up and when u press the button it makes the pin low, the code should be like this

if(!RB0) //if RB0 is not high then execute the code
{
YOUR_CODE;
}
thus the code will be executed when the pin is low.
Similarly if u want to execute a particular code when a pin is high let's say RB0 then the code should be like this
if(RB0)
{
YOUR_CODE;
}
i hope this makes ur points clear.
Regards

#### gidimiz

Hi waseem,

There is a problem with the code that you have posted. You will have to do a wait state and then check the pin again. There are other ways, but this is the simplest. The reson for this is because you want to be sure that the code will work only if you pressed the button, and not just by any chnage in voltage that can be cause from other reason. Also, with your code, the code will be exacuted as long as you are pressing the button.
So lets say you want to toggle a LED on and off, with your code it will turn on and off the led as long as you press the button. and at the end the led could be off.
So, at the end of the day, its good to put some delay or any other function that will make a key press more reliable.

Good luck.

#### waseem

##### Full Member level 5
Hi Gidimiz!
Thanks for the analysis. I already knew what u have mentioned, but what i was trying to stress in my code is that there is no need to write "if(RB0==1)"
u can simply write "if(RB0)" and similarly instead of "if(RB0==0)" u can write
"if(!RB0).
of courese you must add a certain delay to make the switch operation reliable.
Once again thanks for the analysis.
Regards

#### gidimiz

Hi waseem,
waseem said:
... but what i was trying to stress in my code is that there is no need to write "if(RB0==1)"
u can simply write "if(RB0)" and similarly instead of "if(RB0==0)" u can write
"if(!RB0)...
You are correct, but i didnt want to mention this, then i would have to explain whats the differance and how to use this.
Sorry for the miss understanding...

Good luck.

#### sp

##### Full Member level 6
gidimiz said:
Hi waseem,

There is a problem with the code that you have posted. You will have to do a wait state and then check the pin again. There are other ways, but this is the simplest.

i would really like to know bout the other ways u mention...

let's say if i hav a reset button n i want it to b pressed for 4s n if i use the above code, i would hav reliability prob (as the delay of 4s n check again is not reliable) like if i subsequently press n release n if my luck is there i might b pressing at the time when it check again after 4s.. so it would reset...

please share ur thougt on debouncing methods... i am really *&(^# by the debouncing methods...

thank you...

regards,
sp

#### gidimiz

Hi SP,

For a long duration there is another way to do this without making the program stuck!
You will need to implement a timer with interrupt ( you can use any of the TMR ) and set them to a minimum that you will need. For example, if the fasts thing you will need is to flash a LED every 0.5 Sec, then the TMR should be set to interrupt every 0.5. If its only the button, the you can interrupt every 1Sec.
Ok, No you have some timer that interrupts every 1 sec, so what. In the interrupt, you will rise a flag every time this interrupt will happen. ( dont forget to clear the TMRIF flag ). Then in the main code you will check that the Flag was set and that the button was pressed. If everything was correct for 4 times ( i.e. 4 seconds had passed ) then do...
Here is the code in general terms, as I don't know what compiler you are using.
Code:
void interrupt(void)
{
If (TMR0IF)                // I used timer 0 to flag
{
TMR0IF = 0;
SecFlag = 1;
TMR0 = 0x80;	// Reload value to TMR0
}
}

Void main (void )
{
SetIO();		//Sets all the IO and peripheral

While (1)
{
if (SecFlag )		//1 sec has passed
{
SecFlag = 0;       // Reset the flag of the seconds
if (RB0)	// if button still pressed
Button++;
if ( !RB0 && Button >=1 )	// If the button was release at some point or at the end. You can put this function here or better outside this function.
{
Button =0;
}
if(Button >=4 )
RA0 = !RA0;		// Will toggle the Led.
}
If ( !RB0 && Button >=1 )	// Same function as before, but here.
} //end while(1)

}//End main

I some people put the other code in the interrupt, i dont like it becuase i like to keep my interrupt clean and sort, and do most of the function in the main loop. this always increases the speed of my other functions and im not stuck to long in the interrupt.

Good luck.

#### waseem

##### Full Member level 5
Hi Gidimiz!
You are welcome. I will always appreciate your comments, because once you start thinking that u have learnt everything then your downfall begins.
So i really appreciate ur comments since it is always useful to discuss things with other poeple.
Regards.

#### gidimiz

Hi Waseem,

Thanks...
Donload the "FREE, EASY CODE WRITING For MPLAB Users.(Another New Ver. )" That i have posted. You will realy like it if you use C code.
Enjoy.

Status
Not open for further replies.