bit oldstate, oldstate1, oldstate2 ;
int x = 1, y = 1, v = 1;
void main()
{
TRISB = 0 ;
TRISD = 0xFF;
PORTB = 0 ;
oldstate = 0;
oldstate1 = 0;
oldstate2 = 0;
do {
if (Button (&Portd, 1,1,1))
{
Portb = x;
y = x ;
v = x ;
oldstate = 1;
}
if (oldstate && Button(&PORTD, 1, 1, 0))
{
x++;
oldstate = 0;
}
if (Button (&Portd, 2,1,1))
{
Portb = y*2;
oldstate1 = 1;
}
if (oldstate1 && Button(&PORTD, 2, 1, 0))
{
y = y*2;
x = y + 1;
v = y ;
oldstate1 = 0;
}
if (Button (&Portd, 3,1,1))
{
Portb = v/2;
oldstate2 = 1;
}
if (oldstate2 && Button(&PORTD, 3, 1, 0))
{
v = v/2;
x = v + 1 ;
y = v;
oldstate2 = 0;
}
} while(1);
}
byte buttons,old,edges,firstkey;
while(1){
old=buttons; //copy buttons
buttons=(PORTD>>1)&7; //get all button presses
if(buttons==0) { //if no button pressed
firstkey=0; //and key
}
edges=(buttons^old)&buttons; //keep only new presses
if(edges!=0&&firstkey==0){ //if first pressed
firstkey=edges; //store it
}
//at this point firstkey = 0,1,2 or 4 depending on which key pressed first
}
if (button 1 pressed) {
do whatever you need for button 1;
while (any of the buttons pressed) {
do nothing;
}
} else if (button 2 pressed) {
do whatever you need for button 2;
while (any of the buttons pressed) {
do nothing;
}
} else if (button 3 pressed) {
do whatever you need for button 3;
while (any of the buttons pressed) {
do nothing;
}
}
if (any of the buttons is pressed) {
switch (button pressed) {
case 1: do whatever you need for button 1;
case 2: do whatever you need for button 2;
case 3: do whatever you need for button 3;
}
while (any of the buttons pressed) {
do nothing;
}
}
This whole thing is rather convoluted, messy and difficult to follow. Using goto to jump in an out of infinite loops, assigning PORTB to itself, it looks like you started coding without a clear idea of what you were trying to do. I would suggest three things:Code:bit oldstate, oldstate1, oldstate2 ; void main() { ... A: do{ if ((oldstate && Button(&PORTD, 1, 1, 1)) || (oldstate1 && Button(&PORTD, 2, 1, 1)) || (oldstate2 && Button(&PORTD, 3, 1, 1))) { goto B; } ... } while(1); B: { PORTB = PORTB;{ ... } goto A; } }
Wp100, that's only a pulldown resistor Sir.
var: press = 0;
if (any of the buttons pressed) {
if (button 1 pressed) press |= 1;
if (button 2 pressed) press |= 2;
if (button 3 pressed) press |= 4;
} else {
switch (press) {
case 1: do whatever you need for button 1;
case 2: do whatever you need for button 2;
case 4: do whatever you need for button 3;
otherwise: do nothing;
}
press = 0;
}
But as we all know, actual programming was so complicated and has a big big different than writing only algorithms.
char press = 0;
char b1, b2, b3;
while(1) {
// do hardware dependent part here
// set b1, b2 and b3 to 1 if the corresponding button is currently pressed
// set b1, b2 and b3 to 0 if it's not
if (b1|b2|b3) {
if (b1) press |= 1;
if (b2) press |= 2;
if (b3) press |= 4;
} else {
switch (press) {
case 1:
// processing button 1
break;
case 2:
// processing button 2
break;
case 4:
// processing button 3
}
press = 0;
}
// do other stuff, not related to buttons
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?